wp_get_current_user(): WP_User
- Since
- 2.0.3
- Source
wp-includes/pluggable.php:69
Fetches the WP_User instance representing whoever is currently logged in, setting the global current user first if it hasn't been initialized yet. If nobody is logged in, it returns a WP_User with ID 0, which has no capabilities and fails the usual logged-in checks. Use is_user_logged_in() or check the ->exists() method on the returned object before trusting its data, and reach for get_current_user_id() instead if all you need is the numeric ID.
Description
Will set the current user, if the current user is not set. The current user will be set to the logged-in person. If no user is logged-in, then it will set the current user to 0, which is invalid and won't have any permissions.
Compatibility
- WordPress
- since 2.0.3
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Return value
WP_User- Current WP_User instance.
Code examples
Every example is editable and runs in a real WordPress booted in your browser by WordPress Playground. Press Run, then edit the code: clicking away re-runs it. Nothing is sent anywhere until you do.
Show the display name and email of the logged-in user
Print a quick greeting using the current user's stored profile fields.
$current_user = wp_get_current_user();
if ( $current_user->exists() ) {
echo esc_html( 'Logged in as: ' . $current_user->display_name . ' (' . $current_user->user_email . ')' );
} else {
echo esc_html( 'No user is currently logged in.' );
}Check whether the logged-in user authored a given post
Compare the current user's ID against a post's author to decide whether to show an edit link.
$current_user = wp_get_current_user();
$post = get_post( 2 );
if ( $post && (int) $post->post_author === $current_user->ID ) {
echo esc_html( 'You wrote "' . $post->post_title . '", you can edit it.' );
} else {
echo esc_html( 'You did not author this post.' );
}The comparison assumes post_author is stored as a numeric string, so casting to int avoids a loose-comparison mismatch.
Common problems and fixes · 3
- Why does wp_get_current_user() give me a user object with ID 0?
- Why do I get a fatal error calling wp_get_current_user() from a must-use plugin or the top of a plugin file?
- Should I use wp_get_current_user() or get_current_user_id() to check who's logged in?
Why does wp_get_current_user() give me a user object with ID 0?
Why do I get a fatal error calling wp_get_current_user() from a must-use plugin or the top of a plugin file?
Should I use wp_get_current_user() or get_current_user_id() to check who's logged in?
Alternatives and related functions
get_current_user_id- When you only need the numeric user ID, not the full profile data.
is_user_logged_in- When all you need is a true or false answer about whether a visitor is authenticated.
get_userdata- When you need the WP_User object for a specific user ID rather than whoever is currently logged in.
current_user_can- When the goal is a capability check rather than reading properties off the user object yourself.
Performance profile
How much work a call to wp_get_current_user() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Trivial
- Scaling
- Constant
- Instructions
- 3
- Plugin surface
- None
- Called by
- 40
Touches nothing outside its own arguments.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5. The body compiles to 3.
Nothing here hands control to plugin code.
40 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()one call below wp_get_current_user()
Further down the call graph this can also reach query. That is the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 1 distinct outcome
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_get_current_user() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 3 | _wp_get_current_user() |
Across PHP versions
Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 3 instructions, 3 executed per call, 0 branches. The work does not change between versions.
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Uses · 1
- _wp_get_current_user()Retrieves the current user object.
Used by · 40
- WP::init()Set up the current user.
- WP_Customize_Manager::register_controls()Registers some default controls.
- WP_REST_Application_Passwords_Controller::get_user()Gets the requested user.
- WP_REST_Comments_Controller::create_item()Creates a comment.
- WP_REST_Users_Controller::get_current_item()Retrieves the current user.
- WP_Screen::render_meta_boxes_preferences()Renders the meta boxes preferences.
- WP_Widget_Custom_HTML::add_help_text()Add help text to widgets admin screen.
- comment_form()Outputs a complete commenting form for use within a template.
- confirm_delete_users()
- current_user_can()Returns whether the current user has the specified capability.
- get_current_user_id()Gets the current user's ID.
- get_user_locale()Retrieves the locale of a user.
Show all 40
- is_super_admin()Determines whether user is a site admin.
- is_user_logged_in()Determines whether the current visitor is a logged in user.
- is_user_option_local()Check whether a usermeta key has to do with the current blog.
- is_user_spammy()Determines whether a user is marked as a spammer, based on user login.
- populate_network_meta()Creates WordPress network meta and sets the default values.
- send_confirmation_on_profile_email()Sends a confirmation request email when a change of user email address is attempted.
- set_screen_options()Saves option for number of rows when listing posts, pages, comments, etc.
- signup_another_blog()Shows a form for returning users to sign up for another site.
- the_block_editor_meta_box_post_form_hidden_fields()Renders the hidden form required for the meta boxes form.
- update_network_option_new_admin_email()Sends a confirmation request email when a change of network admin email address is attempted.
- update_option_new_admin_email()Sends a confirmation request email when a change of site admin email address is attempted.
- validate_another_blog_signup()Validates a new site sign-up for an existing user.
- validate_blog_form()Validates the new site sign-up.
- wp_admin_bar_my_account_item()Adds the "My Account" item.
- wp_admin_bar_my_account_menu()Adds the "My Account" submenu items.
- wp_ajax_closed_postboxes()Handles closed post boxes via AJAX.
- wp_ajax_hidden_columns()Handles hidden columns via AJAX.
- wp_ajax_meta_box_order()Handles saving the meta box order via AJAX.
- wp_ajax_replyto_comment()Handles replying to a comment via AJAX.
- wp_create_nonce()Creates a cryptographic token tied to a specific action, user, user session, and window of time.
- wp_default_editor()Finds out which editor should be displayed by default.
- wp_enqueue_code_editor()Enqueues assets needed by the code editor for the given settings.
- wp_handle_comment_submission()Handles the submission of a comment, usually posted to wp-comments-post.php via a comment form.
- wp_initial_nav_menu_meta_boxes()Limit the amount of meta boxes to pages, posts, links, and categories for first time users.
- wp_nav_menu_setup()Register nav menu meta boxes and advanced menu items.
- wp_switch_roles_and_user()Switches the initialized roles and current user capabilities to another site.
- wp_update_user()Updates a user in the database.
- wp_verify_nonce()Verifies that a correct security nonce was used with time limit.
Source code
function wp_get_current_user() { return _wp_get_current_user(); }Changelog
Introduced in 2.0.3. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.8.8 tag, from
src/wp-includes/pluggable.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.