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?

🔥 HOT: Interested in boosting your WordPress SEO? My Slim SEO plugin is a super lighweight and automated plugin that handles most the hard work for you: meta tags, sitemap, redirection, schema & link building.

👉 Have a look and you will love it: wpslimseo.com

Leave a Reply

Your email address will not be published. Required fields are marked *

Copyright © 2024 Deluxe Blog Tips. All rights reserved.

My products: Meta Box | Slim SEO