# 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.

- Type: PHP
- Runs: Frontend only
- Category: [Forms](https://wpide.com/library/category/forms/)
- Tags: contact-form-7, redirect, thank-you
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/cf7-redirect-after-submit/

## Code

```php
// 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 use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Redirect to a Thank-You Page After Contact Form 7" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
