Skip to:
Content
Pages
Categories
Search
Top
Bottom

Display WP Username by default without Plugin

  • @sr3

    Participant

    When someone e.g. posts a comment, their first and last name is displayed instead of the username. How can I change that, so that their username will be displayed automatically?

    I know there’s the “BuddyPress Usernames only”-plugin, but it hasn’t been updated in over 2 years and I’d prefer to do this without a plugin.

Viewing 3 replies - 1 through 3 (of 3 total)
  • @henrywright

    Moderator

    If you don’t find a custom solution to this I just wanted to point out that plugin is built by @r-a-y, one of the BuddyPress core developers. I expect it should still work fine. As always, try it on a test install before adding to your live site.

    @sr3

    Participant

    Thanks, I only needed to override the author name in blog post comments, so I searched for the related code in the plugin, modified it and added it to my functions.php in my child theme. Now the username is displayed instead of the full name 🙂

    function sr_get_comment_author( $author ) {
    	global $bp, $comment;
    
    	if( $comment->user_id) {
    		$user=get_userdata($comment->user_id);
    		echo $user->user_nicename;
    	} else {
    		return $author;
    	}
    }
    add_filter( 'get_comment_author', 'sr_get_comment_author' );

    @shanebp

    Moderator

    You don’t need the globals.
    You aren’t using $bp and $comment is passed in the filter hook.

    apply_filters( 'get_comment_author', $author, $comment->comment_ID, $comment );

    Try:

    function sr_get_comment_author( $author, $comment_ID, $comment ) {
    
    	if ( isset( $comment->user_id ) ) {
    		$user = get_userdata($comment->user_id);
    		$author = $user->user_nicename;
    	} 
    
    	return $author;
    
    }
    add_filter( 'get_comment_author', 'sr_get_comment_author', 1, 3 );
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Display WP Username by default without Plugin’ is closed to new replies.
Skip to toolbar