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.

  4. Brad Goss said:

    Hello, is it possible to access these variables outside the wordpress directory? I have setup wordpress under /blog. And under my root folder I have setup a separate registration and login page, where the users can successfully register and login(I know they can login because you can login, goto the blog page and see that your logged in.)

    However when they login, the $current_user->ID returns 0 outside of a wordpress file. I try to display the other global values you’ve listed and they are all empty. Is there a step im missing to initiate the session or something?

  5. JD said:

    I know this is old, so you may not get this comment, but I have a question. Is the $current_user global variable automatically populated every time WordPress runs, or only when a plugin/theme calls get_currentuserinfo()? In other words, is this global always going to be set, or are there times when it wouldn’t be?

Say your thought!

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

contactme

Abhijit Nadgouda
iface Consulting
India
+91 9819820312
My bookmarks

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.