wppaste
WordPress

get_avatar( mixed $id_or_email, int $size = 96, string $default_value = '', string $alt = '', array $args = null ): string|false

Since
2.5.0, 4.2.0, 5.5.0, 6.1.0, 6.3.0
Source
wp-includes/pluggable.php:3197
Retrieves the avatar `` tag for a user, email address, MD5 hash, comment, or post.

Compatibility

WordPress
since 6.3.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$id_or_emailmixed
The avatar to retrieve. Accepts a user ID, Gravatar MD5 hash, user email, WP_User object, WP_Post object, or WP_Comment object.
$sizeintoptional
Height and width of the avatar in pixels. Default 96.Default: 96
$default_valuestringoptional
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'.Default: ''
$altstringoptional
Alternative text to use in the avatar image tag.
Default empty.Default: ''
$argsarrayoptional
Extra arguments to retrieve the avatar.Default: null
  • $heightint

    Display height of the avatar in pixels. Defaults to $size.
  • $widthint

    Display width of the avatar in pixels. Defaults to $size.
  • $force_defaultbooldefault: false

    Whether to always show the default image, never the Gravatar.
  • $ratingstringdefault: is the value of the 'avatar_rating' option

    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)
  • $schemestringdefault: null

    URL scheme to use. See set_url_scheme() for accepted values.
  • $classarray|stringdefault: null

    Array or string of additional classes to add to the img element.
  • $force_displaybooldefault: false

    Whether to always show the avatar - ignores the show_avatars option.
  • $loadingstringdefault: null

    Value for the loading attribute.
  • $fetchprioritystringdefault: null

    Value for the fetchpriority attribute.
  • $decodingstringdefault: null

    Value for the decoding attribute.
  • $extra_attrstringdefault: empty

    HTML attributes to insert in the IMG element. Is not sanitized.

Return value

string|false
<img> tag for the user's avatar. False on failure.

Performance profile

How much work a call to get_avatar() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Heavy

Reaches the database via get_user_by().

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
74–214

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 248.

Plugin surface
2 hooks

Third-party callbacks on 'pre_get_avatar', 'get_avatar' run inside this call, and their cost is not bounded by anything here.

Called by
24

24 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • optionoption read or writeget_option()called directly
  • hookthird-party callbacksapply_filters()called directly
  • cacheobject cachewp_cache_get()one call below get_avatar()
  • serializeserialisationmaybe_unserialize()one call below get_avatar()
  • querycontent queryget_user_by()one call below get_avatar()

Further down the call graph this can also reach transient. That is the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 10 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost get_avatar() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
!empty($args) && !is_object($id_or_email) && $avatar === null && !$args && !get_option()74get_option(), get_option(), wp_parse_args(), wp_get_loading_optimization_attributes(), array_merge(), apply_filters(), get_option()
!empty($args) && !is_object($id_or_email) && $avatar !== null83get_option(), get_option(), wp_parse_args(), wp_get_loading_optimization_attributes(), array_merge(), apply_filters(), apply_filters()
!empty($args) && $avatar === null89–91get_option(), get_option(), wp_parse_args(), wp_get_loading_optimization_attributes(), array_merge(), apply_filters(), size(), array_merge(), get_avatar_data()
!empty($args) && $avatar === null && $args && is_wp_error()93–95get_option(), get_option(), wp_parse_args(), wp_get_loading_optimization_attributes(), array_merge(), apply_filters(), size(), array_merge(), get_avatar_data(), is_wp_error()
!empty($args) && $avatar === null && !$args && get_option()93–95get_option(), get_option(), wp_parse_args(), wp_get_loading_optimization_attributes(), array_merge(), apply_filters(), get_option(), size(), array_merge(), get_avatar_data()
!empty($args) && !is_object($id_or_email) && $avatar === null && get_option() && is_wp_error()97get_option(), get_option(), wp_parse_args(), wp_get_loading_optimization_attributes(), array_merge(), apply_filters(), get_option(), size(), array_merge(), get_avatar_data(), is_wp_error()
!empty($args) && $avatar === null && $args && !is_wp_error()168–208get_option(), get_option(), wp_parse_args(), wp_get_loading_optimization_attributes(), array_merge(), apply_filters(), size(), array_merge(), get_avatar_data(), is_wp_error(), esc_attr(), esc_url(), esc_url(), esc_attr(), apply_filters()
!empty($args) && $avatar === null && get_option() && !is_wp_error()172–210get_option(), get_option(), wp_parse_args(), wp_get_loading_optimization_attributes(), array_merge(), apply_filters(), get_option(), size(), array_merge(), get_avatar_data(), is_wp_error(), esc_attr(), esc_url(), esc_url(), esc_attr(), apply_filters()
!empty($args) && $avatar === null && $args && !is_wp_error()178–212get_option(), get_option(), wp_parse_args(), wp_get_loading_optimization_attributes(), array_merge(), apply_filters(), size(), array_merge(), get_avatar_data(), is_wp_error(), array_merge(), esc_attr(), esc_url(), esc_url(), esc_attr(), apply_filters()
!empty($args) && $avatar === null && get_option() && !is_wp_error()182–214get_option(), get_option(), wp_parse_args(), wp_get_loading_optimization_attributes(), array_merge(), apply_filters(), get_option(), size(), array_merge(), get_avatar_data(), is_wp_error(), array_merge(), esc_attr(), esc_url(), esc_url(), esc_attr(), apply_filters()

This body has more branch combinations than are worth enumerating, so the table covers the outcomes found first rather than every one that exists.

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev24874–21423
8.524874–21423
8.424874–214236 fewer instructions than PHP 8.3
8.325474–22023
8.225474–22023
8.125474–22023
7.425474–22023

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 3

3 hooks fire while get_avatar() runs, in this order:

  1. apply_filters( pre_get_avatar )filterline 3255 (+58 into the body)

    Allows the HTML for a user's avatar to be returned early.

  2. apply_filters( get_avatar )filterline 3259 (+62 into the body)

    Filters the HTML for a user's avatar.

  3. apply_filters( get_avatar )filterline 3357 (+160 into the body)

    Filters the HTML for a user's avatar.

Uses · 10

Used by · 24

Show all 24

Source code

	function get_avatar( $id_or_email, $size = 96, $default_value = '', $alt = '', $args = null ) {		$defaults = array(			// get_avatar_data() args.			'size'          => 96,			'height'        => null,			'width'         => null,			'default'       => get_option( 'avatar_default', 'mystery' ),			'force_default' => false,			'rating'        => get_option( 'avatar_rating' ),			'scheme'        => null,			'alt'           => '',			'class'         => null,			'force_display' => false,			'loading'       => null,			'fetchpriority' => null,			'decoding'      => null,			'extra_attr'    => '',		); 		if ( empty( $args ) ) {			$args = array();		} 		$args['size']    = (int) $size;		$args['default'] = $default_value;		$args['alt']     = $alt; 		$args = wp_parse_args( $args, $defaults ); 		if ( empty( $args['height'] ) ) {			$args['height'] = $args['size'];		}		if ( empty( $args['width'] ) ) {			$args['width'] = $args['size'];		} 		// Update args with loading optimized attributes.		$loading_optimization_attr = wp_get_loading_optimization_attributes( 'img', $args, 'get_avatar' ); 		$args = array_merge( $args, $loading_optimization_attr ); 		if ( is_object( $id_or_email ) && isset( $id_or_email->comment_ID ) ) {			$id_or_email = get_comment( $id_or_email );		} 		/**		 * Allows the HTML for a user's avatar to be returned early.		 *		 * Returning a non-null value will effectively short-circuit get_avatar(), passing		 * the value through the {@see 'get_avatar'} filter and returning early.		 *		 * @since 4.2.0		 *		 * @param string|null $avatar      HTML for the user's avatar. Default null.		 * @param mixed       $id_or_email The avatar to retrieve. Accepts a user ID, Gravatar MD5 hash,		 *                                 user email, WP_User object, WP_Post object, or WP_Comment object.		 * @param array       $args        Arguments passed to get_avatar_url(), after processing.		 */		$avatar = apply_filters( 'pre_get_avatar', null, $id_or_email, $args ); 		if ( ! is_null( $avatar ) ) {			/** This filter is documented in wp-includes/pluggable.php */			return apply_filters( 'get_avatar', $avatar, $id_or_email, $args['size'], $args['default'], $args['alt'], $args );		} 		if ( ! $args['force_display'] && ! get_option( 'show_avatars' ) ) {			return false;		} 		$url2x = get_avatar_url( $id_or_email, array_merge( $args, array( 'size' => $args['size'] * 2 ) ) ); 		$args = get_avatar_data( $id_or_email, $args ); 		$url = $args['url']; 		if ( ! $url || is_wp_error( $url ) ) {			return false;		} 		$class = array( 'avatar', 'avatar-' . (int) $args['size'], 'photo' );

Changelog

Introduced in 2.5.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

6.3.0
Added the fetchpriority argument.from the docblock
6.1.0
Added the decoding argument.from the docblock
5.5.0
Added the loading argument.from the docblock
4.2.0
Added the optional $args parameter.from the docblock
2.5.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/pluggable.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.