current_user

The WordPress current user information is available through this global variable. It is an instance of class WP_User defined in /wp-includes/capabilities.php. It is set in the pluggable function set_current_user($id, $name='') and get_currentuserinfo() in /wp-includes/pluggable-functions.php.

The class WP_User has methods for role and capabilities of the user.

  • WP_User($id, $name = ''): the constructor, it can be used to instantiate a user with specific user ID.
  • get_role_caps(): gets capabilities
  • add_role($role): adds specified role to the user
  • remove_role($role): removes specified role
  • set_role($role): sets role
  • level_reduction($max, $item): reduces level
  • update_user_level_from_caps(): translates from capabilities to user levels
  • add_cap($cap, $grant = true): adds capabilities
  • remove_cap($cap): removes capabilities
  • has_cap($cap): queries for a specific capability
  • translate_level_to_cap($level): translates from level to capabilities

User ID of the current user can be accessed using $current_user->id field. Profile information of the current user can be obtained by using by using the member variable $current_user->data, but other global variables are available like $user_login, $user_level, $user_ID, $user_email, $user_url, $user_pass_md5 and $user_identity for this information.

Plugin and theme authors can use this global variable to get information about the current user. Most commonly required information would be about what the current user can do. WordPress provides the function current_user_can($capability) for finding out capabilities of the user. The different capabilities are the different actions a user can take, they are:

  • switch_themes
  • edit_themes
  • activate_plugins
  • edit_plugins
  • edit_users
  • edit_files
  • manage_options
  • moderate_comments
  • manage_categories
  • manage_links
  • upload_files
  • import
  • unfiltered_html
  • edit_posts
  • edit_others_posts
  • edit_published
  • publish_posts
  • edit_pages
  • read
  • level_10
  • level_9
  • level_8
  • level_7
  • level_6
  • level_5
  • level_4
  • level_3
  • level_2
  • level_1
  • level_0

Update

As mentioned by Mark Jaquith in the comments, level_* are deprecated and are available only for backward compatibility. New plugins and themes should not use them. The methods concerned with levels should also be avoided.

There are some more functions available like user_can_edit_post and similar, but they are deprecated. current_user_can should be used instead of these.

Back to full list of global variables.

Technorati tags: , ,

3 Responses to “current_user”

  1. Sumanth Krishna Says:

    Thanks a lottttttttttt

    I am new to WP and benefited from your post, regarding the display of author’s name.

    Thanks,

  2. Mark Jaquith Says:

    Very nice. Just to be clear, the level_ ones are for backwards compatability only and should not be used for new code.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: