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":
- 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] - 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] - 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:
- *Get user data:
$user->get_data_by( $field, $value );
// Available fields: 'id', 'slug', 'email' or 'login' - **Check if user exists:
$user->exists();
- *Retrieve the value of a property or meta key:
$user->get();
- *Determine whether a property or meta key is set:
$user->has_prop();
- Retrieve all the role capabilities merged with the individual capabilities:
$user->get_role_caps();
- Add role to a user:
$user->add_role( $role );
- Remove role from a user:
$user->remove_role( $role );
- Set role for a user:
$user->set_role( $role );
- Choose the maximum level the user has:
$user->level_reduction( $max, $item );
- Update the maximum user level for the user:
$user->update_user_level_from_caps();
- Add cap to a user:
$user->add_cap( $cap, $grant );
- Remove cap from a user:
$user->remove_cap( $cap );
- Remove all caps from a user:
$user->remove_all_caps();
- Determine whether user has a cap:
$user->has_cap( $cap, $grant );
- Set blog to operate on:
$user->for_blog( $blog_id ); // $blog_id
defaults to current blog
*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 protected]
[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.