2013-11-09

Notes: clarify magic __get's, discard WP version 3.2 cite.

← Older revision

Revision as of 15:41, 9 November 2013

(4 intermediate revisions not shown)

Line 5:

Line 5:

 

== Description ==

 

== Description ==

 

 



Returns a [[Class_Reference/WP_User|WP_User]] object with the information pertaining to the user whose ID is passed to it. The object property <tt>data</tt> maps directly to the wp_users table in the database (see [[Database Description#Table:_wp_users|Database Description]]), while the other properties returned are <tt>ID</tt>, the display <tt>filter</tt>, and various [[Roles and Capabilities]] fields.

+

Returns a [[Class_Reference/WP_User|WP_User]] object with the information pertaining to the user whose ID is passed to it. Properties de-referenced with "->" map directly to wp_users and wp_usermeta tables in the database (see [[Database Description#Table:_wp_users|Database Description]]).

 

 

 

If the user does not exist, the function returns <code>false</code>.

 

If the user does not exist, the function returns <code>false</code>.

Line 24:

Line 24:

 

== Examples ==

 

== Examples ==

 

 



=== Default Usage ===

+

=== Basic Usage ===

 

 



<tt>get_userdata()</tt> returns an object of the user's data that you can assign to a variable. After that, you can echo various parts of the returned object or loop through the data to display it all.

+

The <tt>get_userdata()</tt> function returns an object of the user's data. You can echo various parts of the returned object or loop through the data to display it all.

 

 

 

Example displaying certain parts:

 

Example displaying certain parts:

Line 32:

Line 32:

 

<?php $user_info = get_userdata(1);

 

<?php $user_info = get_userdata(1);

 

echo 'Username: ' . $user_info->user_login . "\n";

 

echo 'Username: ' . $user_info->user_login . "\n";



echo 'User level: ' . $user_info->user_level . "\n";

+

echo 'User roles: ' . implode(', ', $user_info->roles) . "\n";

 

echo 'User ID: ' . $user_info->ID . "\n";

 

echo 'User ID: ' . $user_info->ID . "\n";

 

?>

 

?>

 

 



Results in:

+

Results in: <div style="border:1px solid lightblue; display_inline-block; margin: 1em; padding: 1em;">Username: admin<br />User roles: administrator<br />User ID: 1</div>



<div style="border:1px solid blue; width:50%; padding:10px">Username: admin<br />User level: 10<br />User ID: 1</div>

 

 

 

 

You can also assign certain parts into individual variables for displaying later or in multiple places.

 

You can also assign certain parts into individual variables for displaying later or in multiple places.

Line 44:

Line 44:

 

<?php $user_info = get_userdata(1);

 

<?php $user_info = get_userdata(1);

 

$username = $user_info->user_login;

 

$username = $user_info->user_login;



$first_name = $user_info-> user_firstname;

+

$first_name = $user_info->first_name;



$last_name = $user_info-> user_lastname;

+

$last_name = $user_info->last_name;

 

echo "$first_name $last_name logs into her WordPress site with the user name of $username.";

 

echo "$first_name $last_name logs into her WordPress site with the user name of $username.";

 

?>

 

?>

 

 



Results in:

+

Results in: <div style="border:1px solid lightblue; display_inline-block; margin: 1em; padding: 1em;">Harriet Smith logs into her WordPress site with the user name of mrssmith.</div>



<div style="border:1px solid blue; width:50%; padding:10px">Harriet Smith logs into her WordPress site with the user name of mrssmith.</div>

 

 

 

 

=== Accessing Usermeta Data ===

 

=== Accessing Usermeta Data ===

 

 

 

<?php $user_info = get_userdata(1);

 

<?php $user_info = get_userdata(1);



echo $user_info-> user_lastname . ", " . $user_info-> user_firstname . "\n";

+

echo $user_info->last_name . ", " . $user_info->first_name . "\n";

 

?>

 

?>

 

 



<div style="border:1px solid blue; width:50%; padding:10px">

+

Results in: <div style="border:1px solid lightblue; display_inline-block; margin: 1em; padding: 1em;">Doe, John</div>



Doe, John<br />

 



</div>

 

 

 

 

== Notes ==

 

== Notes ==

Line 83:

Line 83:

 

** source_domain

 

** source_domain

 

 



Note that after wp 3.2, the return value has changed, a WP_User object is returned instead of a dumb object. However, through the usage of magic methods, data fields (such as the meta key rich_editing), get_userdata(1)->rich_editing will map to get_userdata(1)->data->rich_editing, even though in a var_dump() or similar will not show these extra fields. Refer http://core.trac.wordpress.org/ticket/18551.

+

<b>Note:</b> the WP_User object uses PHP 5 "magic" methods to provide some of its properties. For example:<br/>

 

+

<tt>$user_info->user_login</tt> is shorthand for <tt>$user_info->data->user_login</tt>, and<br/>

 

+

<tt>$user_info->rich_editing</tt> is shorthand for <tt>get_user_meta($user_info->ID, 'rich_editing', true)</tt>.

 

 

 

== Source File ==

 

== Source File ==

Show more