0

I'm using this custom function to get the default avatar from my server instead of gravatar:

if(!function_exists('custom_avatar')){
    function custom_avatar($avatar_defaults){
        $new_default_icon = 'http://localhost/gv/wp-content/images/mystery-man.png';
        $avatar_defaults[$new_default_icon] = 'Custom Avatar';
        return $avatar_defaults;
    }
    add_filter('avatar_defaults','custom_avatar');
}

but the custom avatar is not showing up, when I view source code then the src of the image look like this:

http://0.gravatar.com/avatar/a432e8915b383edd8d25c2a4fd5a6995?s=32&d=http%3A%2F%2Flocalhost%2Fgv%2Fwp-content%2Fimages%2Fmystery-man.png%3Fs%3D32&r=G&forcedefault=1

Why does my image src relative to gravatar here? How can I fix this problem?

2
  • Possible duplicate http://wordpress.stackexchange.com/questions/159482
    – Robert hue
    Sep 26, 2014 at 8:56
  • Whereever you want display your custom avatar, do something like this in that template of your active them: <div id="author-avatar"> <?php _e('<img src="'.get_bloginfo('stylesheet_directory').'/images/admin-avatar.jpg" width="48" height="48" alt="' . get_the_author() . '" />');?> </div><!-- #author-avatar -->
    – Tara
    Sep 26, 2014 at 15:53

3 Answers 3

0

just use the function get_avatar, $default is for your custom default.

<?php echo get_avatar( $id_or_email, $size, $default, $alt, $args ); ?> 

https://codex.wordpress.org/Function_Reference/get_avatar

0

Just add this in your functions.php file (Image URL must be complete). Then go to the gravatar choice section and choose the new avatar.

/*** Change default gravatar */
add_filter( 'avatar_defaults', 'new_gravatar' );
function new_gravatar ($avatar_defaults) {
$myavatar = 'https://example.com/image.webp';
$avatar_defaults[$myavatar] = "Default Gravatar";
return $avatar_defaults;
}
0

To display a custom default avatar instead of Gravatar in Wp using a filter hook, you can use the avatar_defaults filter hook

// Add custom default avatar
function custom_default_avatar( $avatar_defaults ) {
    $custom_avatar_url = 'URL_TO_YOUR_CUSTOM_AVATAR_IMAGE'; // Replace this with the URL of your custom avatar image
    $avatar_defaults['custom_avatar'] = 'Custom Avatar'; // Add a label for your custom avatar
    return $avatar_defaults;
}
add_filter( 'avatar_defaults', 'custom_default_avatar' );

// Function to replace Gravatar with custom default avatar
function replace_default_avatar( $avatar, $id_or_email, $size, $default, $alt ) {
    $custom_avatar_url = 'URL_TO_YOUR_CUSTOM_AVATAR_IMAGE'; // Replace this with the URL of your custom avatar image
    $custom_avatar = "<img alt='{$alt}' src='{$custom_avatar_url}' class='avatar avatar-{$size} photo' height='{$size}' width='{$size}' />";
    return $custom_avatar;
}
add_filter( 'get_avatar', 'replace_default_avatar', 10, 5 );

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.