WordPress: Custom default avatar on localhost?

梦想与她 提交于 2019-12-11 00:52:23

问题


I'm trying to add a custom default avatar to WordPress in functions.php, but the image is not displaying in Settings/Discussion or elsewhere on the site. The code works because a new radio field is added with the custom field name, but the image won't display. Is the avatar not displaying because I'm using Localhost?

I don't have enough reps to comment on similar questions.

here's the code:

add_filter( 'avatar_defaults' , 'wps_new_avatar' );
function wps_new_avatar( $avatar_defaults ){
    $new_avatar = get_stylesheet_directory_uri() . '/images/default-avatar.png';
    $avatar_defaults[$new_avatar] = "Default Avatar";
    return $avatar_defaults;
}

I've tried other examples and the 'Add-New-Default-Avatar' plugin with the same result.


回答1:


I was facing the same issue and came up with this completely hackish solution... It works though :)

add_filter( 'get_avatar', 'so_14088040_localhost_avatar', 10, 5 );

function so_14088040_localhost_avatar( $avatar, $id_or_email, $size, $default, $alt )
{
    $whitelist = array( 'localhost', '127.0.0.1' );

    if( !in_array( $_SERVER['SERVER_ADDR'] , $whitelist ) )
        return $avatar;

    $doc = new DOMDocument;
    $doc->loadHTML( $avatar );
    $imgs = $doc->getElementsByTagName('img');
    if ( $imgs->length > 0 ) 
    {
        $url = urldecode( $imgs->item(0)->getAttribute('src') );
        $url2 = explode( 'd=', $url );
        $url3 = explode( '&', $url2[1] );
        $avatar= "<img src='{$url3[0]}' alt='' class='avatar avatar-64 photo' height='64' width='64' />";
    }

    return $avatar;
}

Result:


Of course, this filter is meant for development only.



来源:https://stackoverflow.com/questions/14088040/wordpress-custom-default-avatar-on-localhost

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!