wppaste
WordPress

get_user_option( string $option, int $user = 0, string $deprecated = '' ): mixed

Since
2.0.0
Source
wp-includes/user.php:753
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.

Compatibility

WordPress
since 2.0.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$optionstring
User option name.
$userintoptional
User ID.Default: 0
$deprecatedstringoptional
Use get_option() to check for an option in the options table.Default: ''

Return value

mixed
User option value on success, false on failure.

Performance profile

How much work a call to get_user_option() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Heavy

Reaches the database via get_user_by().

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
14–45

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 53.

Plugin surface
1 hook

Third-party callbacks on 'get_user_option_{$option}' run inside this call, and their cost is not bounded by anything here.

Called by
30

30 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_filters()called directly
  • querycontent queryget_user_by()one call below get_user_option()

Further down the call graph this can also reach option, cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 16 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost get_user_option() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
empty($deprecated) && !empty($user)14get_userdata()
empty($deprecated) && empty($user)17get_current_user_id(), get_userdata()
!empty($deprecated) && !empty($user)18_deprecated_argument(), get_userdata()
!empty($deprecated) && empty($user)21_deprecated_argument(), get_current_user_id(), get_userdata()
empty($deprecated) && !empty($user) && !->has_prop()34get_userdata(), ->get_blog_prefix(), ->has_prop(), ->has_prop(), apply_filters()
empty($deprecated) && !empty($user) && ->has_prop()35get_userdata(), ->get_blog_prefix(), ->has_prop(), ->get(), apply_filters()
empty($deprecated) && empty($user) && !->has_prop()37get_current_user_id(), get_userdata(), ->get_blog_prefix(), ->has_prop(), ->has_prop(), apply_filters()
empty($deprecated) && !empty($user)38get_userdata(), ->get_blog_prefix(), ->has_prop(), ->has_prop(), ->get(), apply_filters()
empty($deprecated) && empty($user) && ->has_prop()38get_current_user_id(), get_userdata(), ->get_blog_prefix(), ->has_prop(), ->get(), apply_filters()
!empty($deprecated) && !empty($user) && !->has_prop()38_deprecated_argument(), get_userdata(), ->get_blog_prefix(), ->has_prop(), ->has_prop(), apply_filters()
!empty($deprecated) && !empty($user) && ->has_prop()39_deprecated_argument(), get_userdata(), ->get_blog_prefix(), ->has_prop(), ->get(), apply_filters()
empty($deprecated) && empty($user)41get_current_user_id(), get_userdata(), ->get_blog_prefix(), ->has_prop(), ->has_prop(), ->get(), apply_filters()
4 further outcomes, up to 45 instructions
!empty($deprecated) && empty($user) && !->has_prop()41_deprecated_argument(), get_current_user_id(), get_userdata(), ->get_blog_prefix(), ->has_prop(), ->has_prop(), apply_filters()
!empty($deprecated) && !empty($user)42_deprecated_argument(), get_userdata(), ->get_blog_prefix(), ->has_prop(), ->has_prop(), ->get(), apply_filters()
!empty($deprecated) && empty($user) && ->has_prop()42_deprecated_argument(), get_current_user_id(), get_userdata(), ->get_blog_prefix(), ->has_prop(), ->get(), apply_filters()
!empty($deprecated) && empty($user)45_deprecated_argument(), get_current_user_id(), get_userdata(), ->get_blog_prefix(), ->has_prop(), ->has_prop(), ->get(), apply_filters()

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 53 instructions, 14–45 executed per call, 5 branches. The work does not change between versions.

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 1

One hook fires while get_user_option() runs, in this order:

  1. apply_filters( get_user_option_{$option} )filterline 789 (+36 into the body)

    Filters a specific user option value.

Uses · 4

Used by · 30

Show all 30

Source code

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

Changelog

Introduced in 2.0.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 6.9.7 tag, from src/wp-includes/user.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.