Using WordPress ‘refresh_user_details()’ PHP function

The refresh_user_details() WordPress PHP function cleans the user cache for a specific user.

Usage

refresh_user_details( $user_id );

Input:

$user_id = 42;

Output:
Cleans the user cache for user with ID 42.

Parameters

  • $id (int) (Required) – The user ID.

More information

See WordPress Developer Resources: refresh_user_details

Examples

Refreshing user cache after updating user profile

In this example, after updating a user’s profile, we refresh their cache using refresh_user_details().

// Updating user profile data
wp_update_user( array( 'ID' => $user_id, 'display_name' => 'John Doe' ) );

// Refreshing user cache
refresh_user_details( $user_id );

Refreshing user cache after changing user role

When changing a user’s role, use refresh_user_details() to clean their cache.

// Changing user role to 'editor'
$user = new WP_User( $user_id );
$user->set_role( 'editor' );

// Refreshing user cache
refresh_user_details( $user_id );

Refreshing user cache after updating user meta

After updating user meta, refresh their cache using refresh_user_details().

// Updating user meta data
update_user_meta( $user_id, 'twitter', '@example' );

// Refreshing user cache
refresh_user_details( $user_id );

Refreshing user cache after deleting user meta

Clean user cache using refresh_user_details() after deleting user meta.

// Deleting user meta data
delete_user_meta( $user_id, 'twitter' );

// Refreshing user cache
refresh_user_details( $user_id );

Refreshing user cache after adding a user to a blog

When adding a user to a blog, refresh their cache using refresh_user_details().

// Adding user to a blog
add_user_to_blog( $blog_id, $user_id, 'author' );

// Refreshing user cache
refresh_user_details( $user_id );