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);