WordPress: Redirect to Thank you page after commenting

Redirecting visitors to a Thank you page after they commented on posts is a good way to communicate with them. In the Thank you page, you can inform that their comments have been queued (for moderation) and say that we’re very glad they joined discussion or something like that.

In WordPress, there are 2 ways to do that:

  • using hidden field in comment form
  • using action hook

Before going into details, remember that you’ve already created a Thank you page in your blog. It’s just a normal page like other WordPress pages. Go to your WordPress Dashboard → Pages → Add New, and write some thing interesting there. After that, remember its URL, for example: (I will use this URL below as you’ll see).

Using hidden field in comment form

Using hidden field in comment form is very easy. Open your comments.php file in the theme folder (/wp-content/themes/theme-name), you’ll find something looks like:

<form action="<?php echo
get_option('siteurl'); ?>/wp-comments-post.php" id="commentform" method="post">

All you need to do is adding this line below:

<input name="redirect_to" type="hidden" value="http://domain.com/thank-you" />

As you see the URL of Thank you page is used as the value of the hidden field.

Using action hook

Hooks are provided by WordPress to allow your plugin to ‘hook into’ the rest of WordPress; that is, to call functions in your plugin at specific times, and thereby set your plugin in motion. For more information about hooks, you can visit the Plugin API page at WordPress Codex.

In our situation, we’ll use the comment_post_redirect action hook. Open your functions.php file in theme folder and insert these lines:

// Redirect to thank you post after comment
add_action('comment_post_redirect', 'redirect_to_thank_page');

function redirect_to_thank_page() {
    return 'http://domain.com/thank-you';
}

That’s all. It is simple and easy, isn’t it?

24 thoughts on “WordPress: Redirect to Thank you page after commenting

  1. Hi this looks really useful, thanks.

    Please can you tell me if it’s possible to add in a html page, that does NOT have the wordpress framework template, for a thank you page?? And if so, how? πŸ™‚

    Thanks a lot! Clare πŸ™‚

    1. You don’t have to use WP page. Instead of that, you can simply change the URL I meant in the post () to URL of your custom html page. That’s all.

  2. Hello,

    It was really useful for me, but I would like to add a “Go Back to the Post” i.e to the post from where the User landed on the thank you page. Any idea for that ?

    Thank you πŸ™‚

    1. Hi Harikrishna,

      There is already a big Back button on the browser; and most of people know how to go back to the previous page by clicking on this Back button. So, I think it’s not necessary to have a “Go Back to the Post” link.

    2. Hello Chan Huu,

      Thank you for the reply πŸ™‚ Well, I managed to add a back to the post link (http://epicmagazine.net/thank-you-for-the-comment/). I had added it due to the request of my visitors. I would suggest you to add it too. The href=”onclick=”location.href = document.referrer;” which will allow the user to go back πŸ˜‰

      Harikrishna (Epic Magazine)

    3. Hi Harikrishna,

      Thanks for your simple but effective trick πŸ˜€ I’ll add it to my blogs as soon as possible πŸ™‚ Nice day πŸ˜‰

  3. Hi rilwis,

    Today I found a plugin that redirect readers to other pages after they’ve already commented; it’s Comment Redirect by Yoast. So, people who don’t want to touch any codes can use this plugin instead of your code snippets.

    Thanks for reading πŸ˜‰

  4. Hey Chan Huu,

    Awesome resource and I’d love to make it work for my site. However, I have now tried both options and cannot get it to work.

    I’ve also tried two separate plugins and neither does the trick.

    I’m using Jetpack plugin, along with its commenting system, so maybe I have to mess with the Jetpack files instead of Theme files?

    If you have any ideas I’d love to hear them!

    Thanks,
    Daniel

Comments are closed.

Newsletter

Subscribe to our newsletter to receive news and tutorials about WordPress and web development.

Want to get away from Pipdig drama? See 30+ Pipdig blog themes alternatives See here β†’