Exclude admin bar from showing avatar customization

I am customizing the WP avatar (to fit a grid) and do not want the avatar customization to show on the admin bar drop down or in the dashboard or on the site (when the admin bar is visible).

I have excluded the avatar change from the dashboard using this code in functions.php:

Read More
function foo_change_avatar($class) {
    if(!is_admin()) {
           // avatar customization code
    }
    return $class;
}
add_filter('get_avatar','foo_change_avatar');

How can I also prevent my customization from showing in the admin bar dropdown when the admin bar is visible on the site?

Related posts

Leave a Reply

1 comment

  1. In the WP_Admin_Bar::add_menus() class method you will find the actions:

    add_action( 'admin_bar_menu', 'wp_admin_bar_my_account_menu', 0 );
    add_action( 'admin_bar_menu', 'wp_admin_bar_my_account_item', 7 );
    

    and the two corresponding callbacks are using get_avatar().

    To prevent the avatar changes in the admin bar, we can remove the foo_change_avatar filter before these callbacks and then add it again after the admin bar is rendered with the wp_after_admin_bar_render filter (it looks like this filter is not documented in the Codex):

    add_action( 'admin_bar_menu', function(){
        remove_filter('get_avatar','foo_change_avatar');
    },0); 
    
    add_action( 'wp_after_admin_bar_render', function(){
        add_filter('get_avatar','foo_change_avatar');
    });
    

    So this part of the admin menu bar:

    enter image description here

    where the get_avatar() is used, should be excluded from the changes.

    We could also have used the admin_bar_menu filter with priority greater than 7 instead of the wp_after_admin_bar_render filter.