Multiple custom buddypress avatars

Multiple custom buddypress avatars

I while back I wrote blog on custom buddypress avatars. A commenter asked about using more then one avtar. Hmmmm, good idea! Simple solution? Sure! We got that.

Create your avatars

And name them something like:

  1. my-avatar-1.png
  2. my-avatar-2.png
  3. my-avatar-3.png

And so on for as many as you’d like.

Mix it up

[code]
function myavatar_add_default_avatar( $url ){
$randomNumber = rand(1, 3);
$imgPath = ‘/_inc/images/default-grav-‘ . $randomNumber . ‘.png’;
return $imgPath;
}
add_filter( ‘bp_core_mysteryman_src’, ‘myavatar_add_default_avatar’ );
[/code]

How does it work?

  1. We hook into the bp_core_mysteryman_scr function.
  2. Generate a random number with the rand(min, max) PHP function. The key here is to define the two arguments to match the number of different default avatars you have.
  3.  Set the variable $imgPath to our new image path witch includes our $randomNumber variable.

Cavets?

Not really. However, there is nothing being saved to the database here so with each refresh, new random avatars will be generated for users who are don’t have a defined avatar.