Execute function before Contact Form 7 submit

13 May, 2014 by Tom Elliott

Contact Form 7 is one of the most popular form management plugins available to WordPress, but it can be a little tricky when you need to extend the functionality beyond the default available options.

Fortunately, we have on_sent_ok and wpcf7_before_send_mail which allow us to run our own custom commands before and after form submission.

For example, I recently needed to create a function that needed to dynamically generate an XML file on form submission, before attaching the XML to the Contact Form 7 email.

The code below creates a similar custom function before a Contact form 7 form is submitted. We need to add this code to the theme’s functions.php file, adding the wpcf7_before_send_mail hook as an add_action call.

add_action('wpcf7_before_send_mail', 'CF7_pre_send');

function CF7_pre_send($cf7) {
    //Put PHP here

One thing to note is that if you try and use echo to try and show any output, this will not be displayed which could look like the function isn’t being triggered. This is due to how Contact Form 7 is designed to work using AJAX commands that execute PHP.

If you want to use form field data, we can grab this using regular $_POST variables as below:

add_action( 'wpcf7_before_send_mail', 'CF7_pre_send' );

function CF7_pre_send($cf7) {
   $output = "";
   $output .= "Name: " . $_POST['name'];
   $output .= "Email: " . $_POST['email'];
 $output .= "Message: " . $_POST['message'];

 file_put_contents("cf7outputtest.txt", $output);

In this example, I’ve used file_put_contents which will save the submitted form data as a regular text file. The file is saved to the WordPress site root.


  • Jedediah says:

    This is great and exactly what I needed for a clients website. However, I need to ask the best way to format the resulting text file. As-is, it puts it all on the same line; no breaks or anything. Where can I add a or a so that it’s easier for the client to read? Thanks a lot.

  • adc ben says:

    pls hw can i do a server validation,i created a form with contact form 7 nd am alsoo using contact form db to view my submitted datas,i have an application written in delphi,am trying too do a server validation on my form against the already registered users in my delphi application.

  • Robert says:

    Hi Tom, great post. In case I want the output to be an XML file attached to the form on form submission, what would the file_put_contents line be then?

  • Luca says:

    Hi Tom,
    I was looking for something similar but I was wondering if is it possible to run validation fields before sending data to an external url instead sending mail.
    I’ve integrated cf7 with a 3rd party service but once I submit it doesn’t show any error messege for the required fields. Do you have any Idea? This fact is happened to you?

  • John says:

    Thanks Tom,
    This hook was driving me crazy. I can’t get it to work even after reading the documentations and lots of forums.
    Your example was really easy to follow and I got it to work on my first try. I’m doing a function that inserts the post data to a table before sending.

  • TheMadMango says:

    Just a heads up: on_sent_ok is no longer a PHP hook. wpcf7_before_send_mail is, and as far as I can tell is effectively the same thing, except that the user has to wait for the actions to complete before the form submission shows as completed and successful.

    Thanks Tom for writing this post. I probably would’ve gone with Gravity Forms were it not for this, and Gravity is a little overkill for what I’m trying to do.

  • Leave a Reply

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