6

I am trying to change the comment author name (or more specifically, the review author in WooCommerce) to be First name Last Initial (e.g., "John D." for "John Doe").

I got most of the way there with the following code in functions.php, but for some reason (perhaps because of how the comment/review was submitted) it tends to blank out the name and replace it with a period (".") in some (not all) of the reviews.

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 (!empty($comment->user_id)){
            $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 {
        $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
    }
    return $author;
}

If I tweak the code like this as a fallback, however, it always displays the full name:

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 (!empty($comment->user_id)){
            $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;
}

I would prefer to leave the actual names in the database intact, and just filter the output on the public-facing side of the site for comments (we might need their full name to display elsewhere, but can't really test it until the comment authors are displaying correctly).

3 Answers 3

3

You are missing a "NOT" logical operator (!) in your if statement. You want to say "if comment author IS NOT empty". As of now, the function is reading that the author is not empty and defaulting to your else statement that tells it to output the author's full name. Use the second block of code but make the following change.

Change the following:

if ( empty($comment->comment_author) ) {

to:

if ( !empty($comment->comment_author) ) {

Otherwise it looks ok to me.

4
  • OK, works great, except that some names still come across as "Anonymous" even though all reviews were done by registered users. I tweaked the code above to show full name as fallback for now...any suggestions? Feb 19, 2015 at 13:40
  • I havent tested this, but looking at the codex it appears as though, get_comment()->user_id defaults to 0 if submitted by a non user. Perhaps try changing if(!empty($comment->user_id)) to if($comment->user_id > 0) Feb 20, 2015 at 16:09
  • Also, you need to make sure the users are logged in while submitting the comments. If they are not, it is my understanding that a user_id of 0 will be logged (even if they use their same credentials). If you don't want to require that they log in first, you may have to write a simple function to check to see if the comment_author_email is registered to a current user. If so, you grab the rest of the user data that way. Feb 20, 2015 at 16:13
  • (^^^ this is not recommended, obviously, as people could submit comments under the guise of other users). Feb 20, 2015 at 16:20
2

Had the same problem ...

Heres my piece of code for that:

add_filter( 'comment_author', 'custom_comment_author', 10, 2 );

function custom_comment_author( $author, $commentID ) {

    $comment = get_comment( $commentID );
    $user = get_user_by( 'email', $comment->comment_author_email );

    if( !$user ):

        return $author;

    else:

        $firstname = get_user_meta( $user->ID, 'first_name', true );
        $lastname = get_user_meta( $user->ID, 'last_name', true );

        if( empty( $firstname ) OR empty( $lastname ) ):

            return $author;

        else:

            return $firstname . ' ' . $lastname;

        endif;

    endif;

}

It checks if there is a firstname and lastname and outputs these. If there are not the regular author is returned.

1
  • This works prefect. All other solutions have some problems.
    – Vlado
    Oct 25, 2022 at 9:07
0

Hmm, after a few minutes of debugging and reading through this topic, I've come to conclusion that is more easy to go through get_user_by() function

So I've went through get_user_by('email',$comment->comment_author_email) and managed to get the user details even if the comment/review is sent without user being logged in.

This is my full code

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

function comments_filter_uprise( $author = '' ) {
    $comment = get_comment( $comment_author_email );

    if ( !empty($comment->comment_author_email) ) {
        if (!empty($comment->comment_author_email)){
            $user=get_user_by('email', $comment->comment_author_email);
            $author=$user->first_name.' '.substr($user->last_name,0,1).'.';
        } else {
            $user = get_user_by( 'email', $comment->comment_author_email );
            $author = $user->first_name;
        }
    } else {
        $user=get_userdata($comment->user_id);
        $author=$user->first_name.' '.substr($user->last_name,0,1).'.';
        $author = $comment->comment_author;
    }
    return $author;
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.