How to save contact form 7 data in Custom Post Types (CPT)

admin2025-06-03  4

Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 6 years ago.

Improve this question

I want to store the data of contact form 7 (CF7) in a custom-post-types (CPT). There are several plugins like:

/

/

/

which store the data in their own cpt. But it is not possible to redirect the data into a CPT of your own.

Is there a plugin or another way how I can store the data from CF7 in a CPT of a third plugin?

Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 6 years ago.

Improve this question

I want to store the data of contact form 7 (CF7) in a custom-post-types (CPT). There are several plugins like:

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

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

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

which store the data in their own cpt. But it is not possible to redirect the data into a CPT of your own.

Is there a plugin or another way how I can store the data from CF7 in a CPT of a third plugin?

Share Improve this question asked Feb 12, 2019 at 1:03 SeverinSeverin 551 gold badge1 silver badge8 bronze badges 2
  • try the plugin Post My Contact Form 7 it does exactly that – Aurovrata Commented Jul 23, 2019 at 13:37
  • Thanks, a perfect solution – Severin Commented Jul 23, 2019 at 17:01
Add a comment  | 

1 Answer 1

Reset to default 5

If you want to save the Mail Content after the Contact Form was sent (or failed), you can use the wpcf7_mail_sent and wpcf7_mail_failed Hookes like this:

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;
}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748917196a314797.html

最新回复(0)