WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

All the comments on my website are anonymous and without registration. To post a comment you have to fill the fields name (not obligatorily) and the text of your comment (obligatorily).

I have the problem with the avatars. All of them are generated the same.

What should I do to make them different?

share|improve this question
Do you want to differentiate Registered users without avatars from Anonymous visitors? – brasofilo Jul 13 '12 at 15:25
No, I don't have registered users :-) – mvod Jul 14 '12 at 0:31
up vote 6 down vote accepted

There are two ways to customize the default avatar:

  1. Add a new default avatar to Settings/Discussion.
  2. Change the output of get_avatar().

Let’s start with the first option; this processes slightly faster.

Add a new default avatar to Settings/Discussion

There is a filter 'avatar_defaults'. You can add more avatars here.
You get an array of default images where the key is an URL and the value the visible name.

Sample Code

add_filter( 'avatar_defaults', 'wpse_58373_kitten_avatar' );

 * Add a new default avatar.
 * @param  array $avatar_defaults Key = URL, Value = Visible name.
 * @return array
function wpse_58373_kitten_avatar( $avatar_defaults )
    $avatar_defaults['http://placekitten.com/32/32'] = 'Kitty';

    return $avatar_defaults;


enter image description here

Change the output of get_avatar().

get_avatar() searches in an option named 'avatar_default' first. We can hook into 'pre_option_avatar_default' and return a custom (random) URL.

Sample code

add_filter( 'pre_option_avatar_default', 'wpse_58373_custom_default_avatar');

 * Return a random image URL
function wpse_58373_custom_default_avatar()
    /* We use images from WP here, you should change this and put some default
     * images into your theme or plugin directory.
    $base_url = admin_url( 'images' ) . '/';
    $images   = array ( 'wp-logo-vs.png', 'wpspin_dark.gif', 'yes.png' );
    $rand     = rand( 0, ( count( $images ) - 1 ) );

    return $base_url . $images[ $rand ];


enter image description here

As you can see – you should return an image with a size that matches your theme’s avatar size. :)

share|improve this answer
Thank you! Instead of placekitten.com I will try http://www.gravatar.com/avatar/{MD5}.jpg?s=32&d=identicon – mvod Jul 14 '12 at 0:35

Re: random avatar generation, this was an extremely helpful answer and worked a treat with one minor modification - the function in question required an argument to pass through. Without the argument, the code returned included a jumble of html which included both the desired url of the image, as well as url of the default icon from gravitar. I changed it from this:


to this:


and that seemed to generate the image url without the jumble. For ease of use, the final code, with the addition of get_template_directory_uri () to target the theme rather than the admin folder, and some tucking away of variables was:

// Random Avatar

add_filter( 'pre_option_avatar_default', 'mind_bullets_default_avatar' );

function mind_bullets_default_avatar ( $value ) {

  $tix_array = array ( 'Amer_Cyborg.jpg', 'Die_hard_3.jpg', 'Thing.jpg' );

// Include randomizer in url generator
    return get_template_directory_uri().'/images/Avatars/'.$tix_array [rand( 0, ( count( $tix_array ) - 1 ) )]; 


Hope to allow users to choose their own soon, but for those who just want to comment, it keeps the look of the site consistent.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.