Thank you page for WordPress comment

Last updated on February 13, 2010. Tags:

The default setting of WordPress is that after you posted a comment, the page (or should I say "post") will reload itself with your comment already posted. If the comment is moderated, you can still see it with a message like "your comment is awating moderation".

Now, what if you want to redirect your visitors to a page that says "Thank you" to your visitors. Perhaps, you want to use the comment form as an online message form or as an order form instead of being a blog comment form. There's a way to do it but it involves not just modifing the theme's PHP files; you also need to modify one of the core WordPress PHP files.

I experiment on this method using WordPress 2.7.1 and WordPress 2.9.1. It may or may not work in nearby versions, but it  will certainly work in these two versions and most likely all the versions between them. I also tried this on WordPress 2.6.1 but it didn't work.

Phase 1: Creating the "thank you" page

It is very easy to create the "thank you" page itself. All you need to do is to go to login to your dashboard and create it just like how you create any WordPress page. After you opened the interface that will allow you to create a new page, follow the steps below:

  1. Give it a title like "Thank you" or "Thank you very much" or any generic short phrase that usually appears in bigger letters in a typical "Thank you" page.
  2. Type the longer phrase in the main content form, something like "Thank you for sending us a message blah blah blah. We will reply to you ASAP."
  3. Provide a "back to previous page" link by editing your "Thank you" page in HTML mode and inserting the following code below the thank you message. You can replace the text "Return to the post" by another text or by an image just like any link.
  4. <a onmouseover="this.style.cursor='pointer'" onclick="history.go(-1);return true;refresh();">Return to the post</a>
  5. Uncheck the "allow comments" and "allow trackbacks and pingbacks".
  6. Publish it.
  7. Take note of its URL and post ID; copy them in Notepad.

I would like to point out that the "back to previous page" code works in a way that when you return to the previous page or post, the comment does not appear even if the blog allows all comments to be posted. The user needs to refresh the post page first before seeing the comment. I still need to find a way to automatically refresh a page upon returning to it, but for certain purposes the code we have now is sufficient.

After you've done the steps above, you will notice that you have one problem. The "thank you" page just appeared in the page list menu. We will deal with this problem later in Phase 3. For now, we need to setup WordPress so that the visitor will be redirected to the "thank you" page after posting a comment.

Phase 2: Redirecting the visitor who posted a comment

After creating the "thank you" page, open the your theme editor and open the theme file functions.php. Insert the following code:

add_filter('comment_post_redirect', 'thank_you_redirect');
function thank_you_redirect($location)
{
return '/thank-you/';
}

The code above is valid if the URL of your "thank you" page is http://www.domain.tld/thank-you/ (i.e., your permalink structure was set to /%postname%/). By this time, you should know the URL of your "thank you" page if you followed step 6 of phase 1.

If you are using the default WordPress URL and the post ID of your "thank you" page is 1 (hence, its URL is like http://www.domain.tld/?p=12), the fourth line of the PHP code will be like this:

return '/?p=1';

If your WordPress-powered website or blog is in a subdirectory (e.g., http://www.domain.tld/wordpressblog/), you need to include the subdirectory in the code even though the WordPress is installed in that subdirectory:

return '/wordpressblog/thank-you/';

If you find the directory structure confusing, you may use full URL that ignores directory structure such as:

return 'http://www.domain.tld/thank-you/';

Phase 3: Removing the "thank you" page to page list menu

Once you've created the "thank you" page, it will be automatically added to the list of all pages in your navigation menu. We do not want this page to appear in the page list menu because first, it looks awkward and second, there's absolutely no reason for your visitors to view this page without them doing anything you want them to do (e.g., leaving a message, placing an order). It's like saying "thank you" to a person who didn't do any favor for you.

The navigation menu that list all pages is generated by the PHP code below. In most WordPress themes, it can be found either at header.php or sidebar.php.

<?php wp_list_pages('title_li'); ?>

Replace the page list menu PHP code above with the one below.

<?php wp_list_pages('exclude=1&title_li'); ?>

In the above example, we assume that the post ID of the "thank you" page is 1. Simply replace 1 with the actual post ID of your "thank you" page to make the code work for your requirements. More details on how to remove or hide pages from the navigation menu can be found in this this tutorial: Removing a static page from page list

Miscellaneous: Removing the "thank you" page from search results

It is possible, although highly unlikely, for your "thank you" page to appear in the search results of either the search engines and the built-in search function of WordPress. To eliminate this possibility, please refer to the tutorials below:

Author's Note: This tutorial is actually an old post dated January 25, 2009 but it went to a major revision so I'm republishing it as a new post. The overhaul was done on Phase 2 while Phase 1 and Phase 3 remained untouched. Prior to this revision, Phase 2 discussed modifying a core WordPress file wp-comments-post.php to make the comment's thank-you page work. This can be problematic because:

  1. A little mistake in any of the WordPress core files might totally ruin your website or blog
  2. When the webmaster upgraded to a newer version of WordPress, the changes made in the core file will be overwritten and the comment's thank-you page will no longer work.

Hence, I look for a solution that will create the same effect, but can be implemented to the theme files instead of the WordPress core files, which I presented now in the revised Phase 2 of this tutorial.

Posted by Greten on January 30, 2010 under WordPress tweaks

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google
  • StumbleUpon
  • Technorati

Related Posts

You might also be interested (randomly generated):

Read Comments

  1. Posted by Steve Patterson on 04.24.10 2:11 am

    Thanks this was helpful. Creation of a plug-in may help with that, when you update the thank-you page going away.

  2. Posted by Piseth on 05.19.10 3:10 am

    Thank you for the thank you code

Post Comments

Please double check your comment before clicking the "Post" button. Once you clicked it, there will be no way for you to edit your comment.





* Required. Your email will never be displayed in public.