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;
}