The WordPress hook comment_post_redirect is used to modify the URL that a user is redirected to after submitting a comment on a post. By default, WordPress redirects the user to the same page where the comment was submitted. However, if you want to redirect the user to a different page or URL after they leave a comment, you can use this hook to modify the redirection URL.
This hook is particularly useful for developers who want to customize the user experience after a comment is submitted. For example, you might want to redirect users to a custom thank you page or offer them a discount code for leaving a comment.
Here’s an example usage code for the comment_post_redirect hook:
function custom_comment_redirect($location, $comment) {
$location = 'https://www.example.com/thank-you-for-commenting/';
return $location;
}
add_filter( 'comment_post_redirect', 'custom_comment_redirect', 10, 2 );
In this example, we have created a custom function called custom_comment_redirect that overrides the default redirection URL. The function takes two parameters – the current redirection URL and the comment object. We have set the redirection URL to a custom thank you page using the $location variable. Finally, we have used the add_filter function to attach our custom_comment_redirect function to the comment_post_redirect hook.
As a result, whenever a user submits a comment on a post, they will be redirected to the custom thank you page instead of the default page.