Kyle Poyser
Kyle Poyser

Reputation: 51

Contact form 7 to custom post type

I would like to process a contact form from contact form 7 into a custom post type.

Currently, this is what I have:

<?php 

if ( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) &&        $_POST['action'] == "front_post") {

//store our post vars into variables for later use
//now would be a good time to run some basic error checking/validation
//to ensure that data for these values have been set
$title     = $_POST['title'];
$content   = $_POST['content'];
$Interest   = $_POST['Interest'];
$post_type = 'purchase';

// the array of arguments to be inserted with wp_insert_post
$new_post = array(
'post_title'    => $title,
'post_content'  => $content,
'tags_input'  => $tags,
'posted_data' => $Interest,
'post_status'   => 'publish',
'post_category' => array('0',$_POST['cat']),          
'post_type'     => $post_type 
);

//insert the the post into database by passing $new_post to wp_insert_post
//store our post ID in a variable $pid
//we now use $pid (post id) to help add out post meta data
$pid=wp_insert_post($new_post);

//we now use $pid (post id) to help add out post meta data
add_post_meta($pid, 'cust_key', $custom_field);
}
?>

Here is a link to the actual form: http://stage.icardpromotions.com/create-purchase-order/

I need to be able to pull in all of the info form this form into the custom post type "purchase"

As you can see, I am currently pulling in the post_content, post_title, etc.

I have also tried to pull in content from content form by input name "Interest" but it dose not work.

Does anyone have a clue how to do this?

Upvotes: 5

Views: 17668

Answers (5)

kadore
kadore

Reputation: 131

While the answer with the most upvotes in this thread works it has some flaws.

First of which is: you can still submit the form and thus create a post in wordpress if for instance you remove "disabled" tag from your submit button. So you can essentially bypass the validation.

Second problem that I had, was probably specific to my usecase, since this function triggers on any cf7 form submission on website. If you have more than one cf7 form this will create posts even if users submit something in some totally different form.

To solve the first problem I think the best way is to hook custom function to "wpcf7_before_send_mail" instead of "wpcf7_posted_data" And to solve the second problem is to check for id of the form you wish to trigger the effect on.

This is how I solved these problems:

function save_cf7_data_to_cpt( $contact_form ) {
    
    if( $contact_form->id() !== $my_form_id )
    return; //dont run the rest if it is not the form you want it to be, $my_form_id we look up in admin or shortcode...

    $submission = WPCF7_Submission::get_instance();
    if ( $submission ) {
        $posted_data = $submission->get_posted_data();
    } //we get to $post_data in this way since it is not provided like in the wpcf7_posted_data approach

  $args = array(
    'post_type' => 'testemonial',
    'post_status'=>'draft',
    'post_title'=>$posted_data['your-name'],
    'post_content'=>$posted_data['your-message'],
  );
  $post_id = wp_insert_post($args);

  if(!is_wp_error($post_id)){
    if( isset($posted_data['your-name']) ){
      update_post_meta($post_id, 'your-name', $posted_data['your-name']);
    }
    if( isset($posted_data['your-message']) ){
      update_post_meta($post_id, 'your-message', $posted_data['your-message']);
    }
 //and so on ...
 return $posted_data;
}
}

add_filter( 'wpcf7_before_send_mail', 'save_cf7_data_to_cpt' ); //hook into wpcf7_before_send_mail to ensure validation is ok

Upvotes: 2

Aurovrata
Aurovrata

Reputation: 2279

there is a plugin to do this, Post My CF7 Form.

The plugin allows you to map a CF7 form and its fields to a an existing post type or a new custom post type.

The mapping process is done using an interactive UI admin page which gives you the option to map your fields to post fields (title, content, excerpt, slug, author) as well as post meta-fields.

In addition, the plugin also introduces a save submit button to allow users to save a draft version of the form, this is especially useful for large complex forms.

The plugin can automatically load taxonomy terms in select/checkbox/radio fields that have been mapped to that taxonomy, thus enabling created posts to be automatically assigned to users selected terms.

The plugin has multiple hooks & filters to customise the process flow.

Upvotes: 0

Purnendu Sarkar
Purnendu Sarkar

Reputation: 342

Can u also use


    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['your-name']) && !empty($posted_data['your-name'])){
            $new_post['post_title'] = $posted_data['your-name'];
        } else {
            $new_post['post_title'] = 'Message';
        }
        $new_post['post_type'] = 'inquiry'; //insert here your CPT
        if(isset($posted_data['tel-901'])){
            $new_post['post_content'] = $posted_data['tel-901'];
        } 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['your-email']) && !empty($posted_data['your-email'])){
            $new_post['meta_input']['sender_email_address'] = $posted_data['your-email'];
        }
        if(isset($posted_data['checkbox-674']) && !empty($posted_data['checkbox-674'])){
            //$new_post['meta_input']['sender_name'] = $posted_data['checkbox-674'];
            
            $ChildSeat=$posted_data['checkbox-674'];
            $Child_Seat='';
            for($a=0;$a<count($ChildSeat);$a++)
            {
                $data['checkbox-674']=$_POST['checkbox-674'][$a];
                $Child_Seat.=$data['checkbox-674'].'<br>';
                $new_post['post_content'] = $Child_Seat;
                
            }
      
      }
        //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;
    }

Upvotes: 0

sagar
sagar

Reputation: 590

 function save_posted_data( $posted_data ) {


       $args = array(
         'post_type' => 'post',
         'post_status'=>'draft',
         'post_title'=>$posted_data['your-name'],
          'post_content'=>$posted_data['your-message'],
       );
       $post_id = wp_insert_post($args);

       if(!is_wp_error($post_id)){
         if( isset($posted_data['your-name']) ){
           update_post_meta($post_id, 'your-name', $posted_data['your-name']);
         }
        // if( isset($posted_data['your-email']) ){
        //   update_post_meta($post_id, 'your-email', $posted_data['your-email']);
        // }
        // if( isset($posted_data['your-subject']) ){
        //   update_post_meta($post_id, 'your-subject', $posted_data['your-subject']);
        // }
         if( isset($posted_data['your-message']) ){
           update_post_meta($post_id, 'your-message', $posted_data['your-message']);
         }
      //and so on ...
      return $posted_data;
     }
 }

add_filter( 'wpcf7_posted_data', 'save_posted_data' );

-------------------- Explaining It-------------------------

First make function and add a hook wpcf7_posted_data to it

---first step---

function save_posted_data( $posted_data ) {

}
add_filter( 'wpcf7_posted_data', 'save_posted_data' );

---second step---

and now u need to add some arguments to the post that needs to be populated using wp_insert_post();

$args = array(
         'post_type' => 'post',
         'post_status'=>'draft',
         'post_title'=>$posted_data['your-name'],
          'post_content'=>$posted_data['your-message'],
       );
$post_id = wp_insert_post($args);

---third step---

check if that populated items is error or not

if(!is_wp_error($post_id)){ //do ur stuffs }

---fourth step---

now checking isset the field or not and updating the metas eg post

if( isset($posted_data['your-name']) ){
    update_post_meta($post_id, 'your-name', $posted_data['your-name']);
}

and in last return the value

return $posted_data;

Full code is above.

Upvotes: 9

Aurovrata
Aurovrata

Reputation: 2279

here is a quick tip as to how to go about achieving the above using your own code, first register your custom post

    add_action('init', 'my_custom_post');
    function (){
      $args = array(
       /*post type registration parameters*/
      );
      register_post_type( 'my_custom_post', $args );
    }

next, you want to capture your posted data and create a new post

    add_filter( 'wpcf7_posted_data', 'save_posted_data' );
    function save_posted_data( $posted_data ) {
      $args = array(
        'post_type' => 'my_custom_post',
       /*other default parameters you want to set*/
      );
      $post_id = wp_insert_post($args);
      if(!is_wp_error($post_id)){
        if( isset($posted_data['form-field-name']) ){
          update_post_meta($post_id, 'form-field-name', $posted_data['form-field-name']);
        }
      //and so on ...
      return $posted_data;
    }

Upvotes: 1

Related Questions