Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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_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 } 
share|improve this question

3 Answers 3

According to the same page you referred, those two lines should be like:

$first_name = $new_user->first_name;
$last_name = $new_user->last_name;

Please check this section.

share|improve this answer
    
I tried that as well and it also didn't work. When I print out the variables I get nothing. –  SDP Aug 17 '12 at 0:48
    
I var_dumped the user object and for some reason none of those properties are in there. –  SDP Aug 17 '12 at 1:10
    
First make sure the user has something in those fields that means user has firstname and lastname fields filled. I've also seen those two properties are not listed when you var_dump the user object but they are clearly shown when you use the property var_dump($new_user->first_name); –  HungryCoder Aug 19 '12 at 11:02
    
No. var_dump($new_user->first_name) is an empty string. –  SDP Aug 19 '12 at 23:24
    
Also, I know those properties are filled in because I can see them in the user's profile in the admin screen. –  SDP Aug 19 '12 at 23:25

You have to use the function get_user_meta

$new_user = get_userdata($user_id);

// get all the meta data of the user
$new_user_data = get_user_meta( $user_id );

// get the first name of the user as a string
$new_user_firstname = get_user_meta( $user_id, 'first_name', true );
share|improve this answer

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.