get_current_user_id(): int
- Since
- MU (3.0.0)
- Source
wp-includes/user.php:726
Returns the numeric ID of the currently logged-in user, or 0 when no one is signed in. It reads the value from wp_get_current_user() rather than querying the database directly, so it reflects whatever the global current user object holds at the moment it's called. Because 0 is a valid, non-error return for logged-out visitors, code that checks the result should not confuse it with a real error condition.
Compatibility
- WordPress
- since MU (3.0.0)
- 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
int- The current user's ID, or 0 if no user is logged in.
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 a personalized greeting only to logged-in users
Print a different message depending on whether the current visitor has an ID.
$user_id = get_current_user_id();
if ( $user_id > 0 ) {
echo esc_html( sprintf( 'Welcome back, user #%d.', $user_id ) );
} else {
echo esc_html( 'Welcome, guest. Log in to see your account.' );
}The sandbox has an administrator logged in, so this prints the welcome-back branch with a real ID.
Record which user last edited a post as post meta
Stamp an existing post with the ID of whoever is currently making the change, then read it back.
$post_id = 2;
$editor_id = get_current_user_id();
update_post_meta( $post_id, '_last_edited_by', $editor_id );
$stored = get_post_meta( $post_id, '_last_edited_by', true );
printf(
'Post %d was last touched by user #%s.',
absint( $post_id ),
esc_html( $stored )
);Common problems and fixes · 3
- Why does get_current_user_id() return 0 even though I know I'm logged in?
- Is checking if (get_current_user_id()) a safe way to test login status?
- Can I use the returned ID to decide if a user is allowed to do something?
Why does get_current_user_id() return 0 even though I know I'm logged in?
Is checking if (get_current_user_id()) a safe way to test login status?
Can I use the returned ID to decide if a user is allowed to do something?
Alternatives and related functions
wp_get_current_user- When you need the full user object (email, roles, display name) rather than just the numeric ID.
is_user_logged_in- When all you need is a true/false answer about login status, without the ID itself.
current_user_can- When the goal is to check whether the current user has a specific capability, not just identify who they are.
get_userdata- When you need data for a user other than the one currently logged in, given their ID.
Performance profile
How much work a call to get_current_user_id() 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
- 5–12
- Plugin surface
- None
- Called by
- 50
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, depending on the branch taken. The body compiles to 13.
Nothing here hands control to plugin code.
50 places in core call this, so the cost is paid more often than your own code shows.
What one call costs · 2 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost get_current_user_id() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!function_exists() | 5 | function_exists() |
function_exists() | 11–12 | function_exists(), 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: 13 instructions, 5–12 executed per call, 2 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 · 50
- WP_Admin_Bar::initialize()Initializes the admin bar.
- WP_Comments_List_Table::get_views()Returns an array of comment status links.
- WP_Customize_Manager::_publish_changeset_values()Publishes the values of a changeset.
- WP_Customize_Manager::changeset_data()Gets changeset data.
- WP_Customize_Manager::customize_pane_settings()Prints JavaScript settings for parent window.
- WP_Customize_Manager::customize_preview_settings()Prints JavaScript settings for preview frame.
- WP_Customize_Manager::get_changeset_posts()Gets changeset posts.
- WP_Customize_Manager::handle_changeset_trash_request()Handles request to trash a changeset.
- WP_Customize_Manager::handle_dismiss_autosave_or_lock_request()Deletes a given auto-draft changeset or the autosave revision for a given changeset or delete changeset lock.
- WP_Customize_Manager::refresh_changeset_lock()Refreshes changeset lock with the current time if current user edited the changeset before.
- WP_Customize_Manager::save_changeset_post()Saves the post for the loaded changeset.
- WP_Customize_Manager::set_changeset_lock()Marks the changeset post as being currently edited by the current user.
Show all 50
- WP_Customize_Nav_Menu_Item_Setting::value_as_wp_post_nav_menu_item()Get the value emulated into a WP_Post and set up as a nav_menu_item.
- WP_Customize_Selective_Refresh::export_preview_data()Exports data in preview after it has finished rendering so that partials can be added at runtime.
- WP_Customize_Widgets::export_preview_data()Communicates the sidebars that appeared on the page at the very end of the page, and at the very end of the wp_footer,
- WP_Internal_Pointers::enqueue_scripts()Initializes the new feature pointers.
- WP_Media_List_Table::display_rows()Generates the list table rows.
- WP_Plugin_Install_List_Table::prepare_items()
- WP_Posts_List_Table::__construct()Constructor.
- WP_Posts_List_Table::get_views()
- WP_Posts_List_Table::prepare_items()
- WP_Posts_List_Table::single_row()
- WP_Query::get_posts()Retrieves an array of posts based on query variables.
- WP_REST_Application_Passwords_Controller::get_current_item_permissions_check()Checks if a given request has access to get the currently used application password for a user.
- WP_REST_Autosaves_Controller::create_item()Creates, updates or deletes an autosave revision.
- WP_REST_Autosaves_Controller::create_post_autosave()Creates autosave for the specified post.
- WP_REST_Comments_Controller::check_edit_permission()Checks if a comment can be edited or deleted.
- WP_REST_Comments_Controller::check_read_permission()Checks if the comment can be read.
- WP_REST_Comments_Controller::create_item_permissions_check()Checks if a given request has access to create a comment.
- WP_REST_Posts_Controller::create_item_permissions_check()Checks if a given request has access to create a post.
- WP_REST_Posts_Controller::prepare_item_for_database()Prepares a single post for create or update.
- WP_REST_Posts_Controller::update_item_permissions_check()Checks if a given request has access to update a post.
- WP_REST_Templates_Controller::prepare_item_for_database()Prepares a single template for create or update.
- WP_REST_Users_Controller::check_role_update()Determines if the current user is allowed to make the desired roles change.
- WP_REST_Users_Controller::delete_current_item()Deletes the current user.
- WP_REST_Users_Controller::delete_current_item_permissions_check()Checks if a given request has access to delete the current user.
- WP_REST_Users_Controller::get_current_item()Retrieves the current user.
- WP_REST_Users_Controller::get_item_permissions_check()Checks if a given request has access to read a user.
- WP_REST_Users_Controller::update_current_item()Updates the current user.
- WP_REST_Users_Controller::update_current_item_permissions_check()Checks if a given request has access to update the current user.
- WP_Screen::render_meta_boxes_preferences()Renders the meta boxes preferences.
- WP_Users_List_Table::single_row()Generates HTML for a single row on the users.php admin panel.
- WP_Widget_Text::render_control_template_scripts()Renders form template scripts.
- _access_denied_splash()Displays an access denied message when a user tries to view a site's dashboard they do not have access to.
- _count_posts_cache_key()Returns the cache key for wp_count_posts() based on the passed arguments.
- _wp_translate_postdata()Renames `$_POST` data from form names to DB post columns.
- build_comment_query_vars_from_block()Helper function that constructs a comment query vars array from the passed block properties.
- bulk_edit_posts()Processes the post data for the bulk editing of posts.
- choose_primary_blog()Handles the display of choosing a user's primary site.
- comments_template()Loads the comment template specified in $file.
Source code
function get_current_user_id() { if ( ! function_exists( 'wp_get_current_user' ) ) { return 0; } $user = wp_get_current_user(); return (int) ( $user->ID ?? 0 );}Changelog
Introduced in MU (3.0.0). 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 7.1.0 tag, from
src/wp-includes/user.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.