Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ckchaudhary/8627935 to your computer and use it in GitHub Desktop.
Save ckchaudhary/8627935 to your computer and use it in GitHub Desktop.
<?php
add_filter( 'bp_core_fetch_avatar', 'revert_to_default_wp_avatar', 80, 3 );//late load
function revert_to_default_wp_avatar( $img, $params, $item_id ){
//we are concerned only with users
if( $params['object']!='user' )
return $img;
//check if user has uploaded an avatar
//if not then revert back to wordpress core get_avatar method
//remove the filter first, or else it will go in infinite loop
remove_filter( 'bp_core_fetch_avatar', 'revert_to_default_wp_avatar', 80, 3 );
if( !emi_user_has_avatar( $item_id ) ){
$width = $params['width'];
// Set image width
if ( false !== $width ) {
$img_width = $width;
} elseif ( 'thumb' == $width ) {
$img_width = bp_core_avatar_thumb_width();
} else {
$img_width = bp_core_avatar_full_width();
}
$img = get_avatar( $item_id, $img_width );
}
//add the filter back again
add_filter( 'bp_core_fetch_avatar', 'revert_to_default_wp_avatar', 80, 3 );
return $img;
}
/**
* Check if the given user has an uploaded avatar
* @return boolean
*/
function emi_user_has_avatar( $user_id=false ) {
if( !$user_id ){
$user_id = bp_loggedin_user_id();
}
if ( bp_core_fetch_avatar( array( 'item_id' => $user_id, 'no_grav' => true,'html'=> false, 'type' => 'full' ) ) != bp_core_avatar_default( 'local' ) )
return true;
return false;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment