avatar_defaults
云策文档标注
概述
本文档介绍了 WordPress 中的 'avatar_defaults' 过滤器,用于修改默认头像选项。头像以键值对形式存储,键为选项值,值为显示的头像名称。
关键要点
- 'avatar_defaults' 过滤器允许开发者自定义默认头像列表。
- 头像数据以关联数组形式传递,键是头像图片地址,值是头像显示名称。
- 该过滤器自 WordPress 2.6.0 版本引入。
代码示例
function wpdocs_new_gravatar( $avatar_defaults ) {
$new_default_avatar = 'https://mysite.com/wp-content/uploads/user.jpg';
$avatar_defaults[ $new_default_avatar ] = 'Default Gravatar I chose';
return $avatar_defaults;
}
add_filter( 'avatar_defaults', 'wpdocs_new_gravatar' );注意事项
- 添加新头像后,需在“设置”>“讨论”>“默认头像”中选择并保存。
- 新头像可在“编辑用户”管理界面中查看效果。
原文内容
Filters the default avatars.
Description
Avatars are stored in key/value pairs, where the key is option value, and the name is the displayed avatar name.
Parameters
$avatar_defaultsstring[]-
Associative array of default avatars.
Source
$avatar_defaults = apply_filters( 'avatar_defaults', $avatar_defaults );
Changelog
| Version | Description |
|---|---|
| 2.6.0 | Introduced. |
Skip to note 2 content
Rafa Carvalhido
Put the image address on the key and a title for it on the value.
function wpdocs_new_gravatar( $avatar_defaults ) { $new_default_avatar = 'https://mysite.com/wp-content/uploads/user.jpg'; $avatar_defaults[ $new_default_avatar ] = 'Default Gravatar I chose'; return $avatar_defaults; } add_filter( 'avatar_defaults', 'wpdocs_new_gravatar' );After that, you must go to
Settings >> Discussion > Default Avatarand you’ll see the new image in the bottom of options for gravatar. Select it and save changes.You can see the new avatars in action going to Edit Users admin sreen.