Use Contact Form 7 To Post Rows to Advanced Custom Fields Repeater

I was in a particular situation where I needed a task management system that could take fields through a form from Contact Form 7 and post them to rows in the Advanced Custom Fields Repeater field I set up.  The idea was to use a form from the front end of WordPress to post to the back-end of WordPress in a custom field.  You can accomplish this by adding code to your functions.php file in the WordPress theme.

This example assumes that you already have a form set up through Contact Form 7 and you have already purchased the Advanced Custom Fields’ Repeater Field plugin.

1. Get Your Field Key

You can grab your field key from the ACF Repeater Field by selecting ‘Show Field Key‘ from the Screen Options.  You will use this in your function to determine which field to add rows to.

cap1

2.  Add WPCF7 Before Send Mail Hook

You add the function below to your functions.php file within the theme.  You are hooking into Contact Form 7’s “wpcf7_before_send_mail’ function to grab the post data and post it to a WordPress repeater field as a new row.

add_action('wpcf7_before_send_mail', 'my_wpcf7_post');

function my_wpcf7_post($wpcf7) {

		$submission = WPCF7_Submission::get_instance();
		
		//GET URL AND ID OF POST
		$url = $submission->get_meta( 'url' );		
		$postid = url_to_postid( $url );		
		
		//GET VALUES
		$data = $submission->get_posted_data();
		$name = $data['the-title'];
		$type = $data['select-field']; 
		$description = $data['your-message'];

		
		//ADD ROWS TO REPEATER FIELD BY KEY
		$field_key = "field_552ffe7e7d43c";
		$post_id = $postid;
		$value = get_field($field_key, $post_id);
		$value[] = array("name" => $name, "type" => $type, "description" => $description);
		update_field( $field_key, $value, $post_id );
		


}

Obviously, your fields in the form and the repater field may be different.  The important things to take away from this functions script is that:

You’ve created the hook:

add_action('wpcf7_before_send_mail', 'my_wpcf7_post');

function my_wpcf7_post($wpcf7) {

		$submission = WPCF7_Submission::get_instance();

}

You’ve got the URL and ID of the post the form was submitted through:

		//GET URL AND ID OF POST
		$url = $submission->get_meta( 'url' );		
		$postid = url_to_postid( $url );

You got the data from the form:

		
		//GET VALUES
		$data = $submission->get_posted_data();
		$variable = $data['whatever-your-form-field-is'];

And you update the repeater field based on those elements:

		
		//ADD ROWS TO REPEATER FIELD BY KEY
		$field_key = "field_552ffe7e7d43c";
		$post_id = $postid;
		$value = get_field($field_key, $post_id);
		$value[] = array("name" => $name, "type" => $type, "description" => $description);
		update_field( $field_key, $value, $post_id );

Hope that helps!  Don’t forget you can always extend this to a plugin as opposed to a theme function as well.

7 Comments

  • Great article.

  • How do I create a single of my website content articles glance upon an additional one particular of my web pages?

  • You can also help in this use of the plugin https://wordpress.org/plugins/acf-contact-form-7/

  • hey !!
    I need to do this urgently in one of my project.I can pay

  • Thank you.

  • Hi,
    I understand the development of the solution, but it is not how to apply it.
    Where indicated and print the sub_field ? And if you have sub_field within another sub_field ? Where indicated the id of contact form 7?

    God job, thanks!

Leave A Comment

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