返回介绍

get_current_user_id()

发布于 2017-09-10 23:12:09 字数 9051 浏览 1177 评论 0 收藏 0

get_current_user_id()

Get the current user’s ID


description


返回值

(int) The current user's ID, or 0 if no user is logged in.


源代码

File: wp-includes/user.php

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 );
}

更新日志

Versiondescription
MUIntroduced.

相关函数

Uses

  • wp-includes/pluggable.php: wp_get_current_user()

Used By

  • wp-includes/script-loader.php: wp_localize_community_events()
  • wp-admin/includes/ajax-actions.php: wp_ajax_get_community_events()
  • wp-includes/class-wp-customize-manager.php: WP_Customize_Manager::_publish_changeset_values()
  • wp-includes/class-wp-customize-manager.php: WP_Customize_Manager::save_changeset_post()
  • wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php: WP_REST_Users_Controller::delete_current_item()
  • wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php: WP_REST_Users_Controller::check_role_update()
  • wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php: WP_REST_Users_Controller::update_current_item_permissions_check()
  • wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php: WP_REST_Users_Controller::update_current_item()
  • wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php: WP_REST_Users_Controller::delete_current_item_permissions_check()
  • wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php: WP_REST_Users_Controller::get_current_item()
  • wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php: WP_REST_Users_Controller::get_item_permissions_check()
  • wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php: WP_REST_Posts_Controller::prepare_item_for_database()
  • wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php: WP_REST_Posts_Controller::update_item_permissions_check()
  • wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php: WP_REST_Posts_Controller::create_item_permissions_check()
  • wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php: WP_REST_Comments_Controller::check_edit_permission()
  • wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php: WP_REST_Comments_Controller::check_read_permission()
  • wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php: WP_REST_Comments_Controller::create_item_permissions_check()
  • wp-includes/comment.php: wp_check_comment_flood()
  • wp-admin/includes/class-wp-screen.php: WP_Screen::render_meta_boxes_preferences()
  • wp-admin/includes/ajax-actions.php: wp_ajax_save_wporg_username()
  • wp-includes/customize/class-wp-customize-nav-menu-item-setting.php: WP_Customize_Nav_Menu_Item_Setting::value_as_wp_post_nav_menu_item()
  • wp-admin/includes/ajax-actions.php: wp_ajax_destroy_sessions()
  • wp-includes/user.php: wp_destroy_all_sessions()
  • wp-includes/user.php: wp_get_all_sessions()
  • wp-includes/user.php: wp_destroy_current_session()
  • wp-includes/user.php: wp_destroy_other_sessions()
  • wp-admin/includes/ms.php: _access_denied_splash()
  • wp-admin/includes/ms.php: choose_primary_blog()
  • wp-admin/includes/ms.php: new_user_email_admin_notice()
  • wp-admin/includes/plugin-install.php: install_plugins_favorites_form()
  • wp-admin/includes/dashboard.php: wp_dashboard_quick_press()
  • wp-admin/includes/dashboard.php: wp_dashboard_recent_drafts()
  • wp-admin/includes/user.php: edit_user()
  • wp-admin/includes/class-wp-plugin-install-list-table.php: WP_Plugin_Install_List_Table::prepare_items()
  • wp-admin/includes/class-wp-internal-pointers.php: WP_Internal_Pointers::enqueue_scripts()
  • wp-admin/includes/class-wp-users-list-table.php: WP_Users_List_Table::single_row()
  • wp-admin/includes/post.php: wp_check_post_lock()
  • wp-admin/includes/post.php: wp_set_post_lock()
  • wp-admin/includes/post.php: wp_create_post_autosave()
  • wp-admin/includes/post.php: post_preview()
  • wp-admin/includes/post.php: wp_autosave()
  • wp-admin/includes/ajax-actions.php: wp_ajax_save_user_color_scheme()
  • wp-admin/includes/post.php: _wp_translate_postdata()
  • wp-admin/includes/post.php: edit_post()
  • wp-admin/includes/ajax-actions.php: wp_ajax_wp_remove_post_lock()
  • wp-admin/includes/ajax-actions.php: wp_ajax_dismiss_wp_pointer()
  • wp-admin/includes/ajax-actions.php: wp_ajax_update_welcome_panel()
  • wp-admin/includes/ajax-actions.php: wp_ajax_inline_save()
  • wp-admin/includes/bookmark.php: wp_insert_link()
  • wp-admin/includes/class-wp-media-list-table.php: WP_Media_List_Table::display_rows()
  • wp-admin/includes/class-wp-posts-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: WP_Posts_List_Table::prepare_items()
  • wp-includes/capabilities.php: is_super_admin()
  • wp-includes/pluggable.php: wp_notify_postauthor()
  • wp-includes/pluggable.php: wp_clear_auth_cookie()
  • wp-includes/deprecated.php: wp_admin_bar_dashboard_view_site_menu()
  • wp-includes/deprecated.php: is_blog_user()
  • wp-includes/class-wp-query.php: WP_Query::get_posts()
  • wp-includes/link-template.php: get_dashboard_url()
  • wp-includes/link-template.php: get_edit_profile_url()
  • wp-includes/link-template.php: get_edit_user_link()
  • wp-includes/link-template.php: get_adjacent_post()
  • wp-includes/class-wp-admin-bar.php: WP_Admin_Bar::initialize()
  • wp-includes/admin-bar.php: wp_admin_bar_wp_menu()
  • wp-includes/admin-bar.php: wp_admin_bar_my_account_item()
  • wp-includes/admin-bar.php: wp_admin_bar_my_account_menu()
  • wp-includes/option.php: wp_user_settings()
  • wp-includes/option.php: get_all_user_settings()
  • wp-includes/option.php: wp_set_all_user_settings()
  • wp-includes/option.php: delete_all_user_settings()
  • wp-includes/user.php: is_user_member_of_blog()
  • wp-includes/user.php: setup_userdata()
  • wp-includes/user.php: get_user_option()
  • wp-includes/post.php: get_posts_by_author_sql()
  • wp-includes/post.php: wp_insert_post()
  • wp-includes/post.php: _count_posts_cache_key()
  • wp-includes/post.php: wp_count_posts()
  • wp-includes/revision.php: wp_restore_post_revision()
  • wp-includes/ms-deprecated.php: is_site_admin()
  • wp-includes/comment-template.php: wp_list_comments()
  • wp-includes/script-loader.php: wp_default_scripts()
  • Show 78 more used by Hide more used by

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 6You must log in to vote on the helpfulness of this note Contributed by SoN9ne

    daniyalahmedk’s example is redundant.

    The code below is the exact same thing but with less overhead.

    Get current user role by ID :

    
    $user = wp_get_current_user();
    echo $user->roles[0];
    

    There is no need to invoke a new WP_User object.

  2. Get current user role by ID :

    $user = new WP_User(get_current_user_id());
    echo $user->roles[0];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文