• Resolved louisreid

    (@louisreid)


    Hi,

    I know that at present mapping to an existing post type is possible but it there isn’t a GUI for it.

    Can you provide an example of how to use the following hook and filters

    add_action( 'cf7_2_post_save-post', 'your_function_name',10,3);
    function your_function_name($cf7_key, $submitted_data, $submitted_files){}
    add_filter( 'cf7_2_post_load-post', 'your_function_name',10,5);
    function your_function_name( $field_value_pairs, $cf7_key, $form_fields, $form_field_options, $cf7_post_id){
      //$form_field_options options set in the form field tags
      //$cf7_post_id the cf7 form id in case you need to load the form object
      foreach($form_fields as $field=>$type){
        $field_value_pairs[$field] = '';//load your value
      }
      //if this is a saved draft form, you can set your mapped post id
      //it will be set as hidden field so you can map the (re)submission to the same post
      $field_value_pairs['map_post_id'] = $post_id;
      return $field_value_pairs;
    }

    I’ve been trying a lot of different options but can’t get the hook to save the form as an existing post type.

    Thank you

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Aurovrata Venet

    (@aurovrata)

    Hi @louisreid

    v2.0 of the plugin is going to be released soon (I am putting the last touches to it this week I hope) which will have a full GUI interface for saving forms to existing posts.

    If you want try to do it with the filter, here is a quick example, assume you have the default CF7 contact form with the following fields: your-name, your-email, your-message which you wish to map to the inbuilt post type under the title, content and meta-field your_email respectively let’s say,

    
    add_action( 'cf7_2_post_save-post', 'a_new_post',10,3);
    function a_new_post($cf7_key, $submitted_data, $submitted_files){
      //let's make sure this is the right cf7 form (set the cf7 form unique key = 'contact-us' to easily identify it. 
      if('contact-us' != $cf7_key){
        return;
      }
      $title =  $submitted_data['your-name'];
      $content = $submitted_data['your-message'];
      $email = $submitted_data['your-email'];
      //first insert/update your new post.
      //for the current user if someone is logged in.
      $author = get_current_user_id();
      if(!$author) $author = 1;//defaults to admin user.
      $post_args = array(
        'post_type' => 'post',
        'post_status' => 'draft',
        'post_title' => $title,
        'post_content' => $content,
        'author'=>$author
      );
      //check if this is an existing post (draft) being saved/submitted
      if(isset($submitted_data['map_post_id']){
        $post_id = $submitted_data['map_post_id'];
        $post_args['ID']=$post_id;
        wp_udpate_post($post_args);
      }else{
        $post_id = wp_insert_post($post_args);
      }
      //update the meta-field
      update_post_meta($post_id, 'your_email', $email);
    }
    /* to load default values into the form */
    add_filter( 'cf7_2_post_load-post', 'load_post_to_form',10,5);
    function load_post_to_form( $field_value_pairs, $cf7_key, $form_fields, $form_field_options, $cf7_post_id){
      //check if the current user has a post
      $user_id = get_current_user_id();
      $title = $message = $email = '';
      $post_query = array(
        'post_type' => 'post',
        'post_status' => 'draft',
        'author' => $user_id
      );
      $posts = get_posts($post_query);
      if(!empty($posts)){
        $post = $posts[0];
        $post_id = $post->ID;
        $title = $post->post_title;
        $message = $post->post_content;
        $email = get_post_meta($post_id, 'your_email', true);
        //it will be set as hidden field so you can map the (re)submission to the same post
        $field_value_pairs['map_post_id'] = $post_id;
      }
      wp_reset_postdata(); //reset to the current query.
      //set form fields
      $field_value_pairs['your-name'] = $title;
      $field_value_pairs['your-message'] = $message;
      $field_value_pairs['your-email'] = $email;
      return $field_value_pairs;
    }
    
    Plugin Author Aurovrata Venet

    (@aurovrata)

    v2.0 of the plugin is now out and working fine (so far no one has reported any bugs) so I am closing this thread as you should be able to resolve your issue with the new version.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Map to existing post type’ is closed to new replies.