Using WordPress ‘floated_admin_avatar()’ PHP function

The floated_admin_avatar() WordPress PHP function is used to add avatars to relevant locations in the admin interface, such as user profile pages and comment sections.

Usage

The function requires a single parameter, a string representing the username whose avatar you want to display.

floated_admin_avatar('username');

Parameters

  • $name (string) – Required. The username for which you want to display the avatar.

More information

See WordPress Developer Resources: floated_admin_avatar()

This function was introduced in WordPress version 5.6. As of now, there is no deprecation planned for it. The source code for this function can be found in the wp-admin/includes/user.php file.

Examples

Displaying the Avatar of the Current User

This function can be used to display the avatar of the currently logged-in user on their profile page.

$current_user = wp_get_current_user();
$floated_admin_avatar($current_user->user_login);

Displaying the Avatar of a Specific User

If you have a specific user whose avatar you want to display, you can pass their username directly to the function.

floated_admin_avatar('johndoe');

Using the Function in a Loop

If you’re displaying a list of users, you can use this function inside a loop to display each user’s avatar.

$users = get_users();
foreach ($users as $user) {
    floated_admin_avatar($user->user_login);
}

Displaying Avatars in the Comments Section

You can also use this function to display avatars in the comments section of a post.

$comments = get_comments();
foreach ($comments as $comment) {
    floated_admin_avatar($comment->user_id);
}

Using the Function in a Custom Plugin

If you’re creating a custom plugin that needs to display user avatars, you can use this function to handle that functionality.

function my_custom_plugin_show_avatars() {
    $users = get_users();
    foreach ($users as $user) {
        floated_admin_avatar($user->user_login);
    }
}
add_action('admin_init', 'my_custom_plugin_show_avatars');

This function will run when the admin interface initializes, and it will display the avatar for each user in the system.