Got Chance

how to change comment 45259 to comment 12

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 :


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 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 :

  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

Oh great! Thanks for the heads up! I will definitely put an entry in!

So I don’t understand, why it is so important for you. I think, that it doesn’t matter if it is #comment-12 or #comment-45259.

i don’t it’s important at all…maybe I don’t understand very well

Can you provide more information on this for the rest of us doll collectors?


I’ve always felt that it should work this way too.. Thank you, thank you!

This is a really great site with alot of good well written post.Found this today via google will i was killing some time.I will be coming back and hopefully be able to contribute to the site.Thanks