钩子文档

get_avatar_data

💡 云策文档标注

概述

本文档介绍了 WordPress 中的 'get_avatar_data' 过滤器,用于在获取头像数据时进行自定义处理。开发者可以通过此过滤器修改传递给 get_avatar_data() 函数的参数,以调整头像的显示属性。

关键要点

  • 'get_avatar_data' 过滤器允许在头像数据返回前修改参数,如大小、默认图像、评级等。
  • 参数 $args 是一个数组,包含 size、height、width、default、force_default、rating、scheme、processed_args 和 extra_attr 等选项。
  • 参数 $id_or_email 指定头像来源,支持用户 ID、邮箱、WP_User 对象等多种类型。
  • 此过滤器在 WordPress 4.2.0 版本中引入,常用于自定义头像显示逻辑。

📄 原文内容

Filters the avatar data.

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.

  • size int
    Height and width of the avatar in pixels. Default 96.
  • height int
    Display height of the avatar in pixels. Defaults to $size.
  • width int
    Display width of the avatar in pixels. Defaults to $size.
  • default string
    URL 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_default bool
    Whether to always show the default image, never the Gravatar.
    Default false.
  • rating string
    What 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.
  • scheme string
    URL 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_args array
    When the function returns, the value will be the processed/sanitized $args plus a “found_avatar” guess. Pass as a reference.
  • extra_attr string
    HTML 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

return apply_filters( 'get_avatar_data', $args, $id_or_email );

Changelog

Version Description
4.2.0 Introduced.