How To Update ACF Fields With Contact Form 7

In the last tutorial I showed you how to create a new custom post type with contact form 7. Now I will show you how to update any acf fields at contact form 7 submission. If you don't know how to create a custom post type install the cpt ui plugin and it will create for you how many you want just add a name it you are done. We will the same function as we do in the last tutorial but we will add few extra functions to update the fields by its id with our values which comes from contact form 7 fields when user submit the form. In this example we will use as exemple text inputs but you can do this implementation for repeaters or images too. I am sure it will work with images too because I used this type of functionality some time ago. 

So check the below function to understand what you should add in your functions.php file.

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

  update_post_meta( $the_post_id, 'my_acf_field', $_POST['field2']);
  update_post_meta( $the_post_id, 'my_acf_field2', $_POST['field3']);
  update_post_meta( $the_post_id, 'my_acf_field3', $_POST['field4']);
  update_post_meta( $the_post_id, 'my_acf_field4', $_POST['field5']);
}

 

As you can see, first we add our function to contact form 7 hook. When sent event is fired our function will run as well, then we get all fields that interest us, and just update the fields by post_id beacuse we got the post id when we inserted the post in the database. So, the steps are like the following: add our function to contact form 7 hook then check when the contact form 7 is submitted then save the post in the database and store in a variable its id. Having post id we can change any want we need for that post because we know its identity. I hope this tutorial will help you, let any questions yo have in the comments.  You can check the previews tutorial here.

This is the easiest way to update acf fields with contact form 7.

0 0 votes
Article Rating
Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Christian Melo
Christian Melo
5 years ago

HI there, thanks for the amazing tutorial, i did exactly how u did and works like a charm, but now i have problem, im trying to upload an image from the CF7 Form to my ACF image field, and its not inserting, can you help me please? Thanks

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