get_user_option()WP 2.0.0

Gets the specified user option. The option will relate to the site user in the network (for multisite installations) or just to the user.

If the user ID (the second parameter) is not passed, the function will use the currently authorized user.

The function first checks the user options (meta-fields) for the current site (check for the multisite version), if the option is found, it will return it; if the option is not found, it will get the global option (not for the network site).

Hooks from the function

Returns

Mixed. The value of the option or false if the option could not be retrieved.

Usage

get_user_option( $option, $user )
$option(string) (required)
The name of the user option to retrieve.
$user(int)
The ID of the user whose option needs to be retrieved.
Default: ''

Examples

0

#1 Usage Example

Let's check whether the user has the ability to use the admin bar in the front of the site:

$bar = get_user_option( 'show_admin_bar_front', get_current_user_id() );

if ( $bar ) {
	echo 'Admin bar is enabled';
} else {
	echo 'The admin bar is disabled';
}

Notes

  • Global. wpdb. $wpdb WordPress database abstraction object.

Changelog

Since 2.0.0 Introduced.

get_user_option() code WP 6.8.3

function get_user_option( $option, $user = 0, $deprecated = '' ) {
	global $wpdb;

	if ( ! empty( $deprecated ) ) {
		_deprecated_argument( __FUNCTION__, '3.0.0' );
	}

	if ( empty( $user ) ) {
		$user = get_current_user_id();
	}

	$user = get_userdata( $user );
	if ( ! $user ) {
		return false;
	}

	$prefix = $wpdb->get_blog_prefix();
	if ( $user->has_prop( $prefix . $option ) ) { // Blog-specific.
		$result = $user->get( $prefix . $option );
	} elseif ( $user->has_prop( $option ) ) { // User-specific and cross-blog.
		$result = $user->get( $option );
	} else {
		$result = false;
	}

	/**
	 * Filters a specific user option value.
	 *
	 * The dynamic portion of the hook name, `$option`, refers to the user option name.
	 *
	 * @since 2.5.0
	 *
	 * @param mixed   $result Value for the user's option.
	 * @param string  $option Name of the option being retrieved.
	 * @param WP_User $user   WP_User object of the user whose option is being retrieved.
	 */
	return apply_filters( "get_user_option_{$option}", $result, $option, $user );
}