我正在寫一個基本的插件。這裏是我的代碼:如何從WP_User對象獲取Wordpress用戶的名字?
$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: <>
根據the codex,這應該工作,但是當我贊同$first_name
或$last_name
他們是空的。奇怪的是,這做工作:
$id = $new_user->ID;
難道我做錯了什麼?
UPDATE:
我var_dump
編$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:
我嘗試這樣做:
$user_meta = get_user_meta($new_user->ID);
var_dump($user_meta);
,我得到這個(姓氏,即使他們在用戶的個人資料正在定義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) "" } }
var_dump($ new_user);您將看到該對象的具體內容和不可用內容。 – 2012-08-17 00:58:27
這些屬性丟失!我剛剛更新了var_dump的問題。我很困惑爲什麼這些屬性不存在。 – emersonthis 2012-08-17 01:09:17
您必須將ID傳遞給'get_user_meta'函數。 – inigomedina 2012-08-17 09:17:15