如何在自定义发布类型(CPT)中保存联系人Form 7数据

时间:2019-02-12 作者:Severin

我想将联系人表单7(CF7)的数据存储在自定义帖子类型(CPT)中。有几个插件,如:

https://de.wordpress.org/plugins/cf7-responses/

https://de.wordpress.org/plugins/cf7-store-to-db-lite/

https://de.wordpress.org/plugins/advanced-cf7-db/

它们将数据存储在自己的cpt中。但不可能将数据重定向到您自己的CPT中。

是否有插件或其他方法可以将CF7中的数据存储在第三个插件的CPT中?

1 个回复
最合适的回答,由SO网友:HU ist Sebastian 整理而成

如果要在发送(或失败)联系人表单后保存邮件内容,可以使用wpcf7_mail_sentwpcf7_mail_failed 像这样的妓女:

add_action(\'wpcf7_mail_sent\',\'save_my_form_data_to_my_cpt\');
add_action(\'wpcf7_mail_failed\',\'save_my_form_data_to_my_cpt\');

function save_my_form_data_to_my_cpt($contact_form){
    $submission = WPCF7_Submission::get_instance();
    if (!$submission){
        return;
    }
    $posted_data = $submission->get_posted_data();
    //The Sent Fields are now in an array
    //Let\'s say you got 4 Fields in your Contact Form
    //my-email, my-name, my-subject and my-message
    //you can now access them with $posted_data[\'my-email\']
    //Do whatever you want like:
    $new_post = array();
    if(isset($posted_data[\'my-subject\']) && !empty($posted_data[\'my-subject\'])){
        $new_post[\'post_title\'] = $posted_data[\'my-subject\'];
    } else {
        $new_post[\'post_title\'] = \'Message\';
    }
    $new_post[\'post_type\'] = \'my_awesome_cpt\'; //insert here your CPT
    if(isset($posted_data[\'my-message\'])){
        $new_post[\'post_content\'] = $posted_data[\'my-message\'];
    } else {
        $new_post[\'post_content\'] = \'No Message was submitted\';
    }
    $new_post[\'post_status\'] = \'publish\';
    //you can also build your post_content from all of the fields of the form, or you can save them into some meta fields
    if(isset($posted_data[\'my-email\']) && !empty($posted_data[\'my-email\'])){
        $new_post[\'meta_input\'][\'sender_email_address\'] = $posted_data[\'my-email\'];
    }
    if(isset($posted_data[\'my-name\']) && !empty($posted_data[\'my-name\'])){
        $new_post[\'meta_input\'][\'sender_name\'] = $posted_data[\'my-name\'];
    }
    //When everything is prepared, insert the post into your Wordpress Database
    if($post_id = wp_insert_post($new_post)){
       //Everything worked, you can stop here or do whatever
    } else {
       //The post was not inserted correctly, do something (or don\'t ;) )
    }
    return;
}

相关推荐

Storing user submitted forms

我想知道创建一个只有注册用户才能看到的表单的最佳方法是什么。我想在这里实现的主要想法是让用户登录并访问一个页面,其中有一个可以多次提交的申请表。每个表单将存储在数据库中并与用户关联。它还可以选择将文件附加到系统。在WP仪表板中,管理员可以浏览系统中的所有用户,并查看他们提交的所有应用程序。是否最好在仪表板中创建一个自定义选项卡并显示每个用户?表单数据是否会被分类为每个用户的元数据?我对如何开发这个有点困惑,如果有任何建议,我将不胜感激。