How To: Change #comment-45259 to #comment-12

One of the things that has been worrying me for long the link to the individual comments in WordPress. The default structure of a link to a comment is :

http://POST-PERMALINK/#comment-COMMENT-ID

This is what it remains like on most of the WordPress blogs. It wouldn’t look bad if you just has a few hundred blogs. But, the latest comment on the latest post (as of now) on Problogger.net has the ID 4452651.

So, I just wanted to change this thing. I wanted to has #comment-1 for the first comment and #comment-7 for the 7th comment and so on. I searched the web for a hack or a plugin, but in vain. (Let me know if one is available). So, I just figured out my own method, which I’m going to teach you. I made it as a hack for a theme, rather than a plugin because it needs you to edit your theme templates. But, as per the principles, you need not touch WordPress’s core files.

Step 1 : Adding the 1, 2, 3 Increment

Open your comments.php file and search for the following :

<?php foreach ($comments as $comment) : ?>

And, change it to as follows :

<?php $i = 1; foreach ($comments as $comment) : ?>

Now, search for the immediate next occurrence of this :

<?php endforeach; /* end for each comment */ ?>

And, change it to this :

<?php $i++; endforeach; /* end for each comment */ ?>

Step 2 : Modifying the Comment’s DIV

Between the above two lines that you modified, resides the code that outputs each comment. So, you’ll find the parent DIV tag. We need to modify its ID. So, find this :

id="comment-<?php comment_ID(); ?>"

And change it to :

id="comment-<?php echo $i; ?>"

Do the same and replace all occurrences of <?php comment_ID(); ?> with <?php echo $i; ?>.

We are almost done now. That is, at this point, if you point your browser to http://POST-URL/#comment-4, it will lead to the 4th comment. But, that is not all. When, somebody makes a comment, he’ll be redirected to the original URL http://POST-NAME/#comment-37456. We’ll tackle that in the next step.

Step 3 : Redirecting to the New URL after Commenting

Scroll to the bottom of your comments.php file and find this line :

<?php do_action('comment_form', $post->ID); ?>

Add the following line after the above line :

<?php gc_comm_id($i); ?>

This line calls the function that we will be defining. It passes our i as the argument. That is, if there are currently 7 comments, i will hold the value 8 for the new comment being posted.

Next, open your functions.php. Create one if it doesn’t exist. Add the following lines to the bottom of the file :

<?php
  function gc_comm_id ($i)
  {
    echo '<input type="hidden" name="gc_comm_id" value="'.$i.'" />';
  } 

  function gc_comm_id_process ($location)
  {
    $gc_comm_id = $_POST['gc_comm_id'];
    $id = strrchr($location, "-");
    return str_replace($id, "-".$gc_comm_id, $location);
  }
  add_filter('comment_post_redirect', 'gc_comm_id_process');
?>

I’ll explain the code to you now. The first function gc_comm_id does nothing but output an invisible field that holds the value of i to the comment form.

The second function, gets the actual redirect URL and then alters it into our new redirect URL containing the value of i. You’ll understand that code if you know a bit of PHP. If you don’t know, you don’t have to bother with it. Just know what it does and that is enough.

Conclusion :

This is just the way that I’ve done it. As usual, if you know a simpler method, do let me know. Also, let me know if there is any plugin available that does it.

56 Responses

Great idea. Thank you for great trick. I ‘ll try with my blog.

That should be pretty useful. I need to learn PHP beyond the very little I do know so that I can come up with useful code like this.

Nice post.

Glad that you found it useful, Yuuliana & George :)

You’re really a pro. For a starter like me, it’s hard to do it correctly. But the way you explained it, I easily understood and what drives me to start doing it? Your sincerity in sharing your knowledge.

Wow, anybody can do this with such great explanations and pictures and everything. When the time comes I will know just what to do. Thanks for posting this.

Glad you liked it :)

Great tip how long did it take you to figure it out? Nice way to post

Great idea. Thank you !

Wow, you are pretty good with the PHP. I have a couple of WordPress blogs and I know this would be great on them.

Mate seems to me that you have good skill in php. From where do you learn this. In India which institute is better to learn this language please let me know.

I am digging this post for you and will look for more posts of yours as well.

Great tip, thanks for the advice. keep it up

Excellent tip. I going to try this as well.

So.. that’s how you do it! good to know..
I built my own blog, and am always looking for tips in php

Cheers

Great tip, I will try on my blog. Thanks for sharking, keep up the good work :)

Thanks for the great tip!

your article is very detail. Thank’s friend’s

Rob

Thanks for the post…very helpful

Nice Information. Thanks

Very nice info there. While the fact remains that I would not want to try this on any of the blogs established I currently run, I will certainly try this on one of the newer blogs that are gaining popularity…

What a great article. I just stumbled upon this site via google and it’s great. I will bookmark it.

Great. You are really clever about this subject!

so if we can cheat the comment it will be fun hihihi

Wow, what a nice feature. You are really skilled. Thank you very much.

I am just developing my php skills – thank you for sharing your thoughts on these features and find these really helpful,

I will use that on my blog, thanks! You are doing a great job on your blog.

Good thanks for this article

was looking for that ……thanks

I’ve been looking around as well without luck. I am not much for coding but the steps you posted were extremely helpful and easily followed! Thanks so much

Hi

Thanks for sharing stunned insights, keep it up.

Thanks you are really good at this. I would use or recommend you. Thanks for the tip

I would recommend using { and } rather than the : construct to keep the code clear.

Great job. I’ll make sure to read more of your articles.

Wow nice tips. thank you for this use full tips.

Wow, I never thought about comments permalinks before. I think my highest number on one blog is a little over 1000 – but I can definitely see how after a few years it could get to be quite long! Great hack I will have to keep in mind!

Nice, thats is a really good idea, i think it could make things a lot nicer and cleaner in the long run. good tip.

Nice, thanks for posting this I have been looking around for something like this.

Nick

That was a very useful php code

thanks for your tips, that i really work

Wow great tips, thank you for you information

Explained very well, thanks for the great tip and advice

exciting and communicative, but would make something more on this topic?

Its really good tip. I’ll try for it.

This is useful infomration, thank you.

Can you tell me who did your layout? I have been looking for one kind of like yours. Thank you.

That’s a nice idea that needs to be tried out.

Hey man. nothing new from you in some time. The latest download tip and now this comment tip are good.. keep them coming :)

Great post, i love your idea, it just makes sense, incrementing posts is a no brainer. thanks for the know how to set it in motion.

Thank you for great idae. I ‘ll try with my blog.

great tips. thanks so much. it is very useful