ifacethoughts

current_user

The Wordpress current user information is available through this global variable. It is an instance of class WP_User defined in [wp root folder]/wp-includes/capabilities.php. It is set in the pluggable function set_current_user($id, $name='') and get_currentuserinfo() in [wp root folder]/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: , ,

Discussion [Participate or Link]

  1. Abhijit Nadgouda @ iface » Wordpress Global Variables said:

    [...] $current_user [...]

  2. Sumanth Krishna said:

    Thanks a lottttttttttt

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

    Thanks,

  3. Mark Jaquith said:

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

Say your thought!

Who are you?

If you want to use HTML you can use these tags: <a>, <em>, <strong>, <abbr>, <code>, <blockquote>. Closing the tags will be appreciated as this site uses valid XHTML.

freshthoughts

freshcomments

personalfavorites

contactme

Abhijit Nadgouda
iface Consulting
India
+91 9819820312
Y!: anadgouda
GTalk: anadgouda@gmail.com
MSN: anadgouda@hotmail.com
Skype: anadgouda

currentproject

Complete Wellbeing

thoughtfulthoughts

A language that doesn't affect the way you think about programming is not worth knowing
Alan Perlis

badgesand...

This is the weblog of Abhijit Nadgouda where he writes down his thoughts on software development and related topics. You are invited to subscribe to the feed to stay updated or check out more subscription options. Or you can choose to browse by one of the topics.

Twitter - Couchdb can be a perfect fit for a lot of publications.