WooCommerce: Change Review Author Display Name from Username

WooCommerce: Change Review Author Display Name from Username featured image
(Last Updated On: December 9, 2016)

Have you ever wanted to change the way that WooCommerce reviews are displayed in your store?

The code below, when added to your theme’s functions.php file will allow you to change comment author display from username to first name and last initial with a . after it.

Please note, you can always change this to suit your needs! The code should be a good starting point, however.

Also note, this may change how regular comment authors names display, but only if they are registered and logged in user’s comments…

Change WooCommerce Review Author Display Name from Username to “FirstName L.”

add_filter('get_comment_author', 'my_comment_author', 10, 1);

function my_comment_author( $author = '' ) {
// Get the comment ID from WP_Query
$comment = get_comment( $comment_ID );
if (!empty($comment->comment_author) ) {
if($comment->user_id > 0){
$user=get_userdata($comment->user_id);
$author=$user->first_name.' '.substr($user->last_name,0,1).'.'; // this is the actual line you want to change
} else {
$author = __('Anonymous');
}
} else {
$author = $comment->comment_author;
}

return $author;
}

Leave a comment below if this helped you get this done. If you’d like clarification, feel free to ask in the comments, too. If you want to book us to complete some WooCommerce customisation or other, contact us!

Happy woocommercing…

9 thoughts on “WooCommerce: Change Review Author Display Name from Username

  1. Hi there,

    This certainly works but it also changes the way customer names are displayed for the backend admin which makes searching for a customer or adding a customer order in the backend very difficult.

    Any workaround for that?

    1. Hi Leanne,

      It shouldn’t change the way customer names are displayed in admin. This code changes the “comment author” part on the *front end* of the website. It does not alter the way usernames / firstnam last name are shown in WooCommerce. Perhaps you installed something else that does that?

      Robin

      1. Can confirm I just installed this function on this website (it altered my name, but the username / buyer name in woocommerce remains the same as it was, first name, last name.).

        My comment author name, though, showed as Robin S., as this code intended.

        Tested and works. You have something else changing user name in wooocommerce / WP admin!

  2. Thanks for your reply Robin 🙂

    I will have to investigate what is causing that to change in the backend but your code certainly works well for the front end.

    Do you know how I could expand on your code to show the first name and last name initial for everyone except administrators? I would like to be able to display replies as Customer Service – of course your code currently works as promised and shows Customer S.

    Appreciate your advice.

  3. This worked perfectly but for some reason if the Full First and Last name is not already chosen as the Display Publicly then it shows “Anonymous” which is not good. If someone has 10k users you’d be sitting there all day.

    You would probably need a script to force the Display name to use First & Last

    1. Hi Stef,

      Yes this is outside of the scope of this function. If you don’t have names set, then this function wouldn’t work. The object of this little snippet is to stop showing full name of a buyer in reviews, which people can get antsy about (I don’t necessarily want my full name to show up – it might put me off posting a review at all!). Its most appropriate when reviews are left by genuine buyers only.

      This script quite literally sets the author to “Anonymous” if the user did not have a user account (i.e. on guest posted reviews). You can change “anonymous” to be something else, like “guest review” or make it use the comment name details provided.

      That’s beyond the scope of this snippet, but it shouldn’t be too hard to figure out… alas I’m deep in some other tasks right now, but another commentor might post this up… or I’ll circle back later and see what I can dream up!

      Good luck!

Leave a Reply

Your email address will not be published. Required fields are marked *

Silicon Dales