Support » Plugins and Hacks » Nicer comment permalinks

  • Because I didn’t really like those permalinks to comments on my WordPress blog, I made some changes to wp-comments.php.
    Instead of http://www.yoursite.com/2004/01/01/a-post/#comment-176, the comments now start counting at 1, so the first comment on that page would be http://www.yoursite.com/2004/01/01/a-post/#comment-1.
    Here are the instructions. I’d like to see this implemented in WordPress.
    wp-comments.php
    search for
    <?php foreach ($comments as $comment) { ?>
    <li id="comment-<?php comment_ID() ?>">
    <?php comment_text() ?>
    <cite><?php comment_type(); ?> by <?php comment_author_link() ?> — <?php comment_date() ?> @ "><?php comment_time() ?></cite> <?php edit_comment_link('Edit This', ' |'); ?>

    <?php } // end for each comment ?>


    replace with
    <?php
    $i = 1;
    foreach ($comments as $comment) { ?>
    <li id="comment-<?php echo $i; ?>">
    <?php comment_text() ?>
    <cite><?php comment_type(); ?> by <?php comment_author_link() ?> — <?php comment_date() ?> @ #comment-<?php echo $i; ?>"><?php comment_time() ?></cite> <?php edit_comment_link('Edit This', ' |'); ?>

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


    Edit: The code didn’t show up perfectly. Replace those $s with a dollar sign.
    MaThIbUs

Viewing 12 replies - 1 through 12 (of 12 total)
  • zach

    (@zach)

    I really like the sounds of this. This is exactly what I want to do. Has anyone had problems implementing this? If not, does anybody see anything wrong with it? Or am I a guinea pig?

    MathiasBynens

    (@mathiasbynens)

    Matt probably fixed the dollar sign bug on the forums, so now the code shows up as it should. (Thanks Matt!)
    Zach: Just go and implement it by following the instructions, it should work fine… If you’re not feeling safe, create a backup of wp-comments.php. Let me know if it worked!
    I’m convinced you aren’t a guinea pig by the way.

    Sushubh

    (@sushubh)

    why not just make it like this!
    /1 for comment 1
    /2 for comment 2
    or am i asking for too much! 😀

    Moderator Matt Mullenweg

    (@matt)

    The reason that comment IDs are used instead of comment order is that the links are supposed to be permalinks. If you link to the fifth comment in a thread, and then the third comment gets deleted, your link is broken if you use the above method. It would also be broken if a comment is held for moderation and approved later, as once it’s approved it would show up in the normal flow with the time it was posted. Using the ID provides a permanent anchor for every comment as long as it exists on the page.

    MathiasBynens

    (@mathiasbynens)

    Matt: Yeah, but if a comment is deleted in a default WP install, the permalink won’t point to any comment anymore, but just to the post itself…
    I get your point; however I’ll still use these comment permalinks just because of the fact that nobody’s linking to comments on my blog but me 😉
    Sushubh:
    That won’t work, unless .htaccess allows rewriting # (which it doesn’t as far as I know).

    Lilandra

    (@lilandra)

    ooo
    i like that..

    Moderator Matt Mullenweg

    (@matt)

    You can’t access fragment identifiers (#anchors) with mod_rewrite. The post plus a number at the end is already used for posts with multiple pages.

    zach

    (@zach)

    If a comment is deleted, I believe it is good form to leave it’s carcass in the flow. Simply put ‘Flame removed.’, etc. But, now that I think about it, I really don’t relish the idea of seeing 10 comments in a row that say ‘Spam Removed’; that would suck.
    However, I do find it confusing as hell when a permalink to a comment says “Link to comment-375 on this page’, especially when it’s like the 3rd from the top. It hurts my trust in the permanence of the link.
    So now I don’t know what to do. I’ve already had to override the default ordered list’s numbering because I don’t want the ordered number different than the comment URI number.
    Dammit, what have I gotten myself into?

    Moderator Matt Mullenweg

    (@matt)

    If you don’t trust the permenance than create a custom system based on date and poster-name.

    charle97

    (@charle97)

    that’s what i’m going to do.

    zach

    (@zach)

    Wait. allusion’s idea can’t work. It makes too much sense. It’s too logical.

    Anonymous

    [digging it all up again]
    Having global comment IDs makes sense as permalinks, but the permalinks are no longer cruft-free. Just longer.
    Anyway, I replaced my comment_ID():
    function comment_ID() {
    global $comment;
    echo mysql2date('g:i:sa', $comment->comment_date);
    echo '-';
    $author = apply_filters('comment_author', $comment->comment_author);
    if (empty($author)) {
    echo 'Anonymous';
    } else {
    $author = preg_replace("/[^A-Za-z0-9]/", "", $author);
    echo $author;
    }
    }

    It will generate links like “/2004/05/21/hello-world/#3:31:00am-MrWordPress”. Everything works fine, so long as no-one wants to use accented or non-English characters.
    It should be obvious that I don’t know what I am doing, but maybe someone else can make this workable. 🙂

Viewing 12 replies - 1 through 12 (of 12 total)
  • You must be logged in to reply to this topic.