# Redirect to a Thank-You Page After Commenting

> Sends first-time commenters to a page of your choice after they submit a comment, instead of back to the post.

- Type: PHP
- Runs: Run everywhere
- Category: [Comments](https://wpide.com/library/category/comments/)
- Tags: comments, redirect, engagement
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/comment-thank-you-redirect/

## Code

```php
add_filter( 'comment_post_redirect', function ( $location, $comment ) {
    $page = get_page_by_path( 'thank-you' );

    if ( ! $page || ! $comment instanceof WP_Comment ) {
        return $location;
    }

    $previous = get_comments( array(
        'author_email' => $comment->comment_author_email,
        'count'        => true,
        'status'       => 'all',
    ) );

    return (int) $previous <= 1 ? get_permalink( $page ) : $location;
}, 10, 2 );
```

## Notes

Create a page with the slug thank-you, or change the slug in the code.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Redirect to a Thank-You Page After Commenting" 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/
