Gets the current user’s ID.
Return
int The current user’s ID, or 0 if no user is logged in.Source
function get_current_user_id() {
if ( ! function_exists( 'wp_get_current_user' ) ) {
return 0;
}
$user = wp_get_current_user();
return ( isset( $user->ID ) ? (int) $user->ID : 0 );
}
Related
Uses | Description |
---|---|
wp_get_current_user()wp-includes/pluggable.php | Retrieves the current user object. |
Used by | Description |
---|---|
inject_ignored_hooked_blocks_metadata_attributes()wp-includes/block-template-utils.php | Inject ignoredHookedBlocks metadata attributes into a template or template part. |
build_comment_query_vars_from_block()wp-includes/blocks.php | Helper function that constructs a comment query vars array from the passed block properties. |
WP_REST_Templates_Controller::prepare_item_for_database()wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php | Prepares a single template for create or update. |
WP_REST_Application_Passwords_Controller::get_current_item_permissions_check()wp-includes/rest-api/endpoints/class-wp-rest-application-passwords-controller.php | Checks if a given request has access to get the currently used application password for a user. |
WP_REST_Autosaves_Controller::create_item()wp-includes/rest-api/endpoints/class-wp-rest-autosaves-controller.php | Creates, updates or deletes an autosave revision. |
WP_REST_Autosaves_Controller::create_post_autosave()wp-includes/rest-api/endpoints/class-wp-rest-autosaves-controller.php | Creates autosave for the specified post. |
wp_default_packages_inline_scripts()wp-includes/script-loader.php | Adds inline scripts required for the WordPress JavaScript packages. |
do_block_editor_incompatible_meta_box()wp-admin/includes/template.php | Renders a “fake” meta box with an information message, shown on the block editor, when an incompatible meta box is found. |
the_block_editor_meta_boxes()wp-admin/includes/post.php | Renders the meta boxes forms. |
register_and_do_post_meta_boxes()wp-admin/includes/meta-boxes.php | Registers the default post meta boxes, and runs the |
WP_Customize_Manager::handle_dismiss_autosave_or_lock_request()wp-includes/class-wp-customize-manager.php | Deletes a given auto-draft changeset or the autosave revision for a given changeset or delete changeset lock. |
WP_Customize_Manager::handle_changeset_trash_request()wp-includes/class-wp-customize-manager.php | Handles request to trash a changeset. |
WP_Customize_Manager::set_changeset_lock()wp-includes/class-wp-customize-manager.php | Marks the changeset post as being currently edited by the current user. |
WP_Customize_Manager::refresh_changeset_lock()wp-includes/class-wp-customize-manager.php | Refreshes changeset lock with the current time if current user edited the changeset before. |
WP_Customize_Manager::get_changeset_posts()wp-includes/class-wp-customize-manager.php | Gets changeset posts. |
update_network_option_new_admin_email()wp-includes/ms-functions.php | Sends a confirmation request email when a change of network admin email address is attempted. |
wp_localize_community_events()wp-includes/script-loader.php | Localizes community events data that needs to be passed to dashboard.js. |
WP_Widget_Text::render_control_template_scripts()wp-includes/widgets/class-wp-widget-text.php | Renders form template scripts. |
wp_ajax_get_community_events()wp-admin/includes/ajax-actions.php | Handles Ajax requests for community events |
WP_Customize_Manager::_publish_changeset_values()wp-includes/class-wp-customize-manager.php | Publishes the values of a changeset. |
WP_Customize_Manager::save_changeset_post()wp-includes/class-wp-customize-manager.php | Saves the post for the loaded changeset. |
WP_Customize_Manager::changeset_data()wp-includes/class-wp-customize-manager.php | Gets changeset data. |
WP_REST_Users_Controller::delete_current_item()wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php | Deletes the current user. |
WP_REST_Users_Controller::check_role_update()wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php | Determines if the current user is allowed to make the desired roles change. |
WP_REST_Users_Controller::update_current_item_permissions_check()wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php | Checks if a given request has access to update the current user. |
WP_REST_Users_Controller::update_current_item()wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php | Updates the current user. |
WP_REST_Users_Controller::delete_current_item_permissions_check()wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php | Checks if a given request has access to delete the current user. |
WP_REST_Users_Controller::get_current_item()wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php | Retrieves the current user. |
WP_REST_Users_Controller::get_item_permissions_check()wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php | Checks if a given request has access to read a user. |
WP_REST_Posts_Controller::prepare_item_for_database()wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php | Prepares a single post for create or update. |
WP_REST_Posts_Controller::update_item_permissions_check()wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php | Checks if a given request has access to update a post. |
WP_REST_Posts_Controller::create_item_permissions_check()wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php | Checks if a given request has access to create a post. |
WP_REST_Comments_Controller::check_edit_permission()wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php | Checks if a comment can be edited or deleted. |
WP_REST_Comments_Controller::check_read_permission()wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php | Checks if the comment can be read. |
WP_REST_Comments_Controller::create_item_permissions_check()wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php | Checks if a given request has access to create a comment. |
wp_check_comment_flood()wp-includes/comment.php | Checks whether comment flooding is occurring. |
WP_Customize_Selective_Refresh::export_preview_data()wp-includes/customize/class-wp-customize-selective-refresh.php | Exports data in preview after it has finished rendering so that partials can be added at runtime. |
WP_Customize_Manager::customize_pane_settings()wp-includes/class-wp-customize-manager.php | Prints JavaScript settings for parent window. |
WP_Screen::render_meta_boxes_preferences()wp-admin/includes/class-wp-screen.php | Renders the meta boxes preferences. |
wp_ajax_save_wporg_username()wp-admin/includes/ajax-actions.php | Handles saving the user’s WordPress.org username via AJAX. |
WP_Customize_Nav_Menu_Item_Setting::value_as_wp_post_nav_menu_item()wp-includes/customize/class-wp-customize-nav-menu-item-setting.php | Get the value emulated into a WP_Post and set up as a nav_menu_item. |
wp_edit_attachments_query_vars()wp-admin/includes/post.php | Returns the query variables for the current attachments request. |
wp_ajax_destroy_sessions()wp-admin/includes/ajax-actions.php | Handles destroying multiple open sessions for a user via AJAX. |
wp_destroy_all_sessions()wp-includes/user.php | Removes all session tokens for the current user from the database. |
wp_get_all_sessions()wp-includes/user.php | Retrieves a list of sessions for the current user. |
wp_destroy_current_session()wp-includes/user.php | Removes the current session token from the database. |
wp_destroy_other_sessions()wp-includes/user.php | Removes all but the current session token for the current user for the database. |
_access_denied_splash()wp-admin/includes/ms.php | Displays an access denied message when a user tries to view a site’s dashboard they do not have access to. |
choose_primary_blog()wp-admin/includes/ms.php | Handles the display of choosing a user’s primary site. |
update_option_new_admin_email()wp-admin/includes/misc.php | Sends a confirmation request email when a change of site admin email address is attempted. |
new_user_email_admin_notice()wp-includes/user.php | Adds an admin notice alerting the user to check for confirmation request email after email address change. |
install_plugins_favorites_form()wp-admin/includes/plugin-install.php | Shows a username form for the favorites page. |
wp_dashboard_quick_press()wp-admin/includes/dashboard.php | Displays the Quick Draft widget. |
wp_dashboard_recent_drafts()wp-admin/includes/dashboard.php | Show recent drafts of the user on the dashboard. |
edit_user()wp-admin/includes/user.php | Edit user settings based on contents of $_POST |
WP_Plugin_Install_List_Table::prepare_items()wp-admin/includes/class-wp-plugin-install-list-table.php | |
WP_Internal_Pointers::enqueue_scripts()wp-admin/includes/class-wp-internal-pointers.php | Initializes the new feature pointers. |
WP_Users_List_Table::single_row()wp-admin/includes/class-wp-users-list-table.php | Generates HTML for a single row on the users.php admin panel. |
wp_check_post_lock()wp-admin/includes/post.php | Determines whether the post is currently being edited by another user. |
wp_set_post_lock()wp-admin/includes/post.php | Marks the post as currently being edited by the current user. |
wp_create_post_autosave()wp-admin/includes/post.php | Creates autosave data for the specified post from |
post_preview()wp-admin/includes/post.php | Saves a draft or manually autosaves for the purpose of showing a post preview. |
wp_autosave()wp-admin/includes/post.php | Saves a post submitted with XHR. |
_wp_translate_postdata()wp-admin/includes/post.php | Renames |
edit_post()wp-admin/includes/post.php | Updates an existing post with values provided in |
bulk_edit_posts()wp-admin/includes/post.php | Processes the post data for the bulk editing of posts. |
wp_ajax_save_user_color_scheme()wp-admin/includes/ajax-actions.php | Handles auto-saving the selected color scheme for a user’s own profile via AJAX. |
wp_ajax_wp_remove_post_lock()wp-admin/includes/ajax-actions.php | Handles removing a post lock via AJAX. |
wp_ajax_dismiss_wp_pointer()wp-admin/includes/ajax-actions.php | Handles dismissing a WordPress pointer via AJAX. |
wp_ajax_update_welcome_panel()wp-admin/includes/ajax-actions.php | Handles updating whether to display the welcome panel via AJAX. |
wp_ajax_inline_save()wp-admin/includes/ajax-actions.php | Handles Quick Edit saving a post from a list table via AJAX. |
wp_insert_link()wp-admin/includes/bookmark.php | Inserts a link into the database, or updates an existing link. |
WP_Media_List_Table::display_rows()wp-admin/includes/class-wp-media-list-table.php | |
WP_Comments_List_Table::get_views()wp-admin/includes/class-wp-comments-list-table.php | |
WP_Posts_List_Table::single_row()wp-admin/includes/class-wp-posts-list-table.php | |
WP_Posts_List_Table::get_views()wp-admin/includes/class-wp-posts-list-table.php | |
WP_Posts_List_Table::__construct()wp-admin/includes/class-wp-posts-list-table.php | Constructor. |
WP_Posts_List_Table::prepare_items()wp-admin/includes/class-wp-posts-list-table.php | |
WP_Customize_Manager::customize_preview_settings()wp-includes/class-wp-customize-manager.php | Prints JavaScript settings for preview frame. |
wp_notify_postauthor()wp-includes/pluggable.php | Notifies an author (and/or others) of a comment/trackback/pingback on a post. |
wp_clear_auth_cookie()wp-includes/pluggable.php | Removes all of the cookies associated with authentication. |
wp_logout()wp-includes/pluggable.php | Logs the current user out. |
wp_admin_bar_dashboard_view_site_menu()wp-includes/deprecated.php | Add the “Dashboard”/”Visit Site” menu. |
is_blog_user()wp-includes/deprecated.php | Checks if the current user belong to a given site. |
WP_Query::get_posts()wp-includes/class-wp-query.php | Retrieves an array of posts based on query variables. |
get_dashboard_url()wp-includes/link-template.php | Retrieves the URL to the user’s dashboard. |
get_edit_profile_url()wp-includes/link-template.php | Retrieves the URL to the user’s profile editor. |
get_edit_user_link()wp-includes/link-template.php | Retrieves the edit user link. |
get_adjacent_post()wp-includes/link-template.php | Retrieves the adjacent post. |
WP_Admin_Bar::initialize()wp-includes/class-wp-admin-bar.php | Initializes the admin bar. |
wp_admin_bar_wp_menu()wp-includes/admin-bar.php | Adds the WordPress logo menu. |
wp_admin_bar_my_account_item()wp-includes/admin-bar.php | Adds the “My Account” item. |
wp_admin_bar_my_account_menu()wp-includes/admin-bar.php | Adds the “My Account” submenu items. |
wp_user_settings()wp-includes/option.php | Saves and restores user interface settings stored in a cookie. |
get_all_user_settings()wp-includes/option.php | Retrieves all user interface settings. |
wp_set_all_user_settings()wp-includes/option.php | Private. Sets all user interface settings. |
delete_all_user_settings()wp-includes/option.php | Deletes the user settings of the current user. |
is_user_member_of_blog()wp-includes/user.php | Finds out whether a user is a member of a given blog. |
setup_userdata()wp-includes/user.php | Sets up global user vars. |
get_user_option()wp-includes/user.php | Retrieves user option that can be either per Site or per Network. |
get_posts_by_author_sql()wp-includes/post.php | Retrieves the post SQL based on capability, author, and type. |
wp_insert_post()wp-includes/post.php | Inserts or update a post. |
_count_posts_cache_key()wp-includes/post.php | Returns the cache key for wp_count_posts() based on the passed arguments. |
wp_count_posts()wp-includes/post.php | Counts number of posts of a post type and if user has permissions to view. |
wp_restore_post_revision()wp-includes/revision.php | Restores a post to the specified revision. |
is_site_admin()wp-includes/ms-deprecated.php | Determine if user is a site admin. |
wp_list_comments()wp-includes/comment-template.php | Displays a list of comments. |
comments_template()wp-includes/comment-template.php | Loads the comment template specified in $file. |
WP_Customize_Widgets::export_preview_data()wp-includes/class-wp-customize-widgets.php | 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_default_scripts()wp-includes/script-loader.php | Registers all WordPress scripts. |
get_page_of_comment()wp-includes/comment.php | Calculates what page number a comment will appear on for comment paging. |
Changelog
Version | Description |
---|---|
MU (3.0.0) | Introduced. |
daniyalahmedk’s example is redundant.
The code below is the exact same thing but with less overhead.
Get current user role by ID :
There is no need to invoke a new WP_User object.
Before the plugins_loaded action it returns 0.
Get current user ID, if the user is logged in.
$user = wp_get_current_user(); $user_id = 0; if ( $user ) { $user_id = (int) $user->ID(); }
Or even simpler$user = wp_get_current_user(); $user_id = isset( $user->ID ) ? (int) $user->ID : 0; // If $user_id is 0 then user not logged in, else they are.
This function is not means for retrieving the current logged in user as it been made abundently clear some of the examples so far.
It is however very useful for validation of existing users’ emails, consider the following example,
in this case a form is used to allow users to change their registered email, and one can validate the change by making sure that the new submitted email is not already in use and does not belong to the current user.
Inside a plugin, if you need to retrieve the current user_id, at least use it inside the init hook.
Example:
Get current user role by ID :
SoN9ne is right, but as we are in get_current_user_id‘s documentation make senses that example (even just for knowledge).
This way we can query for draft posts of a current user: