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 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.

Has anyone had success using a default Avatar for members?

share|improve this question

closed as off-topic by Pieter Goosen, birgire, toscho Aug 4 at 20:59

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

    
The Simple Facebook Connect plugin replaces avatars with Facebook profile pictures. You might check out that plugin, specifically the sfc-login.php file to see how it's being done. –  developdaly Apr 16 '12 at 21:21

1 Answer 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' );
share|improve this answer
    
Yes, it is possible. When you first said "I have tried everything to override the BP Avatar" I figured you wanted to override all avatars. I have edited my answer with a proper method. As you note, it's a bad idea to change the core files, and in this case there are several filters there that make it unnecessary to do so. –  Boone Gorges Apr 17 '12 at 12:29