pre_get_avatar_data
云策文档标注
概述
pre_get_avatar_data 是一个 WordPress 过滤器,用于在 get_avatar_data() 函数执行早期拦截并自定义头像数据。通过返回非空的 'url' 值,可以跳过默认的 Gravatar 获取流程,实现头像的快速自定义。
关键要点
- 过滤器名称:pre_get_avatar_data,在 get_avatar_data() 函数中调用,用于提前处理头像数据。
- 核心功能:通过设置返回数组中的 'url' 成员为非空值,可以短路 get_avatar_data() 的执行,直接返回自定义头像 URL。
- 参数说明:接受 $args 数组(包含 size、height、width、default、force_default、rating、scheme、processed_args、extra_attr 等)和 $id_or_email(用户 ID、邮箱、对象等)。
- 应用场景:常用于替换 Gravatar 头像为本地媒体库图片,以提升加载速度和隐私保护。
代码示例
add_filter( 'pre_get_avatar_data', function( $data, $id_or_email ) {
if ( 'author_email_to_check' == $id_or_email || 'author_id_to_check' == $id_or_email ) {
$img_src = wp_get_attachment_image_src( 'attachment_id_to_display', array( $data['width'], $data['height'] ) );
if ( isset( $img_src[0] ) ) {
$data['url'] = $img_src[0];
}
}
return $data;
}, 10, 2 );注意事项
- 使用示例代码时,需替换 'author_email_to_check'、'author_id_to_check' 和 'attachment_id_to_display' 为实际值。
- scheme 参数在 Gravatar 场景下被忽略,强制使用 HTTPS,但保留用于自定义头像系统。
- 此过滤器自 WordPress 4.2.0 版本引入。
原文内容
Filters whether to retrieve the avatar URL early.
Description
Passing a non-null value in the ‘url’ member of the return array will effectively short circuit get_avatar_data() , passing the value through the ‘get_avatar_data’ filter and returning early.
Parameters
$argsarray-
Arguments passed to get_avatar_data() , after processing.
More Arguments from get_avatar_data( … $args )
Arguments to use instead of the default arguments.
sizeintHeight and width of the avatar in pixels. Default 96.heightintDisplay height of the avatar in pixels. Defaults to $size.widthintDisplay width of the avatar in pixels. Defaults to $size.defaultstringURL for the default image or a default type. Accepts:'404'(return a 404 instead of a default image)'retro'(a 8-bit arcade-style pixelated face)'robohash'(a robot)'monsterid'(a monster)'wavatar'(a cartoon face)'identicon'(the “quilt”, a geometric pattern)'initials'(initials based avatar with background color)'color'(generated background color)'mystery','mm', or'mysteryman'(The Oyster Man)'blank'(transparent GIF)'gravatar_default'(the Gravatar logo) Default is the value of the'avatar_default'option, with a fallback of'mystery'.
force_defaultboolWhether to always show the default image, never the Gravatar.
Default false.ratingstringWhat rating to display avatars up to. Accepts:'G'(suitable for all audiences)'PG'(possibly offensive, usually for audiences 13 and above)'R'(intended for adult audiences above 17)'X'(even more mature than above) Default is the value of the'avatar_rating'option.
schemestringURL scheme to use. See set_url_scheme() for accepted values.
For Gravatars this setting is ignored and HTTPS is used to avoid unnecessary redirects. The setting is retained for systems using the ‘pre_get_avatar_data’ filter to customize avatars.processed_argsarrayWhen the function returns, the value will be the processed/sanitized $args plus a “found_avatar” guess. Pass as a reference.extra_attrstringHTML attributes to insert in the IMG element. Is not sanitized.
$id_or_emailmixed-
The avatar to retrieve. Accepts a user ID, Gravatar SHA-256 or MD5 hash, user email, WP_User object, WP_Post object, or WP_Comment object.
Source
$args = apply_filters( 'pre_get_avatar_data', $args, $id_or_email );
Changelog
| Version | Description |
|---|---|
| 4.2.0 | Introduced. |
Skip to note 2 content
wpexplorer
Can be used to display an image from your media library for a specific user to prevent requests to Gravatar and speed things up.
add_filter( 'pre_get_avatar_data', function( $data, $id_or_email ) { if ( 'author_email_to_check' == $id_or_email || 'author_id_to_check' == $id_or_email ) { $img_src = wp_get_attachment_image_src( 'attachment_id_to_display', array( $data['width'], $data['height'] ) ); if ( isset( $img_src[0] ) ) { $data['url'] = $img_src[0]; } } return $data; }, 10, 2 );Be sure to replace “author_email_to_check”, “author_id_to_check” and “attachment_id_to_display” to their corresponding values.