为什么在打印WP_USER对象时不显示First_Name和Last_Name?

时间:2016-04-15 作者:somebodysomewhere

下面是一些简单的代码,

$user = get_userdata(5);
print_r($user);
echo $user->first_name;
结果呢,

WP_User Object
(
[data] => stdClass Object
    (
        [ID] => 9
        [user_login] => [email protected]
        [user_pass] => $P$Bck6WbL2RpNuUt4VjZGIp525jccfkE/
        [user_nicename] => adamcorolla-com
        [user_email] => [email protected]
        [user_url] => 
        [user_registered] => 2016-04-14 20:35:51
        [user_activation_key] => 
        [user_status] => 0
        [display_name] => addycorolla
    )

[ID] => 9
[caps] => Array
    (
        [subscriber] => 1
    )

[cap_key] => fccv_capabilities
[roles] => Array
    (
        [0] => subscriber
    )

[allcaps] => Array
    (
        [read] => 1
        [level_0] => 1
        [subscriber] => 1
    )

[filter] => 
)

Adam
为什么echo $user->first_name 当属性未列在print_r?

1 个回复
最合适的回答,由SO网友:Sumit 整理而成

first_namelast_name 不属于_users 表it是用户元数据。因此,用户对象没有它。所以当你访问该物业时first_name 用户对象有一个PHP魔术方法__get()

/**
 * Magic method for accessing custom fields
 *
 * @since 3.3.0
 * @param string $key
 * @return mixed
 */
public function __get( $key ) {
    if ( \'id\' == $key ) {
        _deprecated_argument( \'WP_User->id\', \'2.1\', __( \'Use <code>WP_User->ID</code> instead.\' ) );
        return $this->ID;
    }

    if ( isset( $this->data->$key ) ) {
        $value = $this->data->$key;
    } else {
        if ( isset( self::$back_compat_keys[ $key ] ) )
            $key = self::$back_compat_keys[ $key ];
        $value = get_user_meta( $this->ID, $key, true );
    }

    if ( $this->filter ) {
        $value = sanitize_user_field( $key, $value, $this->ID, $this->filter );
    }

    return $value;
}
查询元数据并从中返回值_usermeta 桌子

相关推荐

如何从USERS-edit.php中删除颜色选择器代码

我知道我不应该改变Wordpress的核心。但如果我想在“用户编辑”中使用仪表板用户配置文件页面。php和删除大块代码(如颜色选择器)的方法。从第259行到第336行-我想全部删除。<?php if ( ! ( IS_PROFILE_PAGE && ! $user_can_edit ) ) : ?> <tr class="user-rich-editing-wrap"> <th scope="