Replace default member avatar

function myavatar_add_default_avatar( $url )
{
  return get_stylesheet_directory_uri() . '/images/my_image.png';
}
add_filter( 'bp_core_mysteryman_src', 'myavatar_add_default_avatar' );

Replace buddyypress / wordpress avatar


//****************************************************
//****************************************************
//********** REPLACE DEFAULT WP USER AVATAR **********
//****************************************************
//****************************************************
add_filter( 'bp_core_fetch_avatar', 'my_bp_core_fetch_avatar', 10, 2 );
function my_bp_core_fetch_avatar ($avatar_img_tag, $params)
{

  if (
    (!empty($params)) &&
    ( ($params['object'] == 'user') || $params['object'] == '') )
  {
    //----- CALL IS FOR A USERS AVATAR -----
    $ImgWidth = intval($params['width']);
    $ImgHeight = intval($params['height']);
   
        
    $user_id = $params['item_id'];
    if (is_int($user_id))   //Ensure user ID is set
    {
   
      $ImageUrl = "ADD IMAGE URL HERE";

      
      if (strlen($ImageUrl ) > 0)
      {
          //WE HAVE AN IMAGE FOR THIS USER TO OVERRIDE THE DEFAULT IMAGE WITH
          
          //If there is a width and height specified then override styling to use object-fit: cover (fill area and crop image if needed)
          if ( ($ImgWidth > 0) && ($ImgHeight > 0) )
          {
            //Override the string with our styled img tag so image stays in correct proportion
            $ImgStyle = 'style="';
            $ImgStyle .= 'object-fit: cover;';
            $ImgStyle .= 'width: ' . $ImgWidth . 'px;';
            $ImgStyle .= 'height: ' . $ImgHeight . 'px;';
            $ImgStyle .= '" ';

            $avatar_img_tag = preg_replace( '/(<img src=")(.+)(" class.+)/', "$1$ImageUrl$3", $avatar_img_tag );      //Replace the url
            $avatar_img_tag = str_replace("/>", ">", $avatar_img_tag);                //Ensure it doesn't end with />
            $avatar_img_tag = str_replace(">", " $ImgStyle >", $avatar_img_tag);      //Add our special style to the end
          }
          else
          {
            //Use the original string and just replace the url
            $avatar_img_tag = preg_replace( '/(<img src=")(.+)(" class.+)/', "$1$ImageUrl$3", $avatar_img_tag );      //Replace the url
          }
         
      }
    }

  }

  return $avatar_img_tag;
}

/* THERE IS ALSO THIS FUNCTION IN CASE EVER NEEDED:
add_filter( 'bp_core_fetch_avatar_url', 'my_bp_core_fetch_avatar_url', 10, 2 );
function my_bp_core_fetch_avatar_url ( $avatar_url, $params )
{

	return $avatar_url;
}
*/
USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.

Comments

Your email address will not be published. Required fields are marked *