How to override Member’s Avatars in BuddyPress

I have tried everything to override the BP Avatar. Found about 5-6 different Google results that basically all point to the same solution:

function override_bp_member_avatar($url) {
    return get_stylesheet_directory_uri() . '/default_avatar.png';
}
add_filter('bp_core_fetch_avatar_no_grav', '__return_true'); // http://codex.buddypress.org/extending-buddypress/tips-tricks/
add_filter('bp_core_mysteryman_src', 'override_bp_member_avatar'); // http://www.buddyboss.com/instructions/

Yet it does not work.

Read More

Has anyone had success using a default Avatar for members?

Related posts

Leave a Reply

1 comment

  1. The filters you cite are only for the default/fallback avatars. If you want to replace BP avatars altogether, the key filters are bp_core_fetch_avatar and bp_core_fetch_avatar_url. The latter filters the entire HTML avatar element, while the latter does just the URL.

    How you do the filtering depends on how fancy you want to get. The bp_core_fetch_avatar passes along a lot of useful parameters, if you want to totally rebuild the avatar HTML: https://buddypress.trac.wordpress.org/browser/tags/1.5.5/bp-core/bp-core-avatars.php#L297 Otherwise, for a quick fix, you could just preg_replace() the URL.

    /**
     * This function returns your new avatar URL. You can put whatever logic in here you want
     */
    function wpse_49216_my_new_avatar_url() {
        return 'http://example.com/avatar.jpg';
    }
    add_filter( 'bp_core_fetch_avatar_url', 'wpse_49216_my_new_avatar_url' );
    
    function wpse_49216_filter_bp_avatar( $html ) {
        return preg_replace( '/src=".+?"/', 'src="' . wpse_49216_my_new_avatar_url() . '"', $html );
    }
    add_filter( 'bp_core_fetch_avatar', 'wpse_49216_filter_bp_avatar' );
    

    EDIT: The above method will replace all avatars. If you only want to change the default/fallback avatar (for users who have not uploaded their own), try

    /**
     * Disable Gravatar throughout BP
     */
    add_filter( 'bp_core_fetch_avatar_no_grav', '__return_true' );
    
    /**
     * Provide a global user avatar default
     */
    function wpse_49216_my_new_default_avatar_url() {
        return 'http://example.com/avatar.jpg';
    }
    add_filter( 'bp_core_default_avatar_user', 'wpse_49216_my_new_default_avatar_url' );