I’m writing a basic plugin. Here’s my code:
$new_user = get_userdata($user_id);
$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_dump
ed $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 }
You have to use the function
get_user_meta
According to the same page you referred, those two lines should be like:
Please check this section.
Unfortunately I’ve just stumbled across the same issue and your theory that mu-plugin may be factor is entirely correct.
Funnily enough it’s not that the code itself is in a php file with mu-plugin, but rather that the call is initiated there. For example I had an “add_action” line in php file located in mu-plugins but did in fact call a function in my functions.php file. The result was I wasn’t seeing the first name and last name. I moved that one “add_action” line in to my functions.php and then I got the metadata as expected!
So it’s not so much an answer but I wanted to confirm that have to move this out of mu-plugins to get the desired result. I haven’t delved further into the WP code yet to see if there are any clues as to why this would be the case. I do intend on raising a bug report though.
PS I’ve been testing this on WP 3.4 and 3.4.2