Title: Random Default Avatar Function
Last modified: August 20, 2016

---

# Random Default Avatar Function

 *  [actionbasic](https://wordpress.org/support/users/actionbasic/)
 * (@actionbasic)
 * [13 years, 4 months ago](https://wordpress.org/support/topic/random-default-avatar-function/)
 * I found this code :
 *     ```
       /* WpFreaks.com - Start of Default Avatar Modification */
       if ( !function_exists('fb_addgravatar') ) {
       function fb_addgravatar( $avatar_defaults ) {
       $myavatar = get_bloginfo('template_directory') . '/images/useravatar.png';
       $avatar_defaults[$myavatar] = 'Users';
       $myavatar2 = get_bloginfo('template_directory') . '/images/myavatar.png';
       $avatar_defaults[$myavatar2] = 'My Avatar';
       return $avatar_defaults; }
       add_filter( 'avatar_defaults', 'fb_addgravatar' ); }
       /* WpFreaks.com - End of Default Avatar Modification */
       ```
   
 * And it seems like it will do some of the job. I need it to pick a random image
   from a folder. Is this possible?
 * Any help would be great. Been searching and trying ideas for hours!
 * Best,
    Brandon

Viewing 12 replies - 1 through 12 (of 12 total)

 *  [indybook](https://wordpress.org/support/users/indybook/)
 * (@indybook)
 * [13 years, 4 months ago](https://wordpress.org/support/topic/random-default-avatar-function/#post-3411934)
 *     ```
       $folder = scandir( dirname(__FILE__). '/random_images' );
       $avs = array();
       	foreach( $folder  as $pic ) {
       		if( strpos($pic, '.') ! == 0 ) {
       			$avs[] = $pic; }
       	}
       	$n = rand(0, sizeof($avs)-1);
       	$avatar = '<a href="bla.."><img src="'. get_bloginfo('template_directory') . '/random_images/' . $avs[$n] . '"></a>';
               return $avatar;
       ```
   
 * whatever ‘avatar_defaults’ is. i’d use the filter with wp core ‘get_avatar’
 *  Thread Starter [actionbasic](https://wordpress.org/support/users/actionbasic/)
 * (@actionbasic)
 * [13 years, 4 months ago](https://wordpress.org/support/topic/random-default-avatar-function/#post-3411966)
 * indyhook,
 * thank you so much. I’n just not sure at all how to use that code. Is it used 
   on it’s own in the functions file?
 * I really appreciate any help. This is killing me!
 *  [indybook](https://wordpress.org/support/users/indybook/)
 * (@indybook)
 * [13 years, 4 months ago](https://wordpress.org/support/topic/random-default-avatar-function/#post-3411987)
 * Hi actionbasic.
 * no. this means to be a part of the function that you posted.
    place the code 
   instead all you have between
 * if ( !function_exists
 * and
 * }
    add_filter( ‘avatar_defaults’, ‘fb_addgravatar’ ); }
 * i’m only not sure what ‘avatar_defaults’ function is to where the filter’s added.
   so i’m not sure if the new part works as expected.
    where you actually have the
   initial code? at theme functions file? some plugin? let me know if it won’t work
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [13 years, 4 months ago](https://wordpress.org/support/topic/random-default-avatar-function/#post-3411988)
 * Hehe… simultaneous posting! Never mind.
    ——————————————— You’ll need to wrap 
   the code in a function and hook the filter ‘get_avatar’. Then it can go into 
   the theme’s functions.php file.
 *     ```
       function ab_rand_avatar() {
         //insert indybook's code here
       }
       add_filter('get_avatar', 'ab_rand_avatar');
       ```
   
 *  Thread Starter [actionbasic](https://wordpress.org/support/users/actionbasic/)
 * (@actionbasic)
 * [13 years, 4 months ago](https://wordpress.org/support/topic/random-default-avatar-function/#post-3412005)
 * Indybook / bcworkz,
 * Thank you so much for the help. Again still learning and this is darn tricky 
   for me for some reason! I’ve compiled the code based on both of your instructions.
   Does this look correct? Do I just place this in my functions file? I’m assuming
   I need to create the random_images folder in my template directory and fill it
   with avatar images? Do they have to be .png or anything? I don’t see any file
   type in the code.
 *     ```
       function ab_rand_avatar() {
       $folder = scandir( dirname(__FILE__). '/random_images' );
       $avs = array();
       	foreach( $folder  as $pic ) {
       		if( strpos($pic, '.') ! == 0 ) {
       			$avs[] = $pic; }
       	}
       	$n = rand(0, sizeof($avs)-1);
       	$avatar = '<a href="bla.."><img src="'. get_bloginfo('template_directory') . '/random_images/' . $avs[$n] . '"></a>';
               return $avatar;
       add_filter('get_avatar', 'ab_rand_avatar');
       ```
   
 *  Thread Starter [actionbasic](https://wordpress.org/support/users/actionbasic/)
 * (@actionbasic)
 * [13 years, 4 months ago](https://wordpress.org/support/topic/random-default-avatar-function/#post-3412038)
 * I decided to take a shot and add the above code to my functions file. I get this
   error:
 * Parse error: syntax error, unexpected ‘!’ in /home/content/e/a/t/eatt5911/html/
   wp-content/themes/ari/functions.php on line 44
 * That occurs in this line in the above code:
 * if( strpos($pic, ‘.’) ! == 0 ) {
 * Am I still way off? 🙂 Sorry guys, Tryin!
 * Best,
    Brandon
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [13 years, 4 months ago](https://wordpress.org/support/topic/random-default-avatar-function/#post-3412052)
 * Hi Brandon, I’m afraid I stuck my nose where it didn’t belong in an attempt to
   be helpful. I quickly realized I misconstrued indybook’s intent when we posted
   at the same time, thus the added “Never mind”. I suggest you re-read indybook’s
   second post and ignore my post entirely, then give it another go. He’s unsure
   of the proper filter tag to use, so try both.
 * As for the syntax error, I think the intent was no space between ! and == . Placing
   the code in functions.php file should be fine. The image folder with your avatar
   images should be subfolder /random_images/ in the template folder. Image format
   can be anything that a browser can show. Here I go again with my nose 🙂
 *  Thread Starter [actionbasic](https://wordpress.org/support/users/actionbasic/)
 * (@actionbasic)
 * [13 years, 4 months ago](https://wordpress.org/support/topic/random-default-avatar-function/#post-3412066)
 * Sorry guys to be a pain. Having no luck. IndyBook, not sure exactly what you 
   meant here:
 *     ```
       place the code instead all you have between
   
       if ( !function_exists
   
       and
   
       }
       add_filter( 'avatar_defaults', 'fb_addgravatar' ); }
       ```
   
 * I have inserted this code, trying to use PHP to get a random image…
 *     ```
       /* WpFreaks.com - Start of Default Avatar Modification */
       if ( !function_exists('fb_addgravatar') ) {
       function fb_addgravatar( $avatar_defaults ) {
       $myavatar = get_bloginfo('template_directory') . 'images/avatars/avatar_<?php echo(rand(1,5)); ?>.png';
       $avatar_defaults[$myavatar] = 'Users';
       return $avatar_defaults; }
       add_filter( 'avatar_defaults', 'fb_addgravatar' ); }
       /* WpFreaks.com - End of Default Avatar Modification */
       ```
   
 * Shouldn’t that work?
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [13 years, 4 months ago](https://wordpress.org/support/topic/random-default-avatar-function/#post-3412078)
 * I’m afraid you might be stuck with me, so no 🙂 You can’t echo out to pass a 
   value to PHP, nor should you define another `<?php ?>` block within another. 
   Try this line instead:
    `$myavatar = get_bloginfo('template_directory') . 'images/
   avatars/avatar_' . (string) rand(1,5) . '.png';`
 * I don’t know what structure $avatar_defaults is supposed to be, but your line`
   $avatar_defaults[$myavatar] = 'Users';` doesn’t look right. I would think this
   is more likely correct:
    `$avatar_defaults['Users'] = $myavatar;`
 *  Thread Starter [actionbasic](https://wordpress.org/support/users/actionbasic/)
 * (@actionbasic)
 * [13 years, 4 months ago](https://wordpress.org/support/topic/random-default-avatar-function/#post-3412079)
 * Thank you bcworkz! I feel I’m super close.
 *     ```
       if ( !function_exists('fb_addgravatar') ) {
       function fb_addgravatar( $avatar_defaults ) {
       $myavatar = get_bloginfo('template_directory') . '/images/avatars/avatar_' . (string) rand(1,5) . '.png';
       $avatar_defaults[$myavatar] = 'Users';
       return $avatar_defaults; }
       add_filter( 'avatar_defaults', 'fb_addgravatar' ); }
       ```
   
 * Adding that to my functions.php file will allow me to choose my custom avatar“
   Users” in the Discussions panel, and it WILL show up for commenters who do not
   have there own Avatar.
 * However, so far it is using the same image for all commenters.
 * Ideally, I would like to go to discussions, choose “Users” at the bottom of the
   Avatar list, and have a random image for each commenter who does not have their
   own. I’ll be using small record covers. So you would see a comment section with
   say, 5 different record covers mixed in with people who have an actual Avatar.
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [13 years, 4 months ago](https://wordpress.org/support/topic/random-default-avatar-function/#post-3412084)
 * I investigated this a bit, the hook you are using only adds the option for a 
   different style of avatar. The only thing stored is a static path which is the
   same for all users. In order to have different images assigned to each user, 
   you need to have a scheme where the same image is returned on every call based
   on the user email without storing it in a table.
 * You then hook the ‘get_avatar’ filter to implement this scheme for each comment.
   This hook is not called if a gravatar solution exists. It is triggered from the
   end of get_avatar() in wp-includes/pluggable.php.
 * A possible scheme would be to have eight different images. Do a hash of the email,
   and based on the last hexadecimal digit of the hash, assign one image to 0 and
   1, another to 2 and 3, yet another to 4 and 5, etc.
 * Sorry to give you a bunch more work, but now that you know some coding, it won’t
   be so bad? At least you have assurance this scheme will result in a working solution.
 * (BTW, love the record cover idea, that’s going to be fun!)
 *  [Frumph](https://wordpress.org/support/users/frumph/)
 * (@frumph)
 * [13 years, 1 month ago](https://wordpress.org/support/topic/random-default-avatar-function/#post-3412163)
 * I know i’m a bit late to the party… but I just snippitted the code I use for 
   inside my Easel theme (sans the options ability) which does what you want…
 * [http://frumph.net/2013/04/19/use-my-custom-default-random-avatars-code-in-any-theme/](http://frumph.net/2013/04/19/use-my-custom-default-random-avatars-code-in-any-theme/)

Viewing 12 replies - 1 through 12 (of 12 total)

The topic ‘Random Default Avatar Function’ is closed to new replies.

## Tags

 * [comment](https://wordpress.org/support/topic-tag/comment/)

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 12 replies
 * 4 participants
 * Last reply from: [Frumph](https://wordpress.org/support/users/frumph/)
 * Last activity: [13 years, 1 month ago](https://wordpress.org/support/topic/random-default-avatar-function/#post-3412163)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
