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":
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:
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);
Genius! I also love the simplified comment form on your site now.
Thanks, Ali! 🙂
Such a great idea, Raam! We’re going to use this everywhere.
I’m glad you like it, Matthew! 🙂 It definitely helps make the comments section more friendly.
this code paste in function.php after what code paste on single.php
You don’t need to paste anything into
single.php
.A very great idea. Thanks for sharing this Raam.
You’re most welcome! 🙂
This is great, thanks for the code and idea.
You’re most welcome, David! 🙂
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:
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.
Hentiki, thanks so much for sharing your tweaks for use with translation. I’ve updated the post to include your suggestions.
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!
YOU ROCK !
Thx, for sharing your knowledge, it was very helpful 😉
My pleasure, Leila! 🙂
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).
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 William,
I’m not sure I understand your question. What do you mean by “answer link”?
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.
Brilliant! Thank you so much.
You’re most welcome! 🙂
It doesn’t work for me. I’m using Thesis 1.8 theme. No error, just that it displays only “Reply”
Any advice would be most appreciated.
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.
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 so much for the question, Chris! And great idea to also personalize the Cancel Reply link–I had not even thought of that for some reason. 😉
I’ve gone ahead and updated the post with the code necessary to also personalize the Cancel Reply link and I’ve tested it on the TwentyTwelve theme just to be sure. You can see the updated section at the bottom of the post: https://raamdev.com/2013/personalizing-the-wordpress-comment-reply-link/
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.
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
Hey Rick,
I’m not quite sure what you mean by “get a comment reply for ‘level 3′”; what are you trying to get?
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!
Thank you !
Did u know how to change the class ” comment-reply-link ” ?
i want to add icon from “iconmoons” font css beside the text. 🙂
i done it 😀
$add_below,
‘depth’ => $depth,
‘max_depth’ => $args[‘max_depth’]))), 1 );
?>
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
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.
Me too 🙁 please help us
Thank you very much for your helpful tips. Can you please tell m e when I put my website link in social media or any where, preview image comes blank. How can I fix ?
Hi Janet, I suggest posting your question on the WordPress Support Forum.
Hi Raam, when i try to add this code i got error. Need to change anything themes file?
Hey Acil,
I’m not sure. This was written a few years ago, but as far as I know it should still work.
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.
Hey Alex,
The “Add a Comment” button is part of the Independent Publisher theme that I’m using.
Regarding the ARIA label: that’s actually a bug in the Independent Publisher theme! Thanks so much for reporting it. 🙂 I’ve opened a bug report for that and I’ll get that issue fixed for the next release of the theme.
Awesome, let’s keep things accessible. 🙂
Or you can just use this plugin: https://wordpress.org/plugins/marctv-reply-button/screenshots/
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.
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 .
Hi Masoud,
Apologies for the very, very late reply. If you still need help with this, feel free to send me an email.
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?
Hi Violet,
Apologies for the very, very late reply. If you still need help with this, feel free to send me an email.
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.
Hi Wind,
Apologies for the very, very late reply. If you still need help with this, feel free to send me an email.
Hey Raam, great article, I was working with an issue in the comment area and found your article. Best Regards.!!
Hey Israel! It’s so good to hear from you! I’m happy that this helped. 🙂
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.
Hi, Raam!
Can you show the code to add the nickname of the comment recipient? Something like, “John replied to Michael.”
Unfortunately, I have not found a way to do this. I even created a comment numbering system… But it is not as obvious to users as nicknames or full names.
Hi milcheloveck!
Here’s an example of how to get the name of the comment parent: https://wordpress.stackexchange.com/a/371218
Thank you Raam!!!
Finally if found my solution. Thanks You saved my time.
I’m glad that I helped save you time, John!