Personalizing the WordPress Comment Reply Link

The default WordPress Comment Reply link, which shows up when threaded comments are enabled, is not very personalized. Every comment gets the same plain link that says "Reply":

Comment Reply Link without Comment Author Name

Every now and then I will get someone who accidentally presses one of those reply links thinking they're leaving a new top-level reply for the post.

That might seem like a silly mistake, but it helped me to realize that if the comment reply link included the name of the person you're replying to, it would be more difficult to make that mistake.

We should do anything we can to remove ambiguity. Besides, personalized comment reply links are just plain cool.

It took me a bit of digging around but I finally came up with a solution. Add the following code to the bottom of your theme's functions.php file:

/*
 * Change the comment reply link to use 'Reply to <Author First Name>'
 */
function add_comment_author_to_reply_link($link, $args, $comment){

    $comment = get_comment( $comment );

    // If no comment author is blank, use 'Anonymous'
    if ( empty($comment->comment_author) ) {
        if (!empty($comment->user_id)){
            $user=get_userdata($comment->user_id);
            $author=$user->user_login;
        } else {
            $author = __('Anonymous');
        }
    } else {
        $author = $comment->comment_author;
    }

    // If the user provided more than a first name, use only first name
    if(strpos($author, ' ')){
        $author = substr($author, 0, strpos($author, ' '));
    }

    // Replace Reply Link with "Reply to <Author First Name>"
    $reply_link_text = $args['reply_text'];
    $link = str_replace($reply_link_text, 'Reply to ' . $author, $link);

    return $link;
}
add_filter('comment_reply_link', 'add_comment_author_to_reply_link', 10, 3);

This code also takes into account the fact that some people might use more than a first name when they leave a comment. Having their whole name in the reply link would just look weird, so the code only uses the first name.

Here's what the comment reply links look like with the above code implemented:

Comment Reply Link with Comment Author Name

And that's it! You can see this code in action on my site in the comments section. (Check out this post for a ton of threaded comments.)

Supporting Translations

A commenter pointed out that you can modify the code as follows to support translations:

If you're like me and using WordPress language files to translate the site change this line:
`$link = str_replace($reply_link_text, 'Reply to ' . $author, $link);`
to
`$link = str_replace($reply_link_text, __( 'Reply', 'nameofyourtheme' ).' '. $author, $link);`

the `nameofyourtheme` string has to match the textdomain in your language file, in my case Im using the `twentytwelve` theme so I'll just type:

`$link = str_replace($reply_link_text, __( 'Reply', 'twentytwelve' ).' '. $author, $link);`

Please note that the `.' '.` is added to get a space between the your `"Reply"` string and `$author` variable.

Personalizing the Cancel Reply link

If you'd like to also personalize the 'Click here to cancel your reply' link to instead say "Cancel Reply to [author]", you can use the following code:

/*
 * Change the comment reply cancel link to use 'Cancel Reply to 
 */
function add_comment_author_to_cancel_reply_link($formatted_link, $link, $text){

    $comment = get_comment( $comment );

    // If no comment author is blank, use 'Anonymous'
    if ( empty($comment->comment_author) ) {
        if (!empty($comment->user_id)){
            $user=get_userdata($comment->user_id);
            $author=$user->user_login;
        } else {
            $author = __('Anonymous');
        }
    } else {
        $author = $comment->comment_author;
    }

    // If the user provided more than a first name, use only first name
    if(strpos($author, ' ')){
        $author = substr($author, 0, strpos($author, ' '));
    }

    // Replace "Cancel Reply" with "Cancel Reply to "
    $formatted_link = str_ireplace($text, 'Cancel Reply to ' . $author, $formatted_link);

    return $formatted_link;
}
add_filter('cancel_comment_reply_link', 'add_comment_author_to_cancel_reply_link', 10, 3);

Write a Comment

Comment

62 Comments

  1. Hello, Raam!

    First of all, congrats for the idea and the code!

    I got to your website, after I tried to find a way to translate the “Reply” button in my language. Your post is way more useful, but I still have a problem, because I want to translate your “Reply to” feature to “RΔƒspunde comentariului fΔƒcut de”. In Romanian it means “Answer to the comment made by”.

    Would you please help me to achieve that?

    Thank you!

    • I have to specify that I already tried to change this line of code

      $link = str_replace($reply_link_text, ‘Reply to ‘ . $author, $link);

      but it didn’t worked.

      • Hi Paul,

        The line that you said you tried changing is the correct line to change, so I’m really not sure why it wouldn’t be working.

        This should work:

        $link = str_replace($reply_link_text, 'Răspunde comentariului făcut de ' . $author, $link);
  2. This really is a neat script, I was on the lookout for something just like this.

    A solution for the above problem with translation. If you’re like me and using WordPress language files to translate the site change this line:
    $link = str_replace($reply_link_text, β€˜Reply to β€˜ . $author, $link);
    to
    $link = str_replace($reply_link_text, __( ‘Reply’, ‘nameofyourtheme’ ).’ ‘. $author, $link);

    the nameofyourtheme string has to match the textdomain in your language file, in my case Im using the twentytwelve theme so I’ll just type:
    $link = str_replace($reply_link_text, __( ‘Reply’, ‘twentytwelve’ ).’ ‘. $author, $link);

    Please note that the .’ ‘. is added to get a space between the your “Reply” string and $author variable.

  3. I have changed most of the template to russian in the comment.php file, but after one has commented and someone would like to reply to a comment there is one “Reply” button that I canΒ΄t seem to find and change. I have looked in all the .php files in theme editor and I canΒ΄t seem to find it anywhere. Someone who knows?

    I want to translate only button β€œReply” feature to β€œΠžΡ‚Π²Π΅Ρ‚ΠΈΡ‚ΡŒβ€. In Russian it means β€œReply”.

    • That text is generated by WordPress in the `comment_reply_link()` function. You might want to check your theme’s `functions.php` file and search for that function. Then you can tell the function to use a different “Reply” text like this.

      I hope that helps!

  4. there is just a little problem french people can have with all the accents Γ  Γ© etc., but i don t want to bother you with that πŸ˜‰

    • You should be able to fix that by modifying the code to support translations (see the “Supporting Translations” section I appended to the post).

  5. Hi Raam,

    Would it be possible to modify this code to only display the answer link to the post author ?

    Thanks for this great code and for your help.

      • Hi,

        Thank you for your answer.

        I meant the “reply” link. I finaly added this condition and seems ok :

        $current_user = wp_get_current_user();
        global $post;
        if ($current_user->ID == $author_id=$post->post_author ) {
        return $link;
        }
        Wich seems to be the only way to recober author ID outside the loop.

    • Hi James,

      Thesis uses its own theme framework, so unfortunately it will require custom code from the Thesis developers to make this work. I suggest contacting them to see how you can achieve this.

  6. Like other commenters here, I think the idea of personalising the Reply link is a very good one, particularly in terms of removing ambiguity. I get the impression that quite a few newbie commenters don’t really grasp what the link actually does.

    Is there a simple way of using your stripped-down comment author’s name to personalise a Cancel Reply link? I’m customising Twenty Twelve on a local server, and in the child comments.php I’ve had the line β€œ’cancel_reply_link’ => ‘Or click here to cancel a reply to this commenter’,” (which immediately follows β€œLeave a comment?” ). It seems silly to refer to β€œthis commenter” when the Reply link itself is personalised, so I’ve been trying out my next-to-nonexistent PHP skills with versions of β€œ’cancel_reply_link’ => ‘Or click here to cancel a reply to ‘ . $author,”, but without success. For the time being, I’ve changed the line to β€œ’cancel_reply_link’ => ‘Or click here to cancel this reply’”, which is OK, but if you know a simple way of personalising the Cancel Reply link, I’d appreciate a note below.

    Incidentally, if any readers of your post wish, like me, to add a question mark (or some other text) after the stripped-down comment author’s name, the line β€œ$link = str_replace($reply_link_text, ‘Reply to ‘ . $author. ‘?’, $link);” works β€” and is probably the closest I’ve ever come to writing PHP, even if it was achieved only by brute-force trawling through various permutations of full stops and commas. As a further incidental, I’ve enclosed the question mark in a span, so that the question mark ceases to be displayed on hover.

      • Thanks for this, Raam.

        I have an another question β€” which might be a bit trickier. Can you figure any way to switch between a threaded comments structure and a flat comments structure on a post-by-post basis in WP? The only way I know how to do this is to set up WP as a multi-site, with one of the multi-sites enabling threaded (nested) comments under Settings > Discussion, and the other one disabling it.

        For my part, I think a threaded comments structure is preferable for many types of post but a flat comments structure is best for posts on topics that are inclined to devolve into off-topic arguments between commenters.

        • I have no idea how you might go about changing that setting on a per-post basis. You might be able to “fake” the flat layout by using some CSS styles to undo the indenting and then hide the “Reply” links on each comment; that would essentially give you a flat layout even while comment threading is enabled. You could then write some code that only applies those “flat CSS rules” when a post has a specific Custom Field.

          You might try posting your question in the WordPress.org Support Forums to see if anyone else has any suggestions.

          • That’s a good idea. Thanks, Raam. In the flat-comments child theme of Twenty Twelve I’ve been working on, I’ve already hidden the comments’ Reply links via CSS, and I could try to figure out which selectors control the indenting and disable these via CSS on a post-by-post basis, presumably by calling a second stylesheet for a particular post via the Dashboard’s Edit Post screen (if that’s possible), or by specifying in the one-size-fits-all CCS file that the β€œhide comment Reply link” and the β€œflatten comment structure” code applies only to a set of posts with specific ID numbers. At the moment, many of WP’s selectors remain a black box to me but bit by bit the box is opening up its secrets.

  7. Nice function, works ‘out of box’.

    Enhancement: if using threaded comments, depth set to 3, how do you get a comment reply link for ‘level 3’ comments? Without further indenting of the comments. (For when you want to reply to a third-level threaded comment.)

    Suggestions?

    Thanks…Rick

      • If the settings are to have threads 3 levels deep, then there is no ‘reply’ link for a comment at level 3.

        You have to adjust the code to do that. And, after mucking about, I figured that out.

        I also figured out how to keep the comment content if you submit without filling in required fields. For instance, on your site, if you just type in the comment text, and don’t fill in the name or other required fields (which you can’t tell from your comment form, BTW), then you just get an error message, and you loose all of what you entered. (Which just happened to me, BTW, so had to start all over.)

        But I figured out that too.

        • I’m glad to hear you figured it out, Rick. Care to share what you did?

          I wasn’t aware of that issue with the comment form losing the comment content if you forget to fill in the required fields. Thanks for the heads up. That’s a must-fix IMO!

  8. Thanks for this Raam. I love it.

    Is there any chance you could show how I could make a little change?
    I would like the full name to be displayed when it is the author of the article. E.g. in this page, it would show ‘Reply to Raam Dev’ in your comments

  9. Hi,

    I’m using comment_reply_link on my site but the problem is that when i try to reply a comment, reply doesnt appear below the comment that i reply. It just acts like a new comment. Can you help me if you know the solution? Thanks.

  10. Hello Raam,

    I couldn’t find the answer on another post on your site, so I will ask the question here. Can you write a post or share a link to your code that you used to make the Add a Comment button?

    Also, a suggestion on your post. Since you are showing “to: author name” visually, you need to remove the ARIA label from the link or else screen reader users will here it twice.

    “Reply to Raam to Raam Dev”

    When it should really be.

    “Reply to Raam”

    Thanks.

    • Does that plugin allow you to show the name of the person in the reply link, as this article explains? Looking at the screenshots of your plugin, it doesn’t look like it does.

  11. hello Raam. many thanks for the post and function. workes fine.

    also i have a question related to this topic, but i was not able to find answer for it almost nowhere. maybe you can help me out.

    i’d like to know how is it possible to hide the Reply Button, or Remove it, IF the comment is awaiting for moderation .

    do you have any idea how this can be done ? thnks .

  12. Thanks for sharing! Adding the name to the reply link worked perfectly. For some reason, “anonymous” shows up in the cancel link every time on my site. Is there a way to make the cancel link use the same name that was gotten for the reply link instead of doing it all over again?

  13. Hi Raam,

    When implementing your code for the Cancel Reply Link, I encountered a problem.

    For example, John is the commenter. I managed to change the reply link to ‘Reply to John’ with your code. That’s cool.

    But when I clicked on the ‘Reply to John’ link, the cancel reply link shows ‘Cancel Reply to Wind’. No matter which commenter’s reply link I clicked on, it will always show ‘Cancel Reply to Wind’. Wind is the blog post author. And that’s me.

    Is there anything missing or misspelled on the code of your “Personalizing the Cancel Reply link”? I am not a code savvy guy. So, I need your help to fix this.

    Thank you very much.

  14. Hey Raam, great article, I was working with an issue in the comment area and found your article. Best Regards.!!

  15. Hi Raam,

    I tried your code and it works fine for the reply link.
    But, the problem comes with the reply title, which already content the author.

    In others words…
    The reply-link is OK and display the custom text + the author.
    The reply-title is not OK and display the custom text + the author + “to author”.

    The author is displaying 2 times.
    Did you know how to remove the author in the reply title ?

    Thanks

    • Hi Olivier,

      It sounds like the WordPress theme you’re using may be interfering with the code I shared above. I recommend posting your question on the WordPress.org support forums where there are lots volunteers who might be able to help. I recommend sharing the theme you’re using and either the code you’re using or a link back to this post.

Webmentions

  • Vajrasar Goswami December 6, 2020
  • Tal Gur December 6, 2020