get_user_option() WordPress Function

The get_user_option() function is used to retrieve a user option from the WordPress database. The function accepts two parameters: the user ID and the option name. The function returns the option value, or false if the option does not exist.

get_user_option( string $option, int $user, string $deprecated = '' ) #

Retrieves user option that can be either per Site or per Network.


Description

If the user ID is not given, then the current user will be used instead. If the user ID is given, then the user data will be retrieved. The filter for the result, will also pass the original option name and finally the user data object as the third parameter.

The option will first check for the per site name and then the per Network name.


Top ↑

Parameters

$option

(string)(Required)User option name.

$user

(int)(Optional) User ID.

$deprecated

(string)(Optional)Use get_option() to check for an option in the options table.

Default value: ''


Top ↑

Return

(mixed) User option value on success, false on failure.


Top ↑

More Information

Usage:
get_user_option( $option, $user );

Top ↑

Source

File: wp-includes/user.php

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 );
}


Top ↑

Changelog

Changelog
VersionDescription
2.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