WordPress redirect to a thank you page or message after commenting

First published on August 3, 2008

By default, after someone leaves a comment on a WordPress blog, they are redirected back to the same page, to the section on that page where their comment has been posted. The after-commenting URL usually looks something like this:

Suppose you use the commenting feature as a “contact me” function on one or more of your pages. In this case, you do not display the comments on the page; therefore, after someone has submitted the so-called comment, by default they are redirected to the same page with no confirmation that their message has been received.

You can customize the redirect in two ways:

  • Use a hidden variable named “redirect_to” in the comment form with the value being the URL to redirect to.
  • Write a plugin that uses the comment_post_redirect hook.

We will focus on the second option, as it is arguably more scalable and powerful (because you can potentially add complex rules and manage this in one place).

In our example, you can use a plugin to append a “thankyou” parameter to the URL, which the post or page template would recognize and display a relevant message:

Thank you message after a comment has been submitted

The plugin component would look something like this:

Plugin Name: Redirect to a thank you message
Plugin URI:
Description: Redirect the user to the same page but display some confirmation that their message has been received
Author: Peter Keung
Version: 0.1
Author URI:

$rty_ids = array();
// List the post or page IDs that should get this treatment
// We suppose here that you want this to apply to the pages with the IDs 23 and 26
$rty_ids[23] = '';
$rty_ids[26] = '';

// Set this to true to affect all posts
$rty_all = false;

function rty_comment_redirect ($location, $comment)
    global $_POST, $rty_ids;

    // Leave out the "redirect_to" condition if you want to ignore what was posted in the form
    if( empty( $_POST['redirect_to'] ) && ( $rty_all || isset( $rty_ids[$comment->comment_post_ID] ) ) )
        if( get_option('permalink_structure') == '' )
            // If your URL looks like "?p=8" then we need an ampersand
            $rty_parameter_syntax = '&';
            $rty_parameter_syntax = '?';
        // Append a "thank you" parameter so the post or page template can display the appropriate message
        // Alternatively, you can put a specific page to redirect to
        // Or, you can check the value of the other properties of the $comment object to do something else
        // The $comment object contains: comment_ID, comment_post_ID, comment_author, comment_author_email, 
        // comment_author_url, comment_author_IP, comment_date, comment_date_gmt, 
        // comment_content, comment_karma, comment_approved (1 for approved; 0 for moderation queue; spam for spam), 
        // comment_agent, comment_type, comment_parent, user_id

        $location = get_permalink($comment->comment_post_ID) . $rty_parameter_syntax . 'thankyou';

        return $location;
    // Use the normal redirect location if our conditions don't match
        return $location;

add_action('comment_post_redirect', 'rty_comment_redirect', 15, 2);

Then, somewhere in your “Single post” or “Page” template is the thank you message that displays when needed:

        <?php if( isset( $_GET['thankyou'] ) ) { ?>
		<div class="thankyou" style="border: 1px solid red; padding: 20px;">
			<p>Thanks for your comment!</p>
        <?php } ?>

If you’re adventurous, you can tweak the plugin to add rules so that different comment forms behave differently — some might display a thank you message on the same page; some might redirect to completely different pages; and the message could be different depending on whether the comment was approved, held for moderation, or marked as spam.


14 Responses to “WordPress redirect to a thank you page or message after commenting”

  1. shahnizz says:

    Awesome !! I tried out number 1 and it works perfectly.. but I have no idea why. I can’t find much info in the wordpress docs for the "redirect_to" trick…

    but I’m not complaining..thanks so much for this..I was looking everywhere for this!!!!

  2. doron says:

    was looking for this kind of script

  3. Peter says:

    Thanks very much for this Peter – I was doing a quick look for anyone that had used the function and this was a great example…

  4. Thierry Clermont says:

    Thank you! :)

    What to do if we want to add this option for all our posts?

    Reply from Peter: In that case, you can remove the isset ( $rty_ids[$comment->comment_post_ID] ) condition in the plugin.

  5. Thierry Clermont says:

    Thank you! It’s working pretty well. :)

  6. Maria says:

    Thank you SO MUCH!
    It works perfect, and it is exactly the solution I needed.

  7. Jon says:

    A bit of a newbie question, but how would you actually "install" this plugin on the site. Would I just modify the initial script above, save as a php file in notepad, and then upload to the plugin directory?

    I understand the basics of the script in addition to the mods to the singlepost page, but I’m confused on how to get it added to the site.

    Reply from Peter: Yup, you’ve got it!

  8. Jon says:

    You’re quick on replies Peter. Awesome.

    I’ve successfully installed via some other tutorials I’ve found…

    Now for the modifications of the singlepost page. I’m using Thesis, which I believe uses hooks instead of actual mods of the singlepost file. The only file I would see that I’d assume needs to be modified is index.php in the thesis themes folder, but it tells me to use hooks.

    Any idea on how this is done, or should I continue Googling around. Just saw your reply so I thought I’d ask…

    Reply from Peter: Unfortunately, I’m quite sporadic on replies these days, so you caught an exception there :) I’ve never dealt with Thesis, so unfortunately (again) I cannot give any guidance on how to conform to its intricacies. Good luck, though, and please share your findings with everybody if you can.

  9. Mohd Isa says:

    hi, i has some question. how to redirect to other site with open on new windows when people leave comment in my blog?

    Reply from Peter: I haven’t considered that. You might find some people on the WordPress support forum who have done something similar

  10. luca says:

    Hi, I am wordpress newbie (I use 3.2.1 version). I managed to create, install and activate the plugin but I have problem with this part of code

    <?php if (isset($_GET['thankyou'])) { ?>
    <div class="thankyou" style="border: 1px solid red; padding: 20px;">
    <p>Thanks for your comment!</p>
    <?php } ?>

    in wordpress there aren’t “Single post” or “Page” .php
    Like in other script I have added this code in "functions.php", at the bottom or inside removing <?php. or removing ?> after { ? but I have always the same error:

    Parse error: syntax error, unexpected ‘<‘ in /membri/revisionando/wp-content/themes/hitman72-1.1/functions.php on line …

    any idea? thank you and sorry for my poor english :-)

    Reply from Peter: In most themes, there is a single.php and a page.php (and more if you have custom post types). They are quite essential templates, so if your theme doesn’t have them, I’d ask the plugin author to add them.

  11. luca says:

    I have made the theme with Artisteer (a theme editor), and in the folder (on pc) I have single.php and page.pho. From wodpress template I can’t find them. With notepad I have added your code in both file (one at a time of course) and tranfer via ftp. but when I add new comment nothing happens (restarting browsers, explorer and firefox, and clearing chace), just add the comment witouth "thank you" message :-(
    esample of my page.php

    <?php get_header(); ?>
    <div class="art-content-layout">
    <div class="art-content-layout-row">
    <div class="art-layout-cell art-content">
    <?php get_sidebar(‘top’); ?>
    if(have_posts()) {

    /* Start the Loop */
    while (have_posts()) {
    get_template_part(‘content’, ‘page’);

    } else {


    <?php get_sidebar(‘bottom’); ?>
    <div class="cleared"></div>
    <div class="art-layout-cell art-sidebar1">
    <?php get_sidebar(‘default’); ?>
    <div class="cleared"></div>
    <div class="cleared"></div>
    <?php get_footer(); ?>

    and here I have added your code. thanks for your kindness

    Reply from Peter: Remember to add the relevant post IDs to the array in the plugin itself (or modify the plugin to be used on all posts). Also, you can test whether the code in the template itself works by appending “?thankyou” to the URL.

  12. luca says:

    I have found the 2 file in the wordpress template, adding code I haven’t php error :-) but nothing append after a new post. as regard "post ID" I do not know what it is… How add "thank you" message after EVERY added message? sorry for my newbie

    Reply from Peter: Please read the top of the plugin file, where it has instructions on that.

  13. Heather Jones says:

    This is exactly what I need for a new affiliate template design that I’m working on. thanks!

  14. Hena says:

    It’s great!!thank you so much

Speak your mind

To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word