Search code examples
wordpressavatargravatar

Remove gravatar from avatar link Wordpress


I am using function to change the default avatar, but it returns photo with a link from gravatar. What problem with Wordpress when they have to attach this link from that website? I want change totally it to my website link.

https://secure.gravatar.com/avatar/c4d1fc512b3f9979ac82dafdf6761889?s=60&d=https%3A%2F%2Fdomain.com%2Fwp-content%2Fthemes%2Fdomain%2Fimages%2Ficon%2Favatar.png&r=g

function sandyavatar ($avatar_defaults) {
    $myavatar = get_bloginfo('template_directory') . '/images/icon/avatar.png';
}

Can I use something like: preg_replace to change link? Or where Wordpress code put in for editing it.

Thank you so much.


Solution

  • You can add the following code fragment to your functions.php in your theme, to replace the gravatar-based avatar link with default avatars in local

    function replace_gravatar_with_default_local_avatar() {
        return some_url_to_default_avatar_image;
    }
    add_filter( 'pre_option_avatar_default', 'replace_gravatar_with_default_local_avatar' );
    

    And if you actually want sort of cache or the gravatar, you can establish a cache logic, and use the get_avatar hook to handle this. Here is an example of cache logic.

    function cache_gravatar($avatar) {
        $tmp = strpos($avatar, 'http');
        $g = substr($avatar, $tmp, strpos($avatar, "'", $tmp) - $tmp);
        $tmp = strpos($g, 'avatar/') + 7;
        $f = substr($g, $tmp, strpos($g, "?", $tmp) - $tmp);
        $w = get_bloginfo('wpurl');
        $e = ABSPATH .'avatar/'. $f .'.png';
        $t = dopt('d_avatarDate')*24*60*60; 
        if ( !is_file($e) || (time() - filemtime($e)) > $t )
            copy(htmlspecialchars_decode($g), $e);
        else
            $avatar = strtr($avatar, array($g => $w.'/avatar/'.$f.'.png'));
        if ( filesize($e) < 500 ) 
            copy(get_bloginfo('template_directory').'/img/default.png', $e);
        $avatar = preg_replace("/srcset='([^']*)'/", '', $avatar);
        return $avatar;
    }
    
    add_filter('get_avatar','cache_gravatar');