Home is for the experience. Projects are for the ones who are interested!

How To Create Custom Post Type From Contact Form 7

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. 

One thought on “How To Create Custom Post Type From Contact Form 7

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

Leave a Reply

Your email address will not be published. Required fields are marked *