How to override Member’s Avatars in BuddyPress [closed]

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="https://wordpress.stackexchange.com/questions/49216/.+?"https://wordpress.stackexchange.com/", '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' );

Leave a Comment