I need for a specific plugin to override the wordpress function get_avatar() to bind the profile image to an other image with some condition.

How can I do this ? Is there a specific hook or filter to do this ?

4 Answers 4


There is get_avatar filter. codex ref


I can ovveride in functions.php the function get_avatar() but the best way seems to use the filter get_avatar.

I found the answer here : https://codex.wordpress.org/Plugin_API/Filter_Reference/get_avatar


The avatar function has a filter hook get_avatar. You should use this to filter and replace with your function, like the following example, without logic.

add_filter( 'get_avatar', 'get_avatar', 10, 5 );
 *  Override get_avatar with uploaded avatar else default.
function get_avatar( $avatar, $id_or_email, $size, $default, $alt )
    $avatar = "<img alt='{$alt}' src='{$avatar_path}' class='avatar avatar-{$size} photo avatar-default' height='{$size}' width='{$size}' />";
    return $avatar;

You can use this link

You can use the following filter -

// Apply filter
add_filter( 'get_avatar' , 'my_avatar_func' , 1 , 5 );
function my_avatar_func( $avatar, $id_or_email, $size, $default, $alt ) {
// your code here 
  • That's pretty much the same as all the other answers, isn't it? What's new here?
    – Rup
    Commented Aug 2, 2021 at 12:04

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.