Call a PHP function after Contact Form 7 submit

Contact Form 7 is a popular WordPress plugin that can create contact forms on a WordPress based site. We all know the importance of a contact form on the website. So, in that sense, it is a very useful plugin.

Well, the CF7 plugin also allows developers to extend its feature. The wpcf7_before_send_mail is an action hook of this plugin that executes a function before sending the form data to the email.

With wpcf7_before_send_mail action hook, you can call a PHP function after Contact Form 7 submit. I am going to show you how to do that.

First, create the custom PHP function that you want to execute after CF7 form submit:

function cspd_call_after_for_submit( $contact_data ){
    // Our code will goes here

The $contact_data  variable contains all the form data as an array format. You can fetch any of the data from this variable when it is necessary.

Now add the function to the action hook:

add_action( 'wpcf7_before_send_mail', 'cspd_call_after_for_submit' );

All the code you put inside your function will be run after the submitting of Contact Form 7 and before sending form data to the email.

now the interesting part is that you can get the posted data of the data submitted to contact form 7. For example, if you want to get the contact message subject from a CF7 form with the subject name “your-subject”, then below is the code to get the subject inside the function:


You can see that, we can get the form data just by using the PHP $_POST global variable. It is just like getting form data with PHP post method.

So you can see that the process of getting data of CF7 field is quite easy. You can use the code in your custom WordPress plugin so that it can work with the CF7 plugin. Developers can use wpcf7_before_send_mail action hook to develop add-on of Contact Form 7 plugin.


Also, read:


You have learned how to call a PHP function after Contact Form 7 submit. I hope this tutorial will be helpful to you.


10 responses to “Call a PHP function after Contact Form 7 submit”

  1. Matze says:

    Thanks for your great description but where should I write the add_action call?
    In the functions.php file?

    • Faruque Ahamed Mollick says:

      You can call the add_action in functions.php or in a plugin-specific file. Wherever you like, you can call it.

  2. manni says:

    if i will implement redirection here like header location and redirect to another page ,its working or not

  3. Mario says:

    Hello, I hope I am not wasting your time but in which functions.php file? I have a few of them in my root folders.
    Thank you, Mario

  4. Don Phelix says:

    Hi, I hope I am not late. I was trying to add more cf7 .. I have the data but I want to insert them into the forms one by one with dfnt id.. is that possible… I used Ajax to bring the form data into the function.php … How do I insert those data into cf7 using wpcf7_before_send_mail

  5. varun sharma says:

    how i display product name in contact forms hidden field with this on shop page

  6. Jacques Bayol says:

    Hi Faruque,
    if I have 2 different cf7 forms is there a way to deactivate “function cspd_call_after_for_submit” for one of the forms?
    Jacques from France

  7. Atul Rai says:

    Hi, It will trigger on all contact forms submission if I want to call a function on a specific form only, Is there any process to handle the same?

Leave a Reply

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