WordPress.org

Ready to get started?Download WordPress

Forums

add a class to account avatar in admin bar (2 posts)

  1. hcroeze
    Member
    Posted 1 year ago #

    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.

  2. keesiemeijer
    moderator
    Posted 1 year ago #

    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';

    http://codex.wordpress.org/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.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags