5

Is there a way o changing user's avatar without plugins? Why there's no "Avatar Upload" section in Users > Your Profile?

I can't use a plugin. Am I blind or being forced to use Gravatar? ;/

3
  • Is there a reason you can't use plugins? Is this a wordpress.com blog? (Use @Jan in your reply so I get a notification)
    – Jan Fabry
    Commented Jan 24, 2011 at 15:59
  • @Jan: the reason is I'm helping my buddy to build his own free theme. And one of our goals is to make it plugin-free too. Commented Jan 24, 2011 at 20:57
  • Of course, you can always include all code from a plugin in your template too (via the functions.php file). Is that still "plugin-free" in your definition?
    – Jan Fabry
    Commented Jan 25, 2011 at 7:24

7 Answers 7

9

Avatars are meant to be controlled by the user, not by you. So yes, in a way, you're being forced to use the Gravatar service. But remember, it gives the user the ability to use the same avatar anywhere, and you can always restrict the display of a gravatar based on content ratings (G, PG, PG-13, R).

Gravatar is a hosted service, which is why there's no "Upload Avatar" section in the profile.

You say you "can't use a plugin," but really that's the only way you can add features. If you want to use something other than Gravatar, you'll need to load a plug-in to support it. There are a few plug-ins that support local avatars:

Otherwise, I recommend you educate your users on what Gravatars are and how to use them.

7
  • 2
    That's TOTALLY WRONG! I don't have to educate my users, because I want to change admin's avatar etc. And I'm not able to. And as far as I remember there was an option like this? ;/ That's forcing me to use some strange site that I don't even want to use. That's STRANGE. Imagine I want to set up a blog for a local business network without internet access (security reasons, restricted data) etc. And it's not even possible without a plugin. What a shame. Commented Jan 24, 2011 at 15:23
  • @Wordpressor: By "that's totally wrong", do you mean EAMann's answer is wrong, or is it the situation you don't agree with? Gravatar is owned by Automattic, the company of Matt Mullenweg, who decides what goes in WordPress. That's probably the reason why they only support it out of the box, and other methods are delegated to plugins.
    – Jan Fabry
    Commented Jan 24, 2011 at 15:58
  • 1
    @Jan Fabry: LOL! It's even funnier. I have to create additional input in my admin panel, because my user have to send his Gravatar ID to the back-end. WOOOHAA! AMAZING! I hate you Matt Mullenweg. Commented Jan 24, 2011 at 21:06
  • 2
    @Wordpressor For the context [ Decisions not Options ](wordpress.org/about/philosophy) is one of cornerstones of how are things done in WordPress. Defaulting to gravatars is decision that makes enough sense, is flexible enough and promotes affiliated service. And when decision is made it is usually up to plugins to provide tweaks or alternatives.
    – Rarst
    Commented Jan 24, 2011 at 21:19
  • 2
    @Wordpressor Please sing the random lyrics of Hello Dolly. It will make you feel better ;)
    – Michal Mau
    Commented Jan 25, 2011 at 4:11
6

If you are asking how to replace the default gravatar with one of your own, you can put this code in the functions.php (or better yet the custom-functions.php if your theme supports it).

add_filter( 'avatar_defaults', 'customgravatar' );

function customgravatar ($avatar_defaults) {
$myavatar = get_home_url('Template_directory') . '/images/mycustomgravatar.jpg';
$avatar_defaults[$myavatar] = "My Custom Logo";
return $avatar_defaults;
}

Create an image file and upload it to the image directory for your site. The "My Custom Logo" is the label for the option in the Discussion section of your Dashboard. This way any user who doesn't have a gravatar will get the logo of your site instead.

1
  • I've been using this code for ages, but I need to give the gravatars an ALT text. I'm not sure where I can enter that without breaking the code.
    – japanworm
    Commented May 23, 2015 at 14:17
3

You need a little more code for the avatar_defaults hook to work than the Ray Mitchell answer, I found this code on the wordpress codex pages and it worked fine thanks to the normalized user id passed on to the default avatar hook (this works either with a simple user id or email AND with the object user itself)

    // Apply filter
add_filter( 'get_avatar' , 'my_custom_avatar' , 1 , 5 );

function my_custom_avatar( $avatar, $id_or_email, $size, $default, $alt ) {
    $user = false;

    if ( is_numeric( $id_or_email ) ) {

        $id = (int) $id_or_email;
        $user = get_user_by( 'id' , $id );

    } elseif ( is_object( $id_or_email ) ) {

        if ( ! empty( $id_or_email->user_id ) ) {
            $id = (int) $id_or_email->user_id;
            $user = get_user_by( 'id' , $id );
        }

    } else {
        $user = get_user_by( 'email', $id_or_email );   
    }

    if ( $user && is_object( $user ) ) {

        if ( $user->data->ID == '1' ) {
            $avatar = 'YOUR_NEW_IMAGE_URL';
            $avatar = "<img alt='{$alt}' src='{$avatar}' class='avatar avatar-{$size} photo' height='{$size}' width='{$size}' />";
        }

    }

    return $avatar;
}
4
  • Is $default intended to be the default avatar in the event that these if-statements fail?
    – nu everest
    Commented Dec 12, 2016 at 3:46
  • As stated in the codex $default is the url of the deafult avatar while no avatar is selected, wordpress' default uses "Mystery man"
    – jilt
    Commented Dec 12, 2016 at 9:28
  • codex.wordpress.org/Function_Reference/get_avatar for more info
    – jilt
    Commented Dec 12, 2016 at 9:29
  • i know this old but i spent passed few days looking for a solution, and yours worked for me. Thanks!
    – 730wavy
    Commented Jul 5, 2020 at 20:42
0

I know that on one project site, I was able to create a field on the user profile editor page for accepting an image path(uploaded through the media page if I recall right) that was able to be used for avatars. No plugins needed.

0

I noticed that it depends on which theme is activated. No option in Mantra theme for example, but there is an option in the buddypress default theme.

0

You can do it... You need to add the image to your media. Copy that media file URL and paste/link it to your custom user avatar url under edit user. SORTED!

0

Add your avatar.jpg file to your child themes images folder and the following code to your child themes functions file:

add_filter( 'get_avatar' , 'add_custom_avatar' , 1 , 5 );

function add_custom_avatar( $avatar, $id_or_email, $size, $default, $alt ) {
    $user = false;

    if ( is_numeric( $id_or_email ) ) {

        $id = (int) $id_or_email;
        $user = get_user_by( 'id' , $id );

    } elseif ( is_object( $id_or_email ) ) {

        if ( ! empty( $id_or_email->user_id ) ) {
            $id = (int) $id_or_email->user_id;
            $user = get_user_by( 'id' , $id );
        }

    } else {
        $user = get_user_by( 'email', $id_or_email );   
    }

    if ( $user && is_object( $user ) ) {

        if ( $user->data->ID == '1' ) {
            $avatar = sprintf( '%s/images/avatar.jpg', get_stylesheet_directory_uri() );
            $avatar = "<img alt='{$alt}' src='{$avatar}' class='avatar avatar-{$size} photo' height='{$size}' width='{$size}' />";
        }

    }

    return $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.