The comment_post_redirect WordPress PHP filter is used to modify the location URI where the commenter is redirected after submitting a comment.
Usage
add_filter('comment_post_redirect', 'my_custom_redirect', 10, 2);
function my_custom_redirect($location, $comment) {
// your custom code here
return $location;
}
Parameters
$location(string) – The ‘redirect_to’ URI sent via $_POST.$comment(WP_Comment) – Comment object.
More information
See WordPress Developer Resources: comment_post_redirect
Examples
Redirect to a custom thank you page
Redirect the commenter to a custom thank you page after submitting a comment.
add_filter('comment_post_redirect', 'custom_thank_you_page', 10, 2);
function custom_thank_you_page($location, $comment) {
return 'https://example.com/thank-you/';
}
Redirect back to the comment form
Redirect the commenter back to the comment form after submitting a comment.
add_filter('comment_post_redirect', 'redirect_to_comment_form', 10, 2);
function redirect_to_comment_form($location, $comment) {
return get_permalink($comment->comment_post_ID) . '#respond';
}
Redirect to a specific post
Redirect the commenter to a specific post after submitting a comment.
add_filter('comment_post_redirect', 'redirect_to_specific_post', 10, 2);
function redirect_to_specific_post($location, $comment) {
return get_permalink(123); // Replace 123 with your post ID
}
Redirect based on comment author email
Redirect the commenter to a specific page based on the author’s email.
add_filter('comment_post_redirect', 'redirect_based_on_author_email', 10, 2);
function redirect_based_on_author_email($location, $comment) {
if ('[email protected]' === $comment->comment_author_email) {
return 'https://example.com/special-page/';
}
return $location;
}
Redirect based on comment status
Redirect the commenter to different pages based on the comment’s approval status.
add_filter('comment_post_redirect', 'redirect_based_on_comment_status', 10, 2);
function redirect_based_on_comment_status($location, $comment) {
if ('0' === $comment->comment_approved) {
return 'https://example.com/awaiting-moderation/';
} else {
return 'https://example.com/comment-approved/';
}
}