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
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$heightintDisplay height of the avatar in pixels. Defaults to $size.$widthintDisplay width of the avatar in pixels. Defaults to $size.$force_defaultbooldefault: falseWhether to always show the default image, never the Gravatar.$ratingstringdefault: is the value of the 'avatar_rating' optionWhat 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: nullURL scheme to use. See set_url_scheme() for accepted values.$classarray|stringdefault: nullArray or string of additional classes to add to the img element.$force_displaybooldefault: falseWhether to always show the avatar - ignores the show_avatars option.$loadingstringdefault: nullValue for theloadingattribute.$fetchprioritystringdefault: nullValue for thefetchpriorityattribute.$decodingstringdefault: nullValue for thedecodingattribute.$extra_attrstringdefault: emptyHTML 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
- Scaling
- Constant
- Instructions
- 74–214
- Plugin surface
- 2 hooks
- Called by
- 24
Reaches the database via get_user_by().
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 248.
Third-party callbacks on 'pre_get_avatar', 'get_avatar' run inside this call, and their cost is not bounded by anything here.
24 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- optionoption read or write
get_option()called directly - hookthird-party callbacks
apply_filters()called directly - cacheobject cache
wp_cache_get()one call below get_avatar() - serializeserialisation
maybe_unserialize()one call below get_avatar() - querycontent query
get_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.
| When | Instructions | Calls it makes |
|---|---|---|
!empty($args) && !is_object($id_or_email) && $avatar === null && !$args && !get_option() | 74 | get_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 !== null | 83 | get_option(), get_option(), wp_parse_args(), wp_get_loading_optimization_attributes(), array_merge(), apply_filters(), apply_filters() |
!empty($args) && $avatar === null | 89–91 | get_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–95 | get_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–95 | get_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() | 97 | get_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–208 | get_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–210 | get_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–212 | get_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–214 | get_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
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 248 | 74–214 | 23 | |
| 8.5 | 248 | 74–214 | 23 | |
| 8.4 | 248 | 74–214 | 23 | 6 fewer instructions than PHP 8.3 |
| 8.3 | 254 | 74–220 | 23 | |
| 8.2 | 254 | 74–220 | 23 | |
| 8.1 | 254 | 74–220 | 23 | |
| 7.4 | 254 | 74–220 | 23 |
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:
- apply_filters( pre_get_avatar )filterline 3255 (+58 into the body)
Allows the HTML for a user's avatar to be returned early.
- apply_filters( get_avatar )filterline 3357 (+160 into the body)
Filters the HTML for a user's avatar.
Uses · 10
- get_option()Retrieves an option value based on an option name.
- wp_parse_args()Merges user defined arguments into defaults array.
- wp_get_loading_optimization_attributes()Gets loading optimization attributes.
- get_comment()Retrieves comment data given a comment ID or comment object.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- get_avatar_url()Retrieves the avatar URL.
- get_avatar_data()Retrieves default data about the avatar.
- is_wp_error()Checks whether the given variable is a WordPress Error.
- esc_attr()Escaping for HTML attributes.
- esc_url()Checks and cleans a URL.
Used by · 24
- TwentyNineteen_Walker_Comment::html5_comment()Outputs a comment in the HTML5 format.
- TwentyTwenty_Walker_Comment::html5_comment()Outputs a comment in the HTML5 format.
- WP_Comments_List_Table::floated_admin_avatar()Adds avatars to comment author names.
- WP_MS_Users_List_Table::column_username()Handles the username column output.
- WP_Posts_List_Table::column_title()Handles the title column output.
- WP_Users_List_Table::single_row()Generates HTML for a single row on the users.php admin panel.
- Walker_Comment::comment()Outputs a single comment.
- Walker_Comment::html5_comment()Outputs a comment in the HTML5 format.
- _admin_notice_post_locked()Outputs the HTML for the notice to say that someone else is editing or has taken over editing of this post.
- _wp_dashboard_recent_comments_row()Outputs a row for the Recent Comments widget.
- floated_admin_avatar()Adds avatars to relevant places in admin.
- render_block_core_avatar()Renders the `core/avatar` block on the server.
Show all 24
- render_block_core_latest_comments()Renders the `core/latest-comments` block on server.
- render_block_core_post_author()Renders the `core/post-author` block on the server.
- twentyeleven_comment()Template for comments and pingbacks.
- twentyfourteen_list_authors()Prints a list of all site contributors who published at least one post.
- twentynineteen_get_user_avatar_markup()Returns the HTML markup to generate a user avatar.
- twentysixteen_entry_meta()Prints HTML with meta information for the categories, tags.
- twentyten_comment()Template for comments and pingbacks.
- twentytwelve_comment()Template for comments and pingbacks.
- wp_admin_bar_my_account_item()Adds the "My Account" item.
- wp_admin_bar_my_account_menu()Adds the "My Account" submenu items.
- wp_post_revision_title_expanded()Retrieves formatted date timestamp of a revision (linked to that revisions's page).
- wp_prepare_revisions_for_js()Prepare revisions for JavaScript.
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.
Signature, return type and hooks compared across 5 parsed releases.
fetchpriority argument.from the docblockdecoding argument.from the docblockloading argument.from the docblock$args parameter.from the docblockAbout 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.