Support » Developing with WordPress » Different base avatar picture per role

  • Hello everyone!
    I’m trying to assign a different base avatar picture per role.
    I normally use this code to customize the basic avatar image.

    add_filter( 'avatar_defaults', 'wp_new_gravatar' );
    function wp_new_gravatar ($avatar_defaults) {
    	$myavatar = 'https://MYSITE/my-icon.gif';
    	$avatar_defaults[$myavatar] = "Default Gravatar";
    	return $avatar_defaults;
    }

    In this case I would need to add a different image per role.
    I can’t enter the string that controls the role.
    Do you have an idea how to do this?

    Thank you very much

    • This topic was modified 3 weeks, 6 days ago by woodypad.
    • This topic was modified 3 weeks, 6 days ago by woodypad.
Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator bcworkz

    (@bcworkz)

    You can use the “pre_get_avatar” filter to manage what avatar is displayed for each user. Whatever HTML you return will be what’s used. Return null to accept whatever WP would normally do. Your callback could be passed a number of different ways to determine which user the avatar is for. Ideally your callback should be able to work with any of them, but your theme may only use a subset of these. You might get away with only needing to work with one type of user reference.
    https://developer.wordpress.org/reference/hooks/pre_get_avatar/

    Thread Starter woodypad

    (@woodypad)

    Hello,
    thanks for your reply, i tried to combine in several ways but i can’t get it to work probably because i’m young at this. Can you give an example?
    Thank you

    Moderator bcworkz

    (@bcworkz)

    Something like this (untested, may include syntax errors):

    add_filter('pre_get_avatar', 'avatar_by_role', 10, 3 );
    function avatar_by_url( $html, $id, $args ) {
       // assuming $id is an ID, but in reality could be an email instead
       $user = get_user_by('id', $id );
       $role = array_keys( $user->caps )[0];
       // assume avatar image file has a format similar to "avatar-administrator.jpg"
       return "<img src=\"https://example.com/wp-content/uploads/avatar-{$role}.jpg\" class=\"avatar\" />";
    }
    • This reply was modified 2 weeks, 5 days ago by bcworkz.
    Thread Starter woodypad

    (@woodypad)

    thank you,
    in your code if I understand correctly, you assign it to a user ID right?
    I’m trying to assign him to the whole role.

    Thank you

    Moderator bcworkz

    (@bcworkz)

    I don’t understand. Avatars are related to users, users have roles. Roles themselves don’t have avatars. So you need to determine a user’s role in order to assign an avatar according to their assigned role. The WP_User object has a list of assigned user roles. If they have more than one role assigned, my code arbitrarily selects the first in the list to determine the avatar.

    More sophisticated selection of role out of a list is possible, but picking the first is usually sufficient.

Viewing 5 replies - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.