3

How do I display the commentor's first name and last name in the comments?... rather than their username as currently shows.

3 Answers 3

5

This will get you a first name + last name combination if available, or just the first name or last name if that's all your user submitted.

This assumes you're interested in registered user's names. If you're going to add first and last names to your commenting form... or treat first + last name as "display name" from the back end forward (so possibly not just in commenting forms), either would be something different!

For theme functions.php or plug-in:

add_filter( 'get_comment_author', 'wpse_use_user_real_name', 10, 3 ) ;

//use registered commenter first and/or last names if available
function wpse_use_user_real_name( $author, $comment_id, $comment ) {

    $firstname = '' ;
    $lastname = '' ;

    //returns 0 for unregistered commenters
    $user_id = $comment->user_id ;

    if ( $user_id ) {

        $user_object = get_userdata( $user_id ) ;

        $firstname = $user_object->user_firstname ;

        $lastname = $user_object->user_lastname ; 

    }

    if ( $firstname || $lastname ) {

        $author = $firstname . ' ' . $lastname ; 

        //remove blank space if one of two names is missing
        $author = trim( $author ) ;

    }

    return $author ;

}

Your results may of course vary depending on your installation and whatever particular requirements you may have added 1) for commenting (i.e., "anyone" vs "registered only") and 2) for signing up (are First and Last Name required to register?).

Additionally, in a full installation, you might want to adjust the user profile page, where the user selects a "display name." If you're going to display firstname/lastname instead then it'd be best to deal with that one way or another - by restricting choices, for example, or by adjusting labels and instructions.

1

With the $comment object you can get the name and by means of conditionals you can show the author as you want.
Here I leave an example to show name and first letter of the surname (if any).

add_filter( 'get_comment_author', 'mmr_use_user_real_name', 10, 3 );
function mmr_use_user_real_name( $author, $comment_id, $comment ) {

    $firstname   = '';
    $lastname    = '';
    $author_name = $comment->comment_author;

    if ( $author_name ) {
        $nombre_partes = explode( ' ', $author_name );
        $firstname     = $nombre_partes[0];
        $lastname      = $nombre_partes[1];
        if ( $lastname ) {
            $custom_lastname = substr( $lastname, 0, 1 );
            $author          = $firstname . ' ' . $custom_lastname . '.';
        } else {
            $author = $firstname;
        }
    }

    return $author;
}
0

Take a look at the 'callback' argument of the function wp_list_comments. You can set a own function to render the comment list: https://codex.wordpress.org/Function_Reference/wp_list_comments.

After some googling, i found a great and complete article that may help you: https://blog.josemcastaneda.com/2013/05/29/custom-comment/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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