How To Create Custom Post Type From Contact Form 7

Sometimes this action is a required one, especially when your website has a contact form thourgh users can register its own posts. So in this tutorial we will create a post in our custom post type from wordpress at every contact form submission. This is not hard to achive especially if you already have custom post type created, if not you can create a new custom post type using CPT UI is a wordpress plugin and it is very easy to use if you search a little on google you will find out everything you need to do to create a new custom post type with cpt ui. 

Our code should be places in functions.php file and it will look like below:

add_action('wpcf7_before_send_mail', 'save_application_form');
function save_application_form($wpcf7){
  global $wpdb;
  $submission = WPCF7_Submission::get_instance();

  $field_1 = $_POST['field_1'];

  $my_post = array(
    'post_title' => $field_1,
    'post_status' => 'publish',
    'post_type' => 'custom_post_type_name',
    'post_author' => 1,
  );
  $the_post_id = wp_insert_post( $my_post );
}

Add the above function anywhere in the functions.php and just replace the $_POST with your contact form fields. This is just an example you will can from here add how many fields you need and create a new post with them. Starting from this function you can populate ACF fields if you have too. Just create a new ACF group for your custom post type and then just update the fields after you run wp_insert_post(), I will make a new tutorial for this and I will show you how to create and save ACF fields values from contact form 7 fields. The above function will add only the title, but you can add post_content to my_post array and it will add the content too. 

5 1 vote
Article Rating
Subscribe
Notify of
guest
6 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Eric Cicero
Eric Cicero
3 years ago

I am going to try this for an upcoming project. I will let you know how it goes.

yossi
yossi
2 years ago

is there a video tutorial?
thank you!

floi5314
floi5314
1 year ago

Works like a charm, submitting the form creates an item in my premade custom post type.

Now, I added some acf fields to that custom post type. How should I update or populate those with the submitted formdata?

Let’s say I have a email field in the form. When the user fills this in and submits the form, a custom post type is created. This cpt has a acf field in which I want the submitted email address to be filled in automatically.

I already found something about an update_field function, but after some tinkering about haven’t got this working yet…

Any tips would be appreciated.

floi5314
floi5314
1 year ago

This article you wrote helped me solve the issue, thanks for that!

Viking
Viking
1 month ago

It’s great, thank you!

Last edited 1 month ago by Viking
Viking
Viking
1 month ago

It’s Great, thank you! But can you tell us how can we put an attachment in the post with this method?

6
0
Would love your thoughts, please comment.x
()
x