clean_user_cache() WordPress Function

The clean_user_cache() function is a WordPress function that clears the user cache. This function is useful for when you need to clear the user cache and don't want to wait for the cache to expire.

clean_user_cache( WP_User|int $user ) #

Cleans all user caches.


Parameters

$user

(WP_User|int)(Required)User object or ID to be cleaned from the cache


Top ↑

Source

File: wp-includes/user.php

function clean_user_cache( $user ) {
	global $current_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_email, 'useremail' );
	wp_cache_delete( $user->user_nicename, 'userslugs' );

	/**
	 * 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 );

	// Refresh the global user instance if the cleaning current user.
	if ( get_current_user_id() === (int) $user->ID ) {
		$user_id      = (int) $user->ID;
		$current_user = null;
		wp_set_current_user( $user_id, '' );
	}
}


Top ↑

Changelog

Changelog
VersionDescription
5.8.0Refreshes the global user instance if cleaning the user cache for the current user.
4.4.0'clean_user_cache' action was added.
3.0.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More
Show More