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/
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
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 1 year, 1 month ago by
bcworkz.
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
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.