WP Smith

Creating WordPress & Genesis Websites Since 2010

  • Home
  • About
  • Services
  • Blog
  • Contact

Aug 15 2012

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:

  • *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.

Written by Travis Smith · Categorized: WordPress

StudioPress Premium WordPress Themes     WP Engine Managed WordPress Hosting

What can I do for you!?

Custom Development

We develop plugins by determining both business/functional and technical requirements, following WordPress development best practices, and using agile methodology to ensure you get the best solution.

Consulting

Have questions? Need a reliable developer to consult? Please contact us today!

Customized Theme

We can customize your theme or child theme, or create a child theme for you based on your needs while enhancing the performance of every individual attribute.

Customized Plugin

We can customize your plugins, extend plugins (e.g., Gravity Forms, Jetpack, Soliloquy) based on your needs ensuring security, performance, and positive business impact.

Contact Us

About Travis Smith

As a WordPress enthusiast, developer, and speaker, Travis writes about what he learns in WordPress trying to help other WordPress travelers, beginners and enthusiasts with tutorials, explanations, & demonstrations.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  • Twitter
  • Facebook
  • LinkedIn
  • Google+
  • RSS

Copyright © 2025 � WP Smith on Genesis on Genesis Framework � WordPress � Log in