April 17, 2015
- Wpcf7_before_send_mail Get Form Data
- Wpcf7_before_send_mail Redirect
- Wpcf7_before_send_mail Skip Mail
- Wpcf7_before_send_mail Get Form Id
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 “wpcf7beforesendmail’ function to grab the post data and post it to a WordPress repeater field as a new row. Let’s hook into wpcf7beforesendmail, get the submitted form data, find the enquiry-type dropdown option and then use an if/else statement to send the notifications to specific email addresses.
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.
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.
2 4 6 8 10 12 14 16 18 20 22 24 26 | add_action('wpcf7_before_send_mail','my_wpcf7_post'); functionmy_wpcf7_post($wpcf7){ $submission=WPCF7_Submission::get_instance(); //GET URL AND ID OF POST $postid=url_to_postid($url); //GET VALUES $name=$data['the-title']; $description=$data['your-message']; $field_key='field_552ffe7e7d43c'; $value=get_field($field_key,$post_id); $value[]=array('name'=>$name,'type'=>$type,'description'=>$description); |
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:
2 4 6 | add_action('wpcf7_before_send_mail','my_wpcf7_post'); functionmy_wpcf7_post($wpcf7){ $submission=WPCF7_Submission::get_instance(); } |
You’ve got the URL and ID of the post the form was submitted through:
2 | $url=$submission->get_meta('url'); |

You got the data from the form:

2 | $data=$submission->get_posted_data(); |
And you update the repeater field based on those elements:
2 4 6 | $field_key='field_552ffe7e7d43c'; $value=get_field($field_key,$post_id); $value[]=array('name'=>$name,'type'=>$type,'description'=>$description); |
Hope that helps! Don’t forget you can always extend this to a plugin as opposed to a theme function as well.
7 Comments
Ajay Radadiya
Contact form 7 woocommerce product dropdown field and Contact form 7 custom post type dropdown more good plugin
Nike Air force
Great article.
Simone
How do I create a single of my website content articles glance upon an additional one particular of my web pages?
Beetle
You can also help in this use of the plugin https://wordpress.org/plugins/acf-contact-form-7/
rox
hey !!
I need to do this urgently in one of my project.I can payToplu Sms
Thank you.
Rub
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!
Some notes about using (and perhaps abusing) the Contact Form 7 process in a WordPress site. (Contact Form 7 is a popular plugin for WordPress sites that lets you easily create a Contact Us type form. We use it here, although we’ve also tweaked what it does with our FormSpammerTrap for Contact Form 7 plugin that effectively reduces Contact Form spam. You can check out that plugin – and our other FormSpammerTrap spam-bot blocking techniques at our FormSpammerTrap.com site.)
We were inspired by an article we found via the googles while searching for information on CF7 hooks, because we wanted to do things to the CF7 form content after it was submitted, but before the form was emailed. The article is here. The code is theirs, but we wanted to explain it in more detail (mostly for our own purposes, as we wanted to use a similar process for our own purposes).
Wpcf7_before_send_mail Get Form Data
Let’s take a look at what we can do after the Contact form is submitted, and before CF7 emails the message. Our intent is to change the Subject line of the form to something other than what the visitor entered. You could easily use this code for your own purposes.
First, lets ‘hook’ into the wpcf7_before_send_mail process. This is done with the standard WP add_action function. (Remember that you can ‘hook’ into any WP function that has a ‘hook’. Ask the googles how the add_action thing works.) Here is the code that you would place in your Child Theme’s function.php file (or perhaps in a plugin):
This will wait for the hook to be called, and run the my_change_subject_mail() function.
Now we need to create the my_change_subject_mail function. I’m going to show and explain each line separately. We’ll put the entire function at the end.
This defines the function:
This will get the current WPCF7_ContactForm object. We need this object to change the subject value therein.
Here we get the Submission object, which is generated when the user hits the ‘send’ submit button.
Next, let’s ensure that the contact form has been submitted.

If the form is empty, exit the function.
Wpcf7_before_send_mail Redirect

This is how we read a value of the form. The basic CF7 Contact Form has variables, this next line references the variable that was defined as ‘your-message’. If you want to work with another field on the form, use that field name as the parameter of the $posted_data array.
At this point, you could do a search/replace on the subject, or anything else. For instance, maybe you’d like to store the message fields in a database. You would set various variables – like we did with the $subject variable – to values from the $posted_data array. Then you could store those variables into a table in your database.
We’re not going to do anything with the $subject variable, we just wanted to show you that it could be done.
Our intent is to change the ‘subject’ of the email to something else. First, we have to read the property of the $mail object. This is the object that CF7 uses to create the email.
Since the subject line in our CF7 form is called ‘subject’, here is how we change the contents of that field:

Wpcf7_before_send_mail Skip Mail
Now that we have changed a value in the $mail array, we need to set that value in the WPCF7_ContactForm object:
If we are curious about all of the contents of the WPCF7_ContactForm object, we could quickly write it to the error.log file. This is OK for our purposes, as we are on a development site, and the error.log is a quick way to look at something. Note that you can’t ‘echo’ or ‘print’ anything to the screen in this process; it won’t be shown.
Wpcf7_before_send_mail Get Form Id
All done. We’ll return the object just in case it is needed.
By looking at the contents of the object, you might find other things that you might like to change. The $mail part of the object is stuff that will be emailed.
The result of all of this is that we have changed the subject line of the emailed message with out little function. We could add additional parts (maybe some extra data like the IP address of the sender, or whatever) to the $mail array -maybe the message content – and that would also be sent.
All done! Any questions, use the comments. We’ll try our best to muddle through an answer to your question.
Here’s the entire function:
