Gravatar support was added into the WordPress core in 2.5. You could toggle them and chose what rating level (G/PG/PG13/R/X) you wanted to allow, but you couldn’t easily change the default avatar for users without a custom Gravatar. That is, until recently.
Earlier this month, Ryan committed a modified version of a patch I wrote. It adds a list of default avatars to the bottom of the discussion options page and has already been added to WordPress.com.
What does this mean for theme developers? Well, this new feature also allows you to define custom default avatars that better match your theme. Here is some example code that you would add to your theme’s functions.php
:
<?php add_filter( 'avatar_defaults', 'mytheme_addgravatar' ); function mytheme_addgravatar( $avatar_defaults ) { $myavatar = get_bloginfo('stylesheet_directory') . '/images/avatar.png'; $avatar_defaults[$myavatar] = 'My Theme'; return $avatar_defaults; } ?>
That will add a new default avatar option to the bottom of the user’s settings page.
This is really a brilliant idea.
Well done.
Pingback: Wordpess 2.6 wird einige Verbesserungen bringen, die das System noch komfortabler für die Web2.0 Welt machen.
Absolutely superb – I had changed my default avatar with some coding in the comments.php file, but this is soooo much better! Great job!
Follow up: I just had a look at ticket #6802 and the .png image that was included – does this mean that your idea with an input box to enter a URL for the theme gravatar wasn’t accepted, as I can’t even see it on WordPress 2.7 beta?
John on December 1st, 2008 at 1:38 AM wrote:
Correct. It was decided that it’d be better if an upload form or whatever was there instead of just an input field. Better for the novice user.
However you can just set
$myavatar
in my above code to that URL and problem solved.but in the end they did nothing, as it doesn’t appear at all in WordPress 2.7 beta.
I have already used the code you provided above and it works perfectly, I just couldn’t understand why they hadn’t used your original patch or an amended version of it.
John on December 1st, 2008 at 1:15 PM wrote:
Correct. Having nothing there is better than a confusing option for the novice user. Advanced users can easily use the API.
Or even easier, just pass the URL as a parameter to
get_avatar()
in your theme.I had originally done it this way in comments.php, however, these days the “preferred” method of changing/customizing themes is by use of a child theme (this way there are no problems for theme updates).
Therefore, your method of using functions.php is a far better solution IMO.