WordPress:本地主机上的自定义默认头像? [英] WordPress: Custom default avatar on localhost?
问题描述
我正在尝试在functions.php中向WordPress添加自定义默认头像,但是该图像未显示在Settings/Discussion或网站上的其他位置.该代码有效,因为已添加了带有自定义字段名称的新无线电字段,但是图像不会显示.我正在使用本地主机,因此头像不显示吗?
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.
这是代码:
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;
}
我尝试了其他示例和"Add-New-Default-Avatar"插件,结果相同.
I've tried other examples and the 'Add-New-Default-Avatar' plugin with the same result.
推荐答案
我也遇到了同样的问题,并提出了一个完全hackish的解决方案……虽然可行:)
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;
}
结果:
当然,此过滤器仅用于开发.
Of course, this filter is meant for development only.
这篇关于WordPress:本地主机上的自定义默认头像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!