Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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:

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?

share|improve this question
    
Why are you customizing it if you don't want to show it anywhere? –  vancoder Apr 9 '13 at 21:40
    
I am showing the avatar in several places throughout the site. It's easier to exclude the admin bar and dashboard than the several other places in the site. –  chowwy Apr 10 '13 at 14:59
add comment

1 Answer

up vote 3 down vote accepted

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.

share|improve this answer
    
Accepted and upvoted. Thank you for this complete answer. Very helpful! –  chowwy Apr 10 '13 at 15:00
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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