How to override get_avatar() function?

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 ?

Topic avatar pluggable hooks Wordpress

Category Web


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 
}

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;
}

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


There is get_avatar filter. codex ref

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.