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

Aug 13 2012

[Infographic] WPBeginner Which Platform?

WPBeginner Which Platform?
WPBeginner Which Platform? Click for Larger Image

Written by Travis Smith · Categorized: Infographic, WordPress

Aug 12 2012

Genesis Post Class Patch

Recently, I was asked about a fix for the Genesis Post Class not saving. Here is a patch that will stop executing when Genesis upgrades to 1.9.0 which will re-write this section of the code.

This code does assume WordPress 3.4.x which uses wp_get_theme() instead of get_theme_data(). Please use accordingly in functions.php.

Written by Travis Smith · Categorized: Genesis

Aug 06 2012

[Infographic] BloggingPro WordPress History

BloggingPro WordPress History
BloggingPro WordPress History Click for Larger Image

Written by Travis Smith · Categorized: Infographic, WordPress

Aug 01 2012

Bail Function for save_post Hook

I am often using (and see others using) a set of code, that I see constantly and almost everywhere. Usually on save_post, many people bail out of the function if it's doing auto-save or ajax or cron. So you see a lot of copy-paste in these functions.
[php]
/** Bail out if running an autosave, ajax or a cron */
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;
if ( defined( 'DOING_AJAX' ) && DOING_AJAX )
return;
if ( defined( 'DOING_CRON' ) && DOING_CRON )
return;
[/php]

However, I turned this into a function:

Please let me know what you think!!

Written by Travis Smith · Categorized: WordPress

  • « Previous Page
  • 1
  • …
  • 13
  • 14
  • 15
  • 16
  • 17
  • …
  • 60
  • Next Page »

Need Help?

Please let us know how we can help you!

Get Help

Recommendations

Genesis WordPress Framework
Sucuri Security
Gravity Forms
GetSoliloquy
Get Envira
Scribe SEO
BackupBuddy
WordPress Video User Manuals

Recent Posts

  • Solving WordPress 5XX Server Errors on SiteGround
  • Hiding an User in the WordPress Admin
  • Custom Rewrite Rules for Custom Post Types and Taxonomies
  • WordPress JavaScript Manager Native Functions
  • Causes of WordPress Site Performance Slowdown

About Travis

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.

  • Twitter
  • Facebook
  • LinkedIn
  • Google+
  • RSS

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