Old code in my plugin replaced the get_avatar. It stopped working with the release of WP 4.4, therefore, I decided to try to learn how to use add_filter instead.

There are other posts discussing the number of arguments / parameters being passed, etc. I've even looked at WordPress plugins in the repository which use add_filter, however, even a stripped down file is only loading the $avatar_defaults rather than the add_filter location.

Here is my "stripped down" version to test how to filter get_avatar. You'll see the method set_avatar should try to load a test.png but instead is loading the xenforo_logo.og.png for any non-Gravatar account.

class XenWord_Avatars {

    public function construct() {

    public function hooks() {
        add_filter( 'get_avatar', array( $this, 'set_avatar' ), 10, 5 );

    public function set_avatar( $avatar, $id_or_email, $size, $default, $alt ) {

        if ( is_admin() ) {
            $screen = get_current_screen();
            if ( is_object($screen) && in_array( $screen->id, array( 'dashboard', 'edit-comments' ) ) && $default == 'mm') {
                $default = get_option( 'avatar_default', 'mystery' );

        if ( $default != 'XenForo' ) {
            return $avatar;

        if ( $default == 'XenForo' ) {

            $newAvatar = plugins_url( '../admin/images/test.png', __FILE__ );
            return $newAvatar;

        return $avatar;


$xenword_avatars = new XenWord_Avatars();

 * Add Custom Avatar (Discussion Settings)
 * @param $avatar_defaults
 * @return mixed
 * @throws Zend_Exception
 function xenword_avatar_defaults( $avatar_defaults ) {

    // Get Avatar from plugin folder
    $new_avatar = plugins_url( '../admin/images/xenforo_logo.og.png', __FILE__ );
    $avatar_defaults[$new_avatar] = "XenForo";

    // Remove default avatars
    //unset ( $avatar_defaults['mystery'] );
    //unset ( $avatar_defaults['blank'] );
    //unset ( $avatar_defaults['gravatar_default'] );
    //unset ( $avatar_defaults['identicon'] );
    //unset ( $avatar_defaults['wavatar'] );
    //unset ( $avatar_defaults['monsterid'] );
    //unset ( $avatar_defaults['retro'] );
    return $avatar_defaults;

add_filter( 'avatar_defaults', 'xenword_avatar_defaults' );

Can anyone see anything obviously wrong with what is done in the above code? Once I figure out what is wrong then I can extend this method so that it gets the avatar from XenForo based on the user account.

1 Answer 1


Hopefully this will help someone else in the future.

Number of Parameters

Change the number of parameters in the set_avatar method to 6. The $args was added as the 6th.

Fix the options-discussion.php page

Next, the options-discussion.php page wasn't showing avatars properly and so the is_admin block of code needed to be added.

    public function set_avatar( $avatar, $id_or_email, $size = '96', $default = '', $alt = false, $args ) {

        if ( ! get_option( 'show_avatars' ) ) {
             return false;

        // Properly show Avatars and Gravatars on the options-discussion.php page
        if ( is_admin() ) {
            $screen = get_current_screen();
            if ( is_object( $screen ) && in_array( $screen->id, array( 'dashboard', 'options-discussion' ) ) && ( $default != 'XenForo' ) ) {
                return $avatar;

Debug in many places to check is_numeric, is_object, etc.

Next, a Zend_debug showed $id_or_email an object was being returned and this helped tremendously. In each of the conditionals, work to the $id.

After the conditional, use the $id to grab userinfo, and ultimately the URL ($out).

Return $avatar

The return $avatar is important, therefore, the ending lines require setting a url ($out).

        $avatar = "<img alt='{$safe_alt}' src='{$out}' class='avatar avatar-{$size} photo' height='{$size}' width='{$size}' />";
        return $avatar;

I hope this helps others trying to filter avatars rather than replace the get_avatar.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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