繁体   English   中英

如何将值从 WordPress 中的 WPForm 存储到 MySQL 数据库?

[英]How can I store the values from a WPForm in WordPress to a MySQL database?

如何将 WordPress 中的 WPForm 中的值存储到 MySQL 数据库中?当我们使用 WPForms 时,我必须添加 PHP 部分来存储数据? 当我们使用免费版本的 WPForms 而不购买时,我们可以将表单数据存储到表格中吗?

如果您使用的是 WP Forms Lite,则无法直接执行此操作,您需要升级到 PRO 版本或构建自己的自定义保存操作。

如果您决定使用自己的自定义版本,请参阅下面有关如何在 WP Forms 上交叉表单提交的一些详细信息。

WPForms 有一些操作,您可以在表单提交后使用它们来执行自定义操作:

  1. wpforms_process_complete

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

通过在您自己的模板或插件上为所描述的任何事件使用 wordpress 挂钩 add_action,您可以获取表单数据并进行所需的处理。

wordpress add_action 的参考可以在官方文档中看到。 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;

}

如果您需要更多详细信息,请告诉我。

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 仪表板访问该条目。 此外,在您的表单构建器中,转到设置 » 常规并确保未禁用 WordPress 中存储的条目。

在数据库中创建自定义表,例如wpforms_entrieswpforms_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/

免费版 WPForms 不保存在表单中捕获的条目详细信息。 因此,您需要编写自定义代码以将值保存在自定义数据库表中,然后显示它们。

下面的 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 仪表板中 - https://wordpress.org/plugins/add-entries-functionity-to-wpforms/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM