Post

Another Random Media & Technology Post

Contact Form 7 Redirection

Contact Form 7 Redirection

The Contact Form 7 Plugin is an incredibly useful plugin and a great addition to any WordPress Installation. It is very well supported and regularly updated.

It does not, however, have a built-in redirect to a thank you page. Nonetheless, it has been very simple, to just use the

on_sent_ok

method to add a redirection to a “Thank You” page after a successful form submission. But that method is being deprecated soon and I wanted to find a simple alternative.

This Contact Form 7 support page explains a method of using javascript in order to enable redirection. But if you have more than one contact form that redirects to more than one thank you page, how do you differentiate?

One method might be to add the javascript code to separate template. But it seems easier to add the following piece of code to the WP functions.php file, using the Contact Form 7 number from the shortcode:


add_action( 'wp_footer', 'mycustom_wp_footer' );
function mycustom_wp_footer() {
?>
<script type="text/javascript">
document.addEventListener( 'wpcf7mailsent', function( event ) {
 if ( '1234' == event.detail.contactFormId ) {
 location = 'http://www.mydomain.com/thank-you';
 }
}, false );
</script>
<?php } 


In the above example '1234' is replaced by the Contact Form 7 shortcode form number and the location url is for your own "Thank You" page.

Leave a Comment