How to redirect to another URL after WordPress Contact Form 7 Submission without using the ‘on_sent_ok’ hook method

The Contact Form 7 WordPress plugin is an amazing plugin with great features and the ability to add on additional settings. A great additional setting that we often used was the ability to redirect to another URL after a successful form submission using the ‘on_sent_ok’ hook method. Unfortunately the plugin author has stated that this function will no longer be supported by the end of 2017, which meant we had to find alternative methods. In this tutorial we will teach you a few different methods to redirect to another URL after WordPress Contact Form 7 Submission without using the ‘on_sent_ok’ hook method.

Utilize WordPress Contact Form 7’s Custom DOM Event to run JavaScript

The simplest way to redirect a visitor that’s filled out a successful WordPress Contact Form 7 submission to another URL is by utilizing the custom DOM event to run JavaScript. The following methods will redirect when the ‘wpcf7mailsent’ event occurs:

Method 1: Global redirect via WordPress functions

This option will utilize a WordPress function which automatically adds the JavaScript into your WordPress footer. However, please note that this option will redirect all of your forms to the same URL. Simply open your functions.php file then copy and paste the code below, please refer to the ‘Items to Note’ below for detailed setup instructions.

PHP

function add_this_script_footer(){ ?<script>
document.addEventListener( 'wpcf7mailsent', function( event ) {
    location = 'YOUR REDIRECT URL HERE';
}, false );
</script>
<?php }
add_action('wp_footer', 'add_this_script_footer');

Items to Note:

  • Make sure that you change where it says ‘YOUR REDIRECT URL HERE’ to a URL of your choice.

Method 2: Global redirect added directly to the WordPress footer.php file

With this option you can simply add the JavaScript directly into your WordPress footer.php file in case you don’t feel comfortable editing the WordPress functions. However, please note that this option will also only redirect all of your forms to the same URL. Simply open your footer.php file then copy and paste the code below, please refer to the ‘Items to Note’ below for detailed setup instructions.

JavaScript

<script>
document.addEventListener( 'wpcf7mailsent', function( event ) {
    location = 'YOUR REDIRECT URL HERE';
}, false );
</script>

Items to Note:

  • Make sure that you change where it says ‘YOUR REDIRECT URL HERE’ to a URL of your choice.
  • Paste the code before the following tags; ‘wp_footer()’, ‘/body’, and ‘/html’

Method 3: Redirect form on a specific page added directly to the WordPress footer.php file

With this option you can redirect a form on a specific page by adding the JavaScript along with some PHP if statements directly into your WordPress footer.php file. Simply open your footer.php file then copy and paste the code below, please refer to the ‘Items to Note’ below for detailed setup instructions.

PHP & JavaScript

<?php if ( is_page('YOUR PAGE ID HERE')) { ?>
<script>
document.addEventListener( 'wpcf7mailsent', function( event ) {
    location = 'YOUR REDIRECT URL HERE';
}, false );
</script>
<?php } ?>

Items to Note:

  • Make sure that you change where it says ‘YOUR PAGE ID HERE’ to the WordPress Page ID where your form is located.
  • Make sure that you change where it says ‘YOUR REDIRECT URL HERE’ to a URL of your choice.
  • Paste the code before the following tags; ‘wp_footer()’, ‘/body>, and ‘/html’

Method 4: Multiple redirects added directly to the WordPress footer.php file

With this option you can redirect multiple forms to different URL’s on different pages by adding the JavaScript along with some PHP if else statements directly into your WordPress footer.php file. Simply open your footer.php file then copy and paste the code below, please refer to the ‘Items to Note’ below for detailed setup instructions.

PHP & JavaScript

<php if ( is_page('YOUR PAGE ID HERE')) { >
<script>
document.addEventListener( 'wpcf7mailsent', function( event ) {
    location = 'YOUR REDIRECT URL HERE';
}, false );
</script>
<php } else if ( is_page('YOUR OTHER PAGE ID HERE')) { ?>
<script>
document.addEventListener( 'wpcf7mailsent', function( event ) {
    location = 'YOUR OTHER REDIRECT URL HERE';
}, false );
</script>
<?php } >

Items to Note:

  • Make sure that you change where it says ‘YOUR PAGE ID HERE’ to the WordPress Page ID where your form is located.
  • Make sure that you change where it says ‘YOUR REDIRECT URL HERE’ to a URL of your choice.
  • Make sure that you change where it says ‘YOUR OTHER PAGE ID HERE’ to the WordPress Page ID where your other form is located.
  • Make sure that you change where it says ‘YOUR OTHER REDIRECT URL HERE’ to a URL of your choice.
  • Repeat the PHP else if statement to add additional redirects on additional pages.
  • Paste the code before the following tags; ‘wp_footer()’, ‘/body’, and ‘/html’

Troubleshooting

If you are having problems getting this tutorial to work please reread the tutorial and try again, if you still cannot get it to work please leave us a comment below and we will respond as soon as possible. Please do not email us with problems regarding this tutorial, only comments will be responded to.