Customising default avatars in WordPress

A custom Default Avatar is an option that is built into the Hybrid framework, a happenstance which nudged me about my procrastination in implementing this on other WP sites.

How to do it:

  • Hybrid & Hybrid Child Themes

    Couldn’t be simpler. Go and create your default avatar (a grayscale or pale monochrome version of your favicon or site logo is a good idea for “branding”), go to your WP admin Dashboard and upload it using Media » Add New, then add the full URL to the option in Appearance » Hybrid Settings. Voila!

  • Standard WP themes – up to 2.7

    (You really should upgrade for the new security features). This solution takes only a few lines of code in the comments.php file.

    
    <?php if(function_exists('get_avatar')) {
    echo get_avatar( $comment, $size = '70', $default = '<?php bloginfo('template_directory'); ?>/images/gravatar.png' );
    } ?>
  • Standard WP themes – 2.7+

  • From WP 2.7+, the way that WordPress calls the comment-list has changed, so that the code above will not work if inserted into comments.php – there has to be code added to functions.php instead, and maybe just one little tweak to the array-call in comments.php to set the avatar size. If you are using one of the WordPress framework systems, the code may need adjusting to call framework hooks instead of standard WP hooks (see next section).

    Solution Source: first, add to functions.php (create it if it doesn’t already exist). This code assumes that you have uploaded your own custom gravatar image to a folder named ‘images’ in the theme directory – if you have uploaded the image elsewhere then change the code accordingly.

    <php
    function my_own_gravatar( $avatar_defaults ) {
        $myavatar = get_bloginfo('template_directory') . '/images/gravatar.png';
        $avatar_defaults[$myavatar] = 'GRAVATAR NAME DISPLAYED IN WORDPRESS';
        return $avatar_defaults;
    }
    add_filter( 'avatar_defaults', 'my_own_gravatar' );
    ?>

    You should now see a new default avatar with the name you gave it in the code above added to the list (Mystery Man, Wavicon, Identicon etc) on the Settings » Discussion page. Select it as the default and save. Then there’s merely avatar size to consider, which is a simple array insertion into the comments.php file at the line where we see wp_list_comments :

    <?php wp_list_comments(array('avatar_size'=>70, )); ?>
  • Thesis

    If you are using a WordPress frameworkyou may need to alter the above code slightly to fit with the framework architecture. For Thesis you definitely need a slight change in the code. There’s a good post here but the code’s final line wasn’t quite right. A quick visit to the DIYThemes forums showed me that someone had come up with a fix:

    1. Upload your custom gravatar to the Thesis custom/images folder.
    2. Add the following lines to the Thesis custom_functions.php file.
    
        /* Add a Custom Default Gravatar */
        if ( !function_exists('custom_gravatar') ) {
        function custom_gravatar( $avatar_defaults ) {
        $myavatar = get_bloginfo('template_directory') . '/custom/images/IMAGENAME.png';
        $avatar_defaults[$myavatar] = 'MYDEFAULTGRAVATARNAME';
    
        return $avatar_defaults;
        }
        add_filter( 'avatar_defaults', 'custom_gravatar' );
        }
  • Choose the new default avatar from the Settings » Discussions page, and there you are. Gravatar size can be tweaked as desired.

For more distinctiveness, avatars can also be customised to have different styles for authors and admins. I’ll do a tutorial on that in the next week or so.

2 thoughts on “Customising default avatars in WordPress

  1. Thanks a bundle for this! The other tutorials I’d found all came up with a blank avatar for some reason, so I’m glad that somebody out there has a working solution.

Leave a Reply

Your email address will not be published. Required fields are marked *