我也很感兴趣,在花了几个小时测试、阅读源代码和一些在线文档之后。最终了解它是如何工作的。最后得出的方法简单但不明显。
不确定如何添加图像,您可以修改代码以满足您的实际情况here is the WordPress way to add custom Gravatar. 该示例使用一个虚拟占位符,您可以替换自己的gravatar。
WP中的头像图标来自gravatar。基本上是com。
添加后端选项
// a WordPress way to add avatar for non-registered user/anonymous
// add options in settings -> discussion
add_filter( \'avatar_defaults\' , \'ws366726_avatar_defaults\' );
function ws366726_avatar_defaults($avatar_defaults) {
// you may fetch the meta option for the image
// the key is the path to image for Gravatar based on http://en.gravatar.com/site/implement/images/
// this key will be loaded as default if image is not found for specific user in Gravatar database based on email lookup
// here, you may change to your image saved in meta
$avatar_defaults[\'https://via.placeholder.com/64\'] = \'My Default\';
return $avatar_defaults;
}
Gravatar图像:
http://2.gravatar.com/avatar/52b752660072401e4a971105206d44a0?s=32&d=mm&f=y&r=g mm是神秘人
通过更改d中的参数自定义图像示例:http://2.gravatar.com/avatar/52b752660072401e4a971105206d44a0?s=32&d=https://via.placeholder.com/32?ssl=1&f=y&r=g
这就是WordPress获取默认gravatar图像的方式。
删除后端注释列表中的默认gravatar渲染
// in hook \'admin_bar_menu\', the \'comment author\' will be available for removal
add_action (\'admin_bar_menu\' , \'ws366726_remove_default_avatar_in_comment_list\');
function ws366726_remove_default_avatar_in_comment_list() {
global $wp_filter;
// because \'comment_author\' by default defined inside a class, need to find it out and remove_filter
foreach ($wp_filter[\'comment_author\'][10] as $key => $value) {
if( preg_match(\'#floated_admin_avatar#\', $key) ) {
remove_filter( \'comment_author\', array( $wp_filter[\'comment_author\'][10][$key][\'function\'][0],\'floated_admin_avatar\' ), 10, 2);
}
}
}
为后端注释列表呈现自定义gravatar
add_filter( \'comment_author\' , \'ws366726_comment_author\', 11, 2 );
function ws366726_comment_author( $name ) {
// display default options instead, I think core team could implement it instead of a hard coded \'mystery\' in the future so that developer don\'t have to remove and add back the similar things
$default = get_option( \'avatar_default\', \'mystery\' ); // load the saved default in DB, by default, it is mystery
$avatar = get_avatar( get_comment(), 32, $default );
return "$avatar $name";
}
通过这种添加化身的方式,您可以在相同的设置菜单中控制默认化身
settings -> discussion. 因此,它保留了原有的灵活性。
背后的主要原因是gravatar由get_avatar()
函数,默认情况下,通过以下选项获取匿名化身选项get_option( \'avatar_default\', \'mystery\' )
哪个服务mystery
如果之前未保存,则为默认值。
这个key
神秘之处在于,当它创建链接到gravatar的url时,在何处查找图像。com。根据this post, 它可以是默认加载的自定义url。