首页 > 解决方案 > 联系表 7 - wpcf7_before_send_mail 和 wpcf7_submit 的问题

问题描述

我目前正在一个网站上工作,该网站有用户通过使用 Contact Form 7 创建的表单提交。我正在尝试将其扩展到记录该活动(日期/时间、登录用户、从填写的字段中获取信息),并且我有尝试了很多次和方法来使用钩子 wpcf7_before_send_mail 和 wpcf7_submit 但似乎都没有工作。表单提交,但下面的代码没有运行。我什至尝试手动输入信息(表名、用户 ID、客户等)以查看它是否会提交,但仍然没有。

以下是我用来将一些信息保存到我希望将活动存储在其中的数据库的当前代码:

add_action('wpcf7_before_send_mail', 'save_in_database');

function save_in_database($data) {
    global $wpdb;
    $account_number = get_account_number();
    $department = get_user_role();

    if ($data->title == 'Social_Media_Tool_Form'){
        $submission = WPCF7_Submission::get_instance();
        if ( $submission ) {
            $cf7_data = $submission->get_posted_data();
            $name = $cf7_data['customer-name'];
        }
    }


    $activity_table_name = $wpdb->prefix .'_'. $account_number . '_activity_log';

    //Creates activity log
    $wpdb->insert($activity_table_name, array(
        'date'              =>  current_time('Y-m-d H:i:s'),
        'user_id'           =>  get_current_user_id(),
        'customer_name'     =>  $name,
        'activity_type'     =>  'social',
        'department'        =>  $department
    ));
}

我之前使用过相同的代码来记录其他活动,所以理论上它应该在这里工作。

我目前使用 Wordpress 5.1 并使用 Contact Form 7 5.1.1

标签: phpwordpresshookcontact-form-7

解决方案


我弄清楚了这个问题。在表单上,​​您必须将“subscribers_only: true”添加到其他设置中。因为这没有设置,所以它不允许我从当前用户那里提取信息,这就是它没有提交到数据库的原因。


推荐阅读