on Technology

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:

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:

Leave a Reply for kadir Cancel Reply

Write a Comment

Comment

52 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.

Webmentions

  • https://raamdev.com/2013/personalizing-the-wordpress-comment-reply-link/

  • Vajrasar Goswami January 2, 2017

    Personalizing the WordPress Comment Reply Link via @RaamDev http://t.co/cyrpRRhfiX

  • Tal Gur January 2, 2017

    Via @raamdev Personalizing the WordPress Comment Reply Link http://t.co/iOTkbMvD1V