Redirect to a Thank-You Page After Contact Form 7
Sends visitors to a thank-you page after a Contact Form 7 form is sent successfully, with a different page per form if you like. Handy for conversion tracking.
Opens WPIDE on
Code
// Form ID => thank-you URL. "default" applies to all other forms.
$wpide_cf7_redirects = array(
'default' => '/thank-you/',
// 123 => '/thanks-for-your-quote-request/',
);
add_action( 'wp_footer', function () use ( $wpide_cf7_redirects ) {
if ( ! wp_script_is( 'contact-form-7', 'enqueued' ) ) {
return;
}
$map = array();
foreach ( $wpide_cf7_redirects as $id => $url ) {
if ( $url ) {
$map[ (string) $id ] = esc_url_raw( $url );
}
}
?>
<script>
document.addEventListener( 'wpcf7mailsent', function ( event ) {
var map = <?php echo wp_json_encode( $map ); ?>;
var id = String( event.detail.contactFormId );
var url = map[ id ] || map[ 'default' ];
if ( url ) { window.location.href = url; }
}, false );
</script>
<?php
} );
Notes
Map form IDs (from the form shortcode) to page URLs; the "default" entry covers every other form, so leave it empty to redirect only the forms you list.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.