get_user_option()获取用户选项

目录

描述

译文

检索用户选项,包括global、user或blog。

如果未给出用户ID,使用当前用户ID。如果给出用户ID,检索该用户相关数据。函数结果的过滤器将原始选项名称和最终用户数据库对象作为第三个参数传递。

该选项函数首先查找非全局名称,然后查找全局名称,如果仍然没有找到,函数会查找博客选项。可通过插件设置或修改选项。

原文

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

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

The function will first check for the site-specific user metadata, then the network-wide user metadata. The option can either be modified or set by a plugin.

get_user_option() 描述

用法

<?php get_user_option$option$user ?>

get_user_option() 用法

参数

$option

(string) (必填) User option name.

默认值: None

$user

(integer) (可选) User ID. If not given, the current user will be used.

默认值: 0

$deprecated

(string) (可选) This parameter is no longer used. Setting it will generate a deprecated argument notice.

默认值: Empty string

get_user_option() 参数

返回值

(mixed) 

Option value on success, false on failure

get_user_option() 返回值

示例

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */

<?php

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

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

?>

get_user_option() 示例

注意

  • 使用到: apply_filters() 调用 'get_user_option_$option' hook with result, option parameter, and user data object.
  • 使用到 global: (object) $wpdb WordPress database object for queries.

get_user_option() 注意

历史

  • 3.0.0 - The $check_blog_options parameter was deprecated, and the function no longer checks the options table if the option isn't found in wp_usermeta. See #11615.
  • 添加于 版本: 2.0.0

get_user_option() 历史

源文件

get_user_option() 函数的代码位于 wp-includes/user.php.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * Retrieve user option that can be either per Site or per Network.
 *
 * 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.
 *
 * @since 2.0.0
 *
 * @global wpdb $wpdb WordPress database object for queries.
 *
 * @param string $option     User option name.
 * @param int    $user       Optional. User ID.
 * @param string $deprecated Use get_option() to check for an option in the options table.
 * @return mixed User option value on success, false on failure.
 */
function get_user_option( $option, $user = 0, $deprecated = '' ) {
	global $wpdb;

	if ( !empty( $deprecated ) )
		_deprecated_argument( __FUNCTION__, '3.0' );

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

	if ( ! $user = get_userdata( $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;

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

get_user_option() 源文件

相关