clean_user_cache( WP_User|int $user )

In this article

Cleans all user caches.

Parameters

$userWP_User|intrequired
User object or ID to be cleaned from the cache

Source

function clean_user_cache( $user ) {
	if ( is_numeric( $user ) ) {
		$user = new WP_User( $user );
	}

	if ( ! $user->exists() ) {
		return;
	}

	wp_cache_delete( $user->ID, 'users' );
	wp_cache_delete( $user->user_login, 'userlogins' );
	wp_cache_delete( $user->user_nicename, 'userslugs' );

	if ( ! empty( $user->user_email ) ) {
		wp_cache_delete( $user->user_email, 'useremail' );
	}

	wp_cache_delete( $user->ID, 'user_meta' );
	wp_cache_set_users_last_changed();

	/**
	 * Fires immediately after the given user's cache is cleaned.
	 *
	 * @since 4.4.0
	 *
	 * @param int     $user_id User ID.
	 * @param WP_User $user    User object.
	 */
	do_action( 'clean_user_cache', $user->ID, $user );
}

Hooks

do_action( ‘clean_user_cache’, int $user_id, WP_User $user )

Fires immediately after the given user’s cache is cleaned.

Changelog

VersionDescription
6.2.0User metadata caches are now cleared.
4.4.0'clean_user_cache' action was added.
3.0.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.