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.
Opens WPIDE on
Code
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 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.