Search code examples
phpfilterwoocommercereview

Hide User Review Email from Comment Section


I'm trying to prevent Woocommerce from displaying customer email as the product reviewer's name. So far all attempts to manually change it in the WP Admin User interface have failed. Figured I'd try out php here.

Found this code that's rendering the name in templates/single-product/review-meta.php:

    <strong class="woocommerce-review__author"><?php comment_author(); ?></strong> <?php

I need to modify the comment_author(), so I added a filter (I'm new to php, btw).

add_filter( 'comment_author', 'private_comment_author', 10, 0 );
function private_comment_author() {
    return $comment_ID;
}

The "$comment_ID" is a filler. How can I return the users public display name, or first and last name?


Solution

  • The filter passes the user name string so you can use that to get any information you want about the user like first name.

    function private_comment_author( $user_name ) {
        $user = get_user_by( 'login', $user_name );
    
        if( $user )
            return $user->first_name . ' ' . $user->last_name;
        else
            return '';
    }
    
    add_filter( 'comment_author', 'private_comment_author', 10, 1 );