Site icon WP Smith

An Introduction to WP_User Class

WP_User can be found in root/wp-includes/capabilities.php. The constructor takes 3 parameters: $id (int|string::ID|login), $name (string), and $blog_id (int).

You can instantiate a specific user using the class:
[php]<?php
$user = new WP_User( $id [, $name [, $blog_id ] ] );
?>
[/php]

For example:
[php]<?php
$user = new WP_User( 2 );
?>
[/php]

OR, you can get the current user:
[php]<?php
$current_user = wp_get_current_user();
?>
[/php]

While there are normal functions like (e.g., get_the_author_meta() which uses get_userdata() which uses get_user_by() which is new in 3.3.0 and uses WP_User). WP_User has three "magic methods":

  1. WP_User::__isset( $key ), which is the same as WP_User::has_prop( $key ).
    Example:
    [php]<?php
    $user = new WP_User( $id );
    if ( $user->__isset( 'myfield' ) )
    // do something
    [/php]
  2. WP_User::__get( $key ), which is the same as WP_User::get( $key ).
    Example:
    [php]<?php
    $user = new WP_User( $id );
    if ( $user->__isset( 'myfield' ) )
    $myfield = $user->__get( 'myfield' );
    [/php]
  3. WP_User::__set( $key ).
    Example:
    [php]<?php
    $user = new WP_User( $id );
    if ( ! $user->__isset( 'myfield' ) )
    $myfield = $user->__set( 'myfield', $value );
    [/php]

Here are some other methods:

*NEW in 3.3
**NEW in 3.4

Here is the object:
[php]
WP_User Object
(
[data] => stdClass Object
(
[ID] => 1
[user_login] => myloginusername
[user_pass] => xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
[user_nicename] => My Nice Name
[user_email] => email@domain.com
[user_url] =>
[user_registered] => 2011-07-15 01:16:18
[user_activation_key] =>
[user_status] => 0
[display_name] => My Display Name
)

[ID] => 1
[caps] => Array
(
[administrator] => 1
)

[cap_key] => wp_capabilities
[roles] => Array
(
[0] => administrator
)

[allcaps] => Array
(
[switch_themes] => 1
[edit_themes] => 1
[activate_plugins] => 1
[edit_plugins] => 1
[edit_users] => 1
[edit_files] => 1
[manage_options] => 1
[moderate_comments] => 1
[manage_categories] => 1
[manage_links] => 1
[upload_files] => 1
[import] => 1
[unfiltered_html] => 1
[edit_posts] => 1
[edit_others_posts] => 1
[edit_published_posts] => 1
[publish_posts] => 1
[edit_pages] => 1
[read] => 1
[level_10] => 1
[level_9] => 1
[level_8] => 1
[level_7] => 1
[level_6] => 1
[level_5] => 1
[level_4] => 1
[level_3] => 1
[level_2] => 1
[level_1] => 1
[level_0] => 1
[edit_others_pages] => 1
[edit_published_pages] => 1
[publish_pages] => 1
[delete_pages] => 1
[delete_others_pages] => 1
[delete_published_pages] => 1
[delete_posts] => 1
[delete_others_posts] => 1
[delete_published_posts] => 1
[delete_private_posts] => 1
[edit_private_posts] => 1
[read_private_posts] => 1
[delete_private_pages] => 1
[edit_private_pages] => 1
[read_private_pages] => 1
[delete_users] => 1
[create_users] => 1
[unfiltered_upload] => 1
[edit_dashboard] => 1
[update_plugins] => 1
[delete_plugins] => 1
[install_plugins] => 1
[update_themes] => 1
[install_themes] => 1
[update_core] => 1
[list_users] => 1
[remove_users] => 1
[add_users] => 1
[promote_users] => 1
[edit_theme_options] => 1
[delete_themes] => 1
[export] => 1
[administrator] => 1
)

[filter] =>
)
[/php]

After writing this, I updated the WordPress Codex accordingly, which was missing all the 3.3.0 & 3.4.0 information.