如何将wordpress中wpform的值存储到mysql数据库?

zaq34kh6  于 2021-06-25  发布在  Mysql
关注(0)|答案(4)|浏览(757)

如何将wordpress中wpform的值存储到mysql数据库中?当我们使用wpforms时,我必须添加php部分来存储数据?当我们使用wpforms的免费版本而不购买它时,我们能将表单数据存储到表中吗?

sqserrrh

sqserrrh1#

如果您使用的是wp forms-lite,您不能直接这样做,要么您需要升级到pro版本,要么构建您自己的自定义保存操作。
如果您决定使用自己的自定义版本,下面将详细介绍如何在wp表单上交互表单提交。
wpforms有一些操作可用于在表单提交后执行自己的自定义操作:
WPU表格\流程\完成

do_action( 'wpforms_process_complete', $this->fields, $entry, $form_data, $entry_id );

通过在您自己的模板或插件上使用wordpress钩子add\u操作来处理所描述的任何事件,您可以获得表单数据并执行所需的处理。
wordpress添加操作的参考可以在官方文档中看到。https://developer.wordpress.org/reference/functions/add_action/
做了一个简短的片段,可以帮助您入门:

add_action("wpforms_process_complete", 'function_save_custom_form_data');

function function_save_custom_form_data($params) {

    foreach($params as $idx=>$item) {
        $field_name = $item['name'];
        $fiel_value = $item['value'];
        // Do whatever you need
    }
    return true;

}

如果你需要更多的细节,请告诉我。

nhn9ugyo

nhn9ugyo2#

wpforms的免费版本不保存表单中捕获的条目详细信息。因此,您需要编写自定义代码来保存自定义db表中的值,然后显示它们。
wpforms的钩子可以用来保存在wpforms中输入的数据

/*hook to save entries coming from WPforms into database*/
add_action( 'wpforms_process_entry_save', array( $this, 'ank_wpforms_save_entries' ), 10, 4 );

public function ank_wpforms_save_entries( $fields, $entry, $form_id, $form_data ) {
    //no need to sanitize data coming from WPForms as it is being sanitized in WPForms plugin before this hook using
    //wpforms_process_validate_{$field_type} in class-process.php
    $data                  = array();
    $data['form_id']       = $form_id;
    $data['entry_details'] = $fields;
    //additional sanity checks are also performed while json encoding in "add" before adding in database
    ank_wpforms_entry()->get_class_instance( 'entry-db' )->add( $data );
}

或者,您可以使用这个免费插件将来自wpforms的条目保存到wordpress数据库中,然后在wordpress Jmeter 板中显示它们-https://wordpress.org/plugins/add-entries-functionality-to-wpforms/

093gszye

093gszye3#

例如,创建自定义表 wpforms_entries 以及 wpforms_entry_meta 在数据库中存储表单数据。使用提供的行动钩 wpforms_process_complete 以存储表单条目。

add_action( 'wpforms_process_complete', 'process_entry', 5, 4 );
function process_entry( $form_fields, $entry, $form_data, $entry_id ) {

    global $wpdb;
    $form_id = $form_data['id'];
    $entry_data = array(
        'form_id'         => $form_id,
        'status'          => 'publish',
        'referer'         => $_SERVER['HTTP_REFERER'],
        'date_created'    => current_time( 'mysql' )
    );

    // Insert into wpforms_entries custom table.
    $success = $wpdb->insert( $wpdb->prefix . 'wpforms_entries', $entry_data );
    $entry_id = $wpdb->insert_id;

    // Create meta data.
    if ( $entry_id ) {
        foreach ( $form_fields as $field ) {
            $field = apply_filters( 'wpforms_process_entry_field', $field, $form_data, $entry_id );
            if ( isset( $field['value'] ) && '' !== $field['value'] ) {
                $field_value    = is_array( $field['value'] ) ? serialize( $field['value'] ) : $field['value'];
                $entry_metadata = array(
                    'entry_id'   => $entry_id,
                    'meta_key'   => $field['name'],
                    'meta_value' => $field_value,
                );
                // Insert entry meta.
                $wpdb->insert( $wpdb->prefix . 'wpforms_entrymeta', $entry_metadata );
            }
        }
    }
}

参考文献:https://github.com/sanzeeb3/entries-for-wpforms/blob/master/includes/functions-wpfe-core.php#l59
或者,插件本身也可用:https://wordpress.org/plugins/entries-for-wpforms/

gv8xihay

gv8xihay4#

wpforms将所有表单数据存储在原生wordpress数据库中的两个表中。他们是:

wp_wpforms_entries: In this table, the field values for entries are stored.
wp_wpforms_entry_meta: This table contains meta information about your entries such as IDs associated and the date that entries were submitted.

发布表单后,请确保添加表单条目,以便我们可以从您的wordpress Jmeter 板访问该条目。此外,在窗体生成器中,转到“设置”» 并确保存储在wordpress中的条目未被禁用。

相关问题