The get_comment_author WordPress PHP filter allows you to modify the comment author’s name before it’s displayed on your website.
Usage
add_filter('get_comment_author', 'your_custom_function', 10, 3); function your_custom_function($author, $comment_id, $comment) { // your custom code here return $author; }
Parameters
$author
(string) – The comment author’s username.$comment_id
(string) – The comment ID as a numeric string.$comment
(WP_Comment) – The comment object.
More information
See WordPress Developer Resources: get_comment_author
Examples
Anonymize Comment Author
Replace the author’s name with “Anonymous” for all comments.
add_filter('get_comment_author', 'anonymize_comment_author', 10, 3); function anonymize_comment_author($author, $comment_id, $comment) { return 'Anonymous'; }
Add User Role to Comment Author
Display the user’s role next to their name in the comments.
add_filter('get_comment_author', 'add_user_role_to_comment_author', 10, 3); function add_user_role_to_comment_author($author, $comment_id, $comment) { $user = get_user_by('email', $comment->comment_author_email); if ($user) { $user_role = ucfirst(array_shift($user->roles)); $author = $author . ' (' . $user_role . ')'; } return $author; }
Highlight Admin Comments
Add “Admin” in bold next to the admin’s name in comments.
add_filter('get_comment_author', 'highlight_admin_comments', 10, 3); function highlight_admin_comments($author, $comment_id, $comment) { if (user_can($comment->user_id, 'manage_options')) { $author = $author . ' **(Admin)**'; } return $author; }
Replace Name with Custom Field
Display a custom field value instead of the author’s name.
add_filter('get_comment_author', 'replace_name_with_custom_field', 10, 3); function replace_name_with_custom_field($author, $comment_id, $comment) { $custom_name = get_comment_meta($comment_id, 'custom_name', true); if ($custom_name) { $author = $custom_name; } return $author; }
Add Emoji to Comment Author
Add a smiley emoji to the end of the comment author’s name.
add_filter('get_comment_author', 'add_emoji_to_comment_author', 10, 3); function add_emoji_to_comment_author($author, $comment_id, $comment) { return $author . ' 😊'; }