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

匿名 (未验证) 提交于 2019-12-03 00:52:01

问题:

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_name or $last_name they're empty. Strangely, THIS does work:

    $id = $new_user->ID; 

Am I doing something wrong?

UPDATE:

I var_dumped $new_user and 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?

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) "123esl@gmail.com" ["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:

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):

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

回答1:

The user's first and last names are stored within the 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 ); 


回答2:

I think you're the same person who asked something similar question in the 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.

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.



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!