0

Yes, I need a code to do that. Plugin is not an option, I need to use theme's functions.php file.

Maybe this filter would help:

apply_filters( 'get_avatar', string $avatar, mixed $id_or_email, int $size, string $default, string $alt, array $args )

I have the user ID.

6
  • If you add code to the theme's function.php, the next theme update will overwrite your changes and your added code will be gone. Best to use a simple plugin (a simple plugin header and your code) to add the functionality you need. Feb 20, 2020 at 20:13
  • I'm curious, why would you need to do this?
    – Tom J Nowell
    Feb 20, 2020 at 21:30
  • @TomJNowell, I user asked and I couldn't do it just for her, so I had to disable gravatar for all users (bad).
    – Luis Rock
    Feb 21, 2020 at 15:51
  • @RickHellewell, I am using a child theme.
    – Luis Rock
    Feb 21, 2020 at 15:51
  • Then adding to the child theme's function.php file will be enough. But you didn't mention that. (Providing more details is always good when asking questions here.) Feb 21, 2020 at 16:03

1 Answer 1

2

Use this hook function to replace user's avatar (which is passed in as HTML <img ...> tag) if user's ID or email matches your condition. You can use any other default image, or get individual replacement image for each user.

function my_get_avatar($avatar, $id_or_email, $size, $default, $alt) {
    if ($id_or_email == "[email protected]") {
        $img = "http://path.to/default/avatar.jpg";
        $avatar = "<img src='".$img ."' alt='".$alt."' height='".$size."' width='".$size."' />";
    }
    return $avatar;
}
add_filter('get_avatar', 'my_get_avatar', 10, 5);
1
  • Can you edit this to explain what it does/how it does it so it isn't just copy pasta?
    – Tom J Nowell
    Feb 20, 2020 at 21:31

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.