How To Redirect First Time Commenters in WordPress Without a Plugin

In this tutorial, I will show you two separate codes on how to Redirect all your WordPress Commenters to a Thank You page, and also how to only Redirect your First Time Commenters to a Thank You Page without the need of a plugin.

This tutorial is for you, if you are asking one of these 2 questions:

  1. How can I get ALL my WordPress commenters to redirect to a fancy “Thank You” page.
  2. How can I ONLY get my FIRST WordPress commenters to redirect to a fancy “Thank You” page.

Here is How:

1. Redirect ALL Comments | functions.php

If you want ALL your commenters to redirect to a “Thank You” page, after they post a comment. Then, open the functions.php file located in your theme´s folder, and add (copy and paste) the following CODE-1. Save the file and upload it to the server.

CODE-1:

<?php
/************************************CODE-1*******************************
* @Author: Boutros AbiChedid 
* @Date:  May 14, 2012
* @Websites: bacsoftwareconsulting.com/ ; blueoliveonline.com/
* @Description: Redirect to a 'Thank You' page after any Post/Page Comment.
* @Tested on: WordPress version 3.3.2 
**************************************************************************/
function redirect_after_any_comment(){
	//If you can't figure out the relative path. You can always use absolute path.
    wp_redirect('thank-you'); /* MODIFY the Path Accordingly. */
    exit();
}
//Hooks the 'redirect_after_any_comment' function to the 'comment_post_redirect' filter action.
add_filter('comment_post_redirect', 'redirect_after_any_comment');
?>

CODE-1 Notes:

Use CODE-1 to redirect ALL comments on your WordPress Website to a “Thank You” page, once the comment is posted. You can redirect to any other page on your Website if you like.

On line 11 of the code, if you can´t figure out the relative path of the Thank You page, you can always use the absolute path. In general relative paths are better for portability reasons and ease of maintenance. A common mistake here would be writing the wrong file path or filename.

For more information on Absolute Versus Relative Paths see tutorial1 and tutorial2.

CODE-1 works on WordPress 1.5.1 and higher. But I hope that you will upgrade to the latest version.

CODE-1 References:

2. Redirect First Time Comments ONLY | functions.php

If you ONLY want your FIRST commenters on your Blog to redirect to a “Thank You” page, after they post a comment. Then, open functions.php file located in your theme´s folder, and add (copy and paste) the following CODE-2. Save the file and upload it to the server.

CODE-2:

<?php
/************************************CODE-2*******************************
* @Author: Boutros AbiChedid 
* @Date:   May 14, 2012
* @Websites: bacsoftwareconsulting.com/ ; blueoliveonline.com/
* @Description: Redirect to a 'Thank You' page only after the FIRST 
* Post/Page Comment.
* @Tested on: WordPress version 3.3.2 
**************************************************************************/

function redirect_after_first_comment($url, $comment) {
	$comment_count = get_comments( 
						array( 
						'author_email' => $comment->comment_author_email, 	
						'count' => true 
						) 
					);
	//Check if this is the first Comment (based on the Commenter email)
	if ( $comment_count == 1 ) {
		//Redirect URL for first time Commenters. 
		wp_redirect('thank-you'); /* MODIFY the Path Accordingly */
 		exit();
	}
	//Else, return the current post for repeated commenters.
	return $url = get_permalink();
}
//Hook the 'redirect_after_first_comment' function to the 'comment_post_redirect' filter action.
add_filter( 'comment_post_redirect', 'redirect_after_first_comment', 5, 2);
?>

CODE-2 Notes:

You can use either CODE-1 OR CODE-2 in your functions.php file, but NOT both.

Use CODE-2 if you only want to redirect your FIRST commenter to a “Thank You” page, once the comment is posted. You can redirect to any other page on your Website if you like.

CODE-2 compares the Commenter´s email to the list of emails already existing in the Database using the get_comments() function.

Even if your comment is still waiting to be moderated, it is considered your first comment. If your first comment has not been approved, and you decide to comment again using the same email, then you will be redirected to a Thank you page again.

Regardless when you add CODE-2, your previous repeated commenters will NOT be redirected to a “Thank You” page.

On line 21 of the code, if you can´t figure out the relative path of the Thank You page, you can always use the absolute path. In general relative paths are better for portability reasons and ease of maintenance. A common mistake here would be writing the wrong file path or filename.

For more information on Absolute Versus Relative Paths see tutorial1 and tutorial2.

CODE-2 works on WordPress 2.0.0 and higher. But I hope that you will upgrade to the latest version.

CODE-2 References:

How To Build your “Thank You” Page

Login to your WordPress dashboard. In your Administration Screens, and on the Left Panel, go to Pages -> Add New. This will create a new page.

Add your content to the “Thank You” page as you see fit, and and then click Publish.

You also want to exclude the “Thank You” page from showing up in your Menu. To do that, it depends on how your navigation menu is crafted. But, in case you need the page ID and you don´t know how to find it, read my tutorial on
How To Find the Page ID.

Want to Check My “Thank You” Page?

If you didn´t leave a comment on this Blog before, go ahead and comment so you can see what the “Thank You” page looks like.

Your Turn to Talk

In this tutorial, I showed you How to Redirect ALL your Blog Comments to a “Thank You” Page without a plugin. And I also showed you How to only Redirect your FIRST commenters to a “Thank You” page without a plugin.

How easy did you find this tutorial to implement? Do you have something to add or anything else to say? If so, please share your opinion in the Comment section. Your opinion matters, unless it is a Spam.

If you found this post useful, please consider: linking back to it, subscribing by email to future posts, or subscribing to the RSS feed to have new articles delivered to your feed reader, or feel free to donate. Thanks!

About the Author |
Boutros is a professional Drupal & WordPress developer, Web developer, Web designer, Software Engineer and Blogger. He strives for pixel perfect design, clean robust code, and user-friendly interface. If you have a project in mind and like his work, feel free to contact him. Connect with Boutros on Twitter, and LinkedIn.
Visit Boutros AbiChedid Website.

9 Responses to “How To Redirect First Time Commenters in WordPress Without a Plugin”

  1. Deepak says:

    Thanks, For share the tricks. very useful.Will Implement it on my website

  2. Thanks for the lesson. I also made a page “Thanks for the comment,” but probably somewhere was an extra space in funtstion.fp and an error 500. I will once again try to install the code. Currently working on the blog plugin “Comment redirect” :-((

    • You probably have an additional white space after/before opening/closing php tag.
      Or there is additional characters in the code, because sometimes when you copy the code it also copies some ‘trash’ characters with it and you need to remove them.

      General Warning

      When you add several PHP code blocks in your theme´s funtions.php file, make sure that you do NOT leave any white space (spaces, newline) before the opening PHP tag (<?php) or after the closing PHP tag (?>).

      Thanks,
      Boutros.

  3. MangOleh says:

    Thanks, For share the tricks. very usefull.

  4. Sibz Solutions says:

    Nice tutorials! I had the same problem. I followed these tutorials and used them already for one of the sites that I am working on and it worked perfectly. Thanks a ton!

  5. Thanks Boutros, really useful this. I mentioned you in the WP forums – see my post here: wordpress.org/support/topic/redirect-first-comment

    I changed your code slightly, swapping return $url = get_permalink();for return $url = get_comment_link();

    Thanks ever so much, I had a plugin doing this, but prefer to code things myself when possible :-)

    Christopher

    • Thanks Christopher. Glad that you found my tutorial useful. Thanks for mentioning it on WordPress forum.
      I too, like to only add plugins when absolutely essential and hack the theme (or add code to the child theme) in all other cases.

  6. Mike says:

    Another amazing how-to – and in clear, concise, easily understandable language (very helpful for a WordPress / PHP newbie like me!).

    I did notice that if you include the opening and closing php tags in your code segments in functions.php, it causes an error since functions.php already has these. Someone new to php wouldn’t necessarily realize that you need to remove these before adding the code segment.

    • Thanks Mike for your feedback. Truly appreciated.
      About the opening/closing PHP tags: I always add them to the code on purpose, so that my code is independent on its own. If you leave them, then you need to add my code outside your existing opening/closing tags.
      If you add my code within your opening/closing PHP tags, then you should remove the ones in my code.
      In my tutorials, I expect some very basic understanding of WordPress/PHP. I understand the source of confusion and thanks for pointing this out.
      Please read the section below. Thanks.

      General Warning:

      When you add several PHP code blocks in your theme´s funtions.php file, make sure that you do NOT leave any white space (spaces, newline) before the opening PHP tag (<?php) or after the closing PHP tag (?>). The correct way is as follows:

      <?php 
      //Some Code here beetween the opening PHP tag (above) 
      //and the closing PHP tag (below)...
      ?>
      <?php 
      //Some other Code here ...
      ?>
      

      In the above code, if you leave any white space or a newline between lines 4 and 5, you will get the following error: “Warning: Cannot modify header information - headers already sent by…