Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

wordpress

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

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) "" } }
like image 835
emersonthis Avatar asked Aug 16 '12 21:08

emersonthis


3 Answers

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:

# 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/

like image 191
Jazzpaths Avatar answered Oct 26 '22 04:10

Jazzpaths


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 );
like image 40
Matthew Blancarte Avatar answered Oct 26 '22 03:10

Matthew Blancarte


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.

like image 35
arooaroo Avatar answered Oct 26 '22 04:10

arooaroo