Call a PHP function after Contact Form 7 submit

By Faruque Ahamed Mollick

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
}

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:

$_POST["your-subject"]

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.

 

Leave a Reply

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