scriptygoddess

08 Nov, 2008

Put comments on a separate page (in wordpress)

Posted by: Jennifer In: WordPress|WordPress Hacks|WordPress Plugins|WordPress scripts

I've had a few clients ask for this, and I saw some requests to do this on the forums, but no one ever that I found came up with a solution. Here's the scenario, you have your blog posts, and maybe they're long, and rather than make the page *even longer* by dumping the comments on the same page at the bottom, you want a special, separate page for the comments for each post.

Now the trick I've done to accomplish this, I will warn you worked in the scenario it was designed in – (I had "permalinks" turned on. I'm not sure how this would work, if at all, if they weren't turned on, or if the permalink configuration was different) I'm sure there are probably other setups where this would not work. So, that's my disclaimer.

Basically what we're going to do is use the query string to determine whether or not to display comments or the post on the page. It's not terribly "pretty" – and maybe a htaccess expert can tell us how to accomplish the same task by adding a line or two in there to keep the URLs "pretty" – but in the meantime, I'm just happy to have it work at all!

So first – surround anything you would want only on the POST version of the page with this:
*UPDATED TO ADD: This problem/question has come up a number of times. These changes will probably go on your single.php template file (because thats the page that normally dispalys the comments.) If for some reason you don't have a single.php file, then your "single-post view" is probably being derived from the default single.php template file (in the default theme directory) – so it's probably safe to grab a copy of that one and make your changes to it, and then upload it to your theme's directory)

<?php if (!isset($_GET['show'])) { ?>
POST PAGE ONLY STUFF GOES HERE...
<?php } ?>

*ALSO UPDATED TO ADD:The code above should be WITHIN the "WP LOOP".

Inside there you will want to create a link that will bring the user to the "comment page" for the post. You do that like this:

<a href="?show=comments">Click here to view/write comments</a>

Then you wrap your call to the comments template like so:

<?php if (isset($_GET['show']) && $_GET['show'] == "comments") { ?>
<?php comments_template(); ?>
<?php } ?>

*UPDATED TO ADD: Take a look at where the comments_template() line normally goes on the default theme, if you're using a custom theme. You want to make sure you really only put that around the comments_template() line… not large sections of your single.php template file…

Now, ON your comments template page (comments.php), you will need to modify the comments form so that after a comment gets submitted, you end up back on the comment version of the page. To do that you need to grab the current URL of the page you're looking at – and put it into a hidden field (named "redirect_to") in the comment form. To get PHP to pull in the full URL, I grabbed this function from webcheatsheet.com:

function curPageURL() {
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}

To make that available throughout, I created a "plugin" file, and just threw that function into it. (obviously have to activate the plugin before you can start using that function in your theme). (There's probably another way to "add a function" like that, but this worked for me) If you don't already have one, create a "functions.php" file for your theme and add that function in there (surrounded by <?php and ?> tags obviously)

So then before the </form> of the comment form I added this:

<?php if (function_exists('curPageURL')) { ?>
<input type="hidden" name="redirect_to" value="<?php echo curPageURL() ?>" />
<?php } ?>

That's it. Hope that helps someone else. :)

Updated:
Here are the modifications applied to the default wordpress theme. I've made comments to the changes I added in so you can see what I did and then apply it as appropriate to your own theme. In the zip are JUST the files I modified – not the whole wordpress default theme – so single.php, index.php, functions.php and comments.php.

35 Responses to "Put comments on a separate page (in wordpress)"

1 | Steve

November 14th, 2008 at 6:45 pm

Avatar

thanks

you should be able to make this function accessible in all places without needing a plugin by adding it to the themes functions.php file, or creating one if a theme doesn't already have one.

2 | Jennifer

November 14th, 2008 at 8:50 pm

Avatar

AHA! That's what I was looking for. I totally forgot about adding a functions.php to the theme. :) Thank you.

3 | Steve

November 14th, 2008 at 9:57 pm

Avatar

Hi – you are welcome. I've learned some good tips from some of your posts. Glad to be able to help out.

4 | nikki

November 18th, 2008 at 3:29 pm

Avatar

hi – thank you for the tips – i set it up as described and "Click to view/write comments" takes me to main page listing and i cannot view comments…any suggestions?

5 | Jennifer

November 18th, 2008 at 7:34 pm

Avatar

Emailed Nikki on the side. The issue was that she was not using permalinks. So that means we need to make sure the post ID is included in the URL on the page where we're viewing comments only. So this line…

<a href="?show=comments">Click here to view/write comments</a>

needs to be the following in that setup:

<a href="/?p=<? echo $_GET['p']; ?>&show=comments">Click here to view/write comments</a>

6 | Andrew Jaswa

November 29th, 2008 at 4:58 pm

Avatar

Ahh just what I was looking for.

If you are really looking for tighter integration with WP you could edit function get_comments_link() in the comment-template.php file in /wp-includes with the following:

function get_comments_link() {
// Old
// return get_permalink() . '#comments';
// New for permalinks turned OFF
// return get_permalink() . '&show=comments' . '#comments';
// New for permalinks turned ON
// return get_permalink() . '?show=comments' . '#comments';

}

This makes it easy to just use the built in functions provided by WP.

If you use comments_popup_link() you may need to edit that further (for use with popup comments) though I haven't looked into that much.

7 | nikki

December 11th, 2008 at 1:17 pm

Avatar

Hi Jennifer…it has been a while since I tested posting comments, I just did so today, and when I clicks submit, it brings me to a blank page. I can get back fine and view the comment I posted fine, but it was not doing this blank page business when I first installed the plugin. Other things have been edited on the site, but not anything having to do with the comments area.
Any clues or suggestions?

8 | Jennifer

December 18th, 2008 at 7:39 pm

Avatar

Just a note – Nikki's issue was resolved. (She was missing the code for the redirect_to hidden field)

9 | d_kc

January 8th, 2009 at 8:25 pm

Avatar

This is great.

I got it working. Comments are displayed on ?show=comments just fine, but when I try to post a comment the page loads blank at http://www.domain.com/wp-comments-post.php ….the comment doesn't get posted.

I did everything you described and i'm using pretty permalinks – /%category%/%postname%/ on WordPress 2.7

10 | Jennifer

January 8th, 2009 at 11:21 pm

Avatar

@d_kc – email me a link and I'll try to help out: scripty @ (this domain)

11 | Richard

January 21st, 2009 at 5:54 am

Avatar

One thing I really want to do is show a list of comments from one category or selected categories ONLY, not just comments from a single post. Any chance of a snippet to show that? This seems to be a useful way of focusing on several discussions on a category topic.Many thanks.

12 | Mark

February 4th, 2009 at 3:50 pm

Avatar

WHAT? Imagine I have not a clue about funtions, ftp, coding, java script. Anyone got a step by step guide on how to do this so I got a comments page? Maybe with screen shots. I would be eternally grateful to you guys.

13 | Jennifer

February 5th, 2009 at 11:09 am

Avatar

@mark
If I can free up some time, I'll upload some sample files – I can take the "default" theme that comes with WordPress – and modify it with these changes – hopefully that will allow you to see what needs to be done. It's hard to be more specific than that because themes can vary so much. But at least modifying the default one, that should help…?

14 | Jennifer

February 5th, 2009 at 11:50 am

Avatar

@mark – zip is linked to from the post. Hope that helps clear up any confusion…

15 | Ariah Fine

March 3rd, 2009 at 8:08 am

Avatar

So, I’m wondering if it’s possible to put just the comment form on a separate page in addition to the standard comments on each post.
specifically, I want to create a top frame on external links that would allow a reader to comment on my original post while viewing an external link.
Like Facebook has on posted items.

I found this script to do the frame thing, now I just need to know how to create a comment form that will submit the content to the post that the browser was just previously on.
Makes sense?

16 | Mark

March 3rd, 2009 at 10:40 am

Avatar

@Jennifer
Okay. That would help I think. I dont see a zip though.

17 | Jennifer

March 3rd, 2009 at 10:58 am

Avatar

http://www.scriptygoddess.com/downloads/scriptygoddess_comments_sep_page.zip
(The link is shown in the "updated" paragraph at the bottom of the post) 😀

18 | Jennifer

March 3rd, 2009 at 10:59 am

Avatar

@Ariah – totally swamped at the moment – but if I can, I'll cycle back here in a few days or a week or so to see if I understand and can help you with what you need…

19 | Jacob

March 27th, 2009 at 3:38 pm

Avatar

Thank you so very much for this. We're in the process of revamping our website (specifically we're trying to add more functionality and interactivity), but we were hesitant to add comments directly our articles for fear of them bogging down the article content. This is exactly what I was looking for. Thank you so much.

20 | weston

June 1st, 2009 at 4:05 pm

Avatar

Thanks for this tip!

I was having problems all day with this but found out that needs to be in the loop!

DUH!

Everything else worked great, i changed it so i didn't need the function

<input type="hidden" name="redirect_to" value="?show=comments" />

21 | Amit

June 11th, 2009 at 2:27 am

Avatar

Thanks a lot for this useful post. You saved me a lot of work :) … 5 stars!

22 | scriptygoddess » Put comments on a separate page (in wordpress) | WpMash - WordPress News

August 31st, 2009 at 2:02 pm

Avatar

[…] See the rest here: scriptygoddess » Put comments on a separate page (in wordpress) […]

23 | John

January 25th, 2010 at 3:05 am

Avatar

Hi there. Iwas having problems all day with this but found out that needs to be in the loop!

DUH!

Thanks, John.

24 | Kelly

April 3rd, 2010 at 6:47 pm

Avatar

Hi Jennifer,
I've been trying to use your tutorial and I cannot figure out where we've gone wrong. We've studied your theme file examples and followed exactly. If you can, please take a peek and give us a clue where we've gone wrong.

The comment form does not show up at all. And the link to comments page delivers a blank/broken page.

I'll probably paste the old comments form below soon since I hate to leave it inoperable too long. If you let me know when, I'll be glad to take that back out in case you cannot tell. Thank you – we've searched high and low & you are the only one doing just what we need to do!

25 | Jennifer

April 3rd, 2010 at 9:41 pm

Avatar

(Just an update on Kelly's issue – she had the code to show the comment INSIDE the IF statement on whether or not to show the post… so when the post was hidden – so would any code to display the comment form…)
😀

26 | Kelly

April 4th, 2010 at 7:43 pm

Avatar

Thank you so much Jennifer. This is a creative idea and you showed me how to make it work to just what I needed. 😀

27 | Benoist

June 26th, 2010 at 10:07 pm

Avatar

Sorry bad english, it's not my native language.

Very Very interessing hack, i have try it and work well. But i see that comments pages are not indexed by google… because (perhaps) there is the same url canonical… I have see your plugin work on 5 websites and no comments pasges indexed for all…

Have you a solution for indexing them ? and Possible rewrite "http://www.website.com/permalink/?show=comments" to http://www.website.com/permalink/comments/ ?

Thanks

28 | hellbent86

July 10th, 2010 at 12:19 pm

Avatar

Hello! Can you advice me how to display comments list without the comments template (name, email fields, signup button)
Thank you!

29 | konteyner

June 6th, 2011 at 11:48 am

Avatar

Very Very interessing hack, i have try it and work well. But i see that comments pages are not indexed by google… because (perhaps) there is the

30 | Jennifer

June 6th, 2011 at 12:43 pm

Avatar

I think Google should still index them, but unless the link is crawled on each page – google may not get them right away. You might need to add the URLs specifically to your sitemap.xml to specifically request they be crawled. If you're using a plugin to create your sitemap.xml file, then it would need some hacking to get it to list them…

31 | uplink

October 10th, 2011 at 11:46 am

Avatar

Any Possible to rewrite "http://www.website.com/permalink/?show=comments" to http://www.website.com/permalink/comments/ ?

Please help with that!
thanks!

32 | Curt

December 30th, 2011 at 2:13 am

Avatar

Pretty cool. But having major problems with WP 3.3 and my custom layout. Anyway to do this without the
?show=comments
Either that or can someone show me the way to editing the pagination?

33 | Curt

January 9th, 2012 at 6:21 pm

Avatar

If anyone comes across this with similar issues I was able to sort out the pagination issues painlessly with easyCommentsPaginate from http://www.mushtitude.com/

Thanks for the great tutorial!

34 | Phil Owen

February 17th, 2012 at 3:49 pm

Avatar

I'm writing my first custom theme (partly as a means of learning PHP), and this tutorial worked spectacularly for me.

The only challenge I've come across is that the url's for particular comments (i.e. myurl/mypage/#comment-13) won't work. Since the specific comment url isn't "/?show=comments", it just goes to the content page when clicked.

Do you have a fix for this?

Thanks!

35 | Curt

February 19th, 2012 at 10:36 am

Avatar

@Phil Owen
I changed the actual link to the comments to just be myurl/mypage/?show=comments and removed the specific comment id

It does involve changing a core file wp-comments-post.php

At the bottom line 100 or so I changed:

$location = empty($_POST['redirect_to']) ? get_comment_link($comment_id) : $_POST['redirect_to'] . '#comment-' . $comment_id;

to

$location = empty($_POST['redirect_to']) ? get_comment_link($comment_id) : $_POST['redirect_to'];

Normally don't like to change core files but it's a simple fix after updates.

Featured Sponsors

Genesis Framework for WordPress

Advertise Here


  • Scott: Just moved changed the site URL as WP's installed in a subfolder. Cookie clearance worked for me. Thanks!
  • Stephen Lareau: Hi great blog thanks. Just thought I would add that it helps to put target = like this:1-800-555-1212 and
  • Cord Blomquist: Jennifer, you may want to check out tp2wp.com, a new service my company just launched that converts TypePad and Movable Type export files into WordPre

About


Advertisements