如何从 WP_User 对象获取 Wordpress 用户的名字?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11995588/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 18:59:05  来源:igfitidea点击:

How to get Wordpress user's first name from WP_User object?

wordpress

提问by emersonthis

I'm writing a basic plugin. Here's my code:

我正在编写一个基本的插件。这是我的代码:

    $new_user = get_userdata($user_id); //$user_id is passed as a parameter

$first_name1 = $new_user->user_firstname;
$last_name1 = $new_user->user_lastname;
    echo "<" . $first_name1 . $last_name1 . ">";
    //returns: <>

$first_name2 = $new_user-first_name;
$last_name2 = $new_user->last_name;
    echo "<" . $first_name2 . $last_name2 . ">";
    //returns: <>

According to the codex, this should work, but when I echo $first_nameor $last_namethey're empty. Strangely, THISdoes work:

根据codex,这应该有效,但是当我回显$first_name$last_name它们为空时。奇怪的是,确实有效:

    $id = $new_user->ID;

Am I doing something wrong?

难道我做错了什么?

UPDATE:

更新:

I var_dumped $new_userand those properties aren't in there! Is this because I'm calling it from inside a plugin in the /mu-plugins directory? Do those properties get added later?

var_dumped $new_user那些属性不在那里!这是因为我从 /mu-plugins 目录中的插件内部调用它吗?以后会添加这些属性吗?

object(WP_User)#334 (7) { ["data"]=> object(stdClass)#330 (10) { ["ID"]=> string(3) "758" ["user_login"]=> string(7) "emerson" ["user_pass"]=> string(34) "$P$BB2PuvRbyGUSVZR1M8FLSujPvMO2MW0" ["user_nicename"]=> string(7) "emerson" ["user_email"]=> string(16) "[email protected]" ["user_url"]=> string(0) "" ["user_registered"]=> string(19) "2012-08-17 01:03:27" ["user_activation_key"]=> string(0) "" ["user_status"]=> string(1) "0" ["display_name"]=> string(7) "emerson" } ["ID"]=> int(758) ["caps"]=> array(1) { ["subscriber"]=> string(1) "1" } ["cap_key"]=> string(15) "wp_capabilities" ["roles"]=> array(1) { [0]=> string(10) "subscriber" } ["allcaps"]=> array(15) { ["read"]=> bool(true) ["level_0"]=> bool(true) ["read_questions"]=> bool(true) ["read_answers"]=> bool(true) ["publish_questions"]=> bool(true) ["immediately_publish_questions"]=> bool(true) ["publish_answers"]=> bool(true) ["read_private_forums"]=> bool(true) ["publish_topics"]=> bool(true) ["edit_topics"]=> bool(true) ["publish_replies"]=> bool(true) ["edit_replies"]=> bool(true) ["assign_topic_tags"]=> bool(true) ["access_s2member_level0"]=> bool(true) ["subscriber"]=> string(1) "1" } ["filter"]=> NULL } 

UPDATE2:

更新2:

I tried this:

我试过这个:

$user_meta = get_user_meta( $new_user->ID );
var_dump($user_meta);

and I got this (last_name and first_name are empty even though they're defined in the user's profile):

我得到了这个(last_name 和 first_name 是空的,即使它们是在用户的配置文件中定义的):

array(11) { ["wp_user_level"]=> array(1) { [0]=> string(1) "0" } ["show_admin_bar_front"]=> array(1) { [0]=> string(4) "true" } ["wp_capabilities"]=> array(1) { [0]=> string(32) "a:1:{s:10:"subscriber";s:1:"1";}" } ["use_ssl"]=> array(1) { [0]=> string(1) "0" } ["admin_color"]=> array(1) { [0]=> string(5) "fresh" } ["comment_shortcuts"]=> array(1) { [0]=> string(5) "false" } ["rich_editing"]=> array(1) { [0]=> string(4) "true" } ["description"]=> array(1) { [0]=> string(0) "" } ["nickname"]=> array(1) { [0]=> string(7) "emerson" } ["last_name"]=> array(1) { [0]=> string(0) "" } ["first_name"]=> array(1) { [0]=> string(0) "" } }

回答by Matthew Blancarte

The user's first and last names are stored within the user_meta.

用户的名字和姓氏存储在 user_meta 中。

http://codex.wordpress.org/Function_Reference/get_user_meta

http://codex.wordpress.org/Function_Reference/get_user_meta

So if you'd like to access all of that data, for example, try this:

因此,例如,如果您想访问所有这些数据,请尝试以下操作:

$user_meta = get_user_meta( $new_user->ID );

Or, if you'd like to access a single meta value, try this:

或者,如果您想访问单个元值,请尝试以下操作:

$last_name = get_user_meta( $new_user->ID, 'last_name', true );

回答by arooaroo

I think you're the same person who asked something similar questionin the Wordpress StackExchange.

我认为您是在 Wordpress StackExchange 中提出类似问题的同一个人。

It's not an answer per se, but an explanation seems to be that there's a bug in the WP platform itself when obtaining these meta fields when called from mu-plugins code.

这本身不是一个答案,但一种解释似乎是在从 mu-plugins 代码调用时获取这些元字段时,WP 平台本身存在一个错误。

I've recently experienced this myself and I had some code in my functions.php that was called on the user_register hook. When that hook was defined in mu-plugins I got the issue you report. When that hook was defined in my functions.php it worked fine.

我最近自己经历过这个,我的functions.php中有一些代码在user_register钩子上被调用。当在 mu-plugins 中定义该钩子时,我收到了您报告的问题。当在我的functions.php 中定义该钩子时,它工作正常。

回答by Jazzpaths

The data you're looking for are already within the WP_USER object the function get_userdata() returns to you. To access user's first and last name just do as follow:

您要查找的数据已经在函数 get_userdata() 返回给您的 WP_USER 对象中。要访问用户的名字和姓氏,请执行以下操作:

# Retrieve the user's data (here we suppose, of course, the user exists)
$new_user = get_userdata( $user_id );
# Get the user's first and last name
$first_name = $new_user->first_name;
$last_name = $new_user->last_name;

Reference: https://developer.wordpress.org/reference/classes/wp_user/

参考:https: //developer.wordpress.org/reference/classes/wp_user/