Use my custom default random avatars code in any theme.
What this does is give your users who do *not* have gravatars from gravatar.com when they post comments, custom avatars that you create. It will pick one from inside the directory and constantly use it based on the end users email address that they use when making a comment.
This code will only replace “default” avatars, not ones that people have made at gravatar.com.
The ComicPress and Easel themes already have this, this is a code snippet for ‘other’ themes.
This is not code for someone who is completely new to PHP and WordPress coding.
Include this into your functions.php file of your main theme you use, not the child theme. You will have to add it again if your theme updates. This is meant for theme author’s to include it in their themes on updates.
function frumph_random_default_avatar_callback( $id_or_email = '' ) { if (!empty($id_or_email)) { $count = count($results = glob(get_stylesheet_directory() . '/images/avatars/*')); if ($count) { $checknum = hexdec(substr(md5($id_or_email),0,5)) % $count; if ($count > 0) { $custom_avatar = basename($results[(int)$checknum]); $replace_src_avatar = get_stylesheet_directory_uri().'/images/avatars/'.$custom_avatar; return $replace_src_avatar; } } else return get_option('avatar_default'); } else return get_option('avatar_default'); }
THEN, you need to modify your get_avatar call in your theme (if your theme has it), this code is if your theme HAS a call to the get_avatar function.
What you do is you search for the function ‘get_avatar’ within the theme you are using, for example the thematic theme in the library/extensions/ has a file called ‘comments-extensions’ inside of it and the line with get_avatar looks like this:
$avatar = str_replace( "class='avatar", "class='photo avatar", get_avatar( $avatar_email, $avatar_size ) );
You want to modify the get_avatar function in it, the 3rd position is the the reference to the ‘default’ image, right after size, you will pass the email of the commenter to the function.
$avatar = str_replace( "class='avatar", "class='photo avatar", get_avatar( $avatar_email, $avatar_size, frumph_random_default_avatar_callback($avatar_email) ) );
In the twentytwelve theme, it’s in the content*.php files, looks something like this:
<?php echo get_avatar( get_the_author_meta( 'user_email' ), apply_filters( 'twentytwelve_author_bio_avatar_size', 68 ) ); ?>
You modify it to add the 3rd argument which changes the default image that it uses, with my code you also pass the email of the commenter in the function.
<?php echo get_avatar( get_the_author_meta( 'user_email' ), apply_filters( 'twentytwelve_author_bio_avatar_size', 68 ), frumph_random_default_avatar_callback(get_the_author_meta('user_email')) ); ?>
THEN
Add your images into the theme (or child theme’s) images/avatars/ directory so wp-content/themes/mychildtheme/images/avatars/ directory.
The reason I use get_stylesheet_directory is so that the images are retained in the child theme and if the child theme doesn’t exist the end user can put them in their main theme, but checks child theme first.
– Phil
This is amazing!
I’ve searched many alternatives before landed to your code and it works.
Can you point me to the right direction on how to override the global get_avatar function and skip the extensive search on each of the theme templates?
Thank you.
With the way get_avatar works, it doesn’t work as a callback to the get_avatar function itself ;/ I tried for hours and hours to do it that way and couldn’t get it working.
Just wanted to let you know that this little piece of code helped me out fantastically. Thank you sir!