• Hi,

    could anyone tell me how I can add a class to the avatar shown in the adminbar? I found this code in adminbar.php:

    $avatar = get_avatar( $user_id, 16 );
    	$howdy  = sprintf( __('Howdy, %1$s'), $current_user->display_name );
    	$class  = empty( $avatar ) ? '' : 'with-avatar';
    
    	$wp_admin_bar->add_menu( array(
    		'id'        => 'my-account',
    		'parent'    => 'top-secondary',
    		'title'     => $howdy . $avatar,
    		'href'      => $profile_url,
    		'meta'      => array(
    			'class'     => $class,
    			'title'     => __('My Account'),
    		),
    	) );

    Thanks in advance.

Viewing 1 replies (of 1 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    Copy the whole wp_admin_bar_my_account_item() function to your functions.php:

    rename the function to my_admin_bar_my_account_item.
    and add the admin_bar_menu action to it:

    add_action( 'admin_bar_menu', 'my_admin_bar_my_account_item', 10 );
    
    /**
     * Add the "My Account" item.
     *
     * @since 3.3.0
     */
    function my_admin_bar_my_account_item( $wp_admin_bar ) {
    	$user_id      = get_current_user_id();
    	$current_user = wp_get_current_user();
    	$profile_url  = get_edit_profile_url( $user_id );
    
    	if ( ! $user_id )
    		return;
    
    	$avatar = get_avatar( $user_id, 16 );
    	$howdy  = sprintf( __('Howdy, %1$s'), $current_user->display_name );
    	$class  = empty( $avatar ) ? '' : 'with-avatar myclass';
    
    	$wp_admin_bar->add_menu( array(
    		'id'        => 'my-account',
    		'parent'    => 'top-secondary',
    		'title'     => $howdy . $avatar,
    		'href'      => $profile_url,
    		'meta'      => array(
    			'class'     => $class,
    			'title'     => __('My Account'),
    		),
    	) );
    }

    And add your class (myclass) to this line as in the example:

    $class  = empty( $avatar ) ? '' : 'with-avatar myclass';

    https://codex.www.ads-software.com/Function_Reference/add_node

    btw:
    consider creating a child theme instead of editing your theme directly – if you upgrade the theme all your modifications will be lost.

Viewing 1 replies (of 1 total)
  • The topic ‘add a class to account avatar in admin bar’ is closed to new replies.