Function and usage of get_avatar_data() in wordpress


Answers ( 1 )


    The get_avatar_data() function in WordPress is designed to retrieve information about avatars. Here's a detailed breakdown of its functionality and usage:

    Function Definition:

    get_avatar_data( mixed $id_or_email, array $args = null ): array


    • Purpose: To retrieve default data about an avatar.
    • Return Type: This function returns an array.


    1. $id_or_email (mixed, required):

      • This parameter specifies the avatar to retrieve. It accepts various types of input:
        • User ID
        • Gravatar MD5 hash
        • User email
        • WP_User object
        • WP_Post object
        • WP_Comment object
    2. $args (array, optional):

      • An array of arguments to customize the avatar data retrieval. Default arguments can be overridden by this parameter.
      • Key elements in the $args array:
        • size (int): Height and width of the avatar in pixels. Default is 96.
        • height (int): Display height in pixels. Defaults to the value of $size.
        • width (int): Display width in pixels. Defaults to the value of $size.
        • default (string): Specifies the URL for the default image or a default type (e.g., 'retro', 'robohash', 'monsterid', etc.). The default is the value of the 'avatar_default' option, with a fallback of 'mystery'.
        • force_default (bool): Determines whether to always show the default image. Default is false.
        • rating (string): Sets the rating for which avatars are displayed ('G', 'PG', 'R', 'X'). The default is the value of the 'avatar_rating' option.
        • scheme (string): URL scheme to use. See set_url_scheme() for accepted values.
        • processed_args (array): After the function returns, this contains the processed/sanitized $args plus a "found_avatar" guess. Passed by reference.
        • extra_attr (string): HTML attributes to insert in the IMG element. Not sanitized. Default is empty.

    Return Values:

    The returned array includes the arguments passed in $args along with additional elements:

    • found_avatar (bool): True if an avatar was found, false or not set if none was found.
    • url (string|false): The URL of the found avatar, or false if none was found.

    Sample Usage:

    // Example of retrieving avatar data for a user by email
    $user_email = "";
    $avatar_data = get_avatar_data($user_email, [
        'size' => 150,
        'default' => 'retro',
        'force_default' => true
    if ($avatar_data['found_avatar']) {
        echo 'Avatar URL: ' . $avatar_data['url'];
    } else {
        echo 'No avatar found.';

    In this example, the function retrieves the avatar data for a user based on their email address. It specifies a size of 150 pixels, uses a retro style as the default, and forces the default avatar to be used regardless of whether the user has a custom Gravatar. If an avatar is found, it displays the URL; otherwise, it indicates that no avatar was found.

Leave an answer