Random Default Avatar Function

UPDATE! After a little help from the wordpress.org forums, I made some progress with this code:

// Custom Default Avatar

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.

Read More

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.

I had this working 3 or so years ago, but it is not proving so easy this time. Again, all help is GREATLY appreciated.

Related posts

Leave a Reply

1 comment

  1. The function get_avatar‘s 3rd argument is the default image, which you can also pass a function’s return value to, so wherever you run get_avatar in your theme you can set a function that changes the default avatar as a 3rd argument.

    get_avatar( get_the_author_meta( 'user_email' ), 64, 'http://example.com/path/to/image.jpg' );
    

    Or, with a function’s return value as third argument:

    get_avatar( get_the_author_meta( 'user_email' ), 64, figure_out_new_default_image() );
    

    This IMO is the best place to pass the default image information to, since the $avatar_defaults is for setting up a singular image that can be configured in Settings > Discussion of wp-admin.