返回介绍

wp_get_current_user()

发布于 2017-09-11 12:03:05 字数 5665 浏览 930 评论 0 收藏 0

wp_get_current_user()

Retrieve the current user object.


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.


返回值

(WP_User) Current WP_User instance.


源代码

File: wp-includes/pluggable.php

function wp_get_current_user() {
	return _wp_get_current_user();
}

更新日志

Versiondescription
2.0.3Introduced.

相关函数

Uses

  • wp-includes/user.php: _wp_get_current_user()

Used By

  • wp-includes/l10n.php: get_user_locale()
  • 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-comments-controller.php: WP_REST_Comments_Controller::create_item()
  • wp-includes/comment.php: wp_handle_comment_submission()
  • wp-admin/includes/class-wp-screen.php: WP_Screen::render_meta_boxes_preferences()
  • wp-signup.php: signup_another_blog()
  • wp-signup.php: validate_another_blog_signup()
  • wp-signup.php: validate_blog_form()
  • wp-admin/includes/ms.php: update_option_new_admin_email()
  • wp-admin/includes/ms.php: send_confirmation_on_profile_email()
  • wp-admin/includes/misc.php: set_screen_options()
  • wp-admin/includes/schema.php: populate_network()
  • wp-admin/includes/ajax-actions.php: wp_ajax_closed_postboxes()
  • wp-admin/includes/ajax-actions.php: wp_ajax_hidden_columns()
  • wp-admin/includes/ajax-actions.php: wp_ajax_meta_box_order()
  • wp-admin/includes/ajax-actions.php: wp_ajax_replyto_comment()
  • wp-admin/includes/nav-menu.php: wp_nav_menu_setup()
  • wp-admin/includes/nav-menu.php: wp_initial_nav_menu_meta_boxes()
  • wp-admin/includes/ms.php: confirm_delete_users()
  • wp-includes/capabilities.php: is_super_admin()
  • wp-includes/capabilities.php: current_user_can()
  • wp-includes/capabilities.php: current_user_can_for_blog()
  • wp-includes/pluggable.php: wp_verify_nonce()
  • wp-includes/pluggable.php: wp_create_nonce()
  • wp-includes/pluggable.php: is_user_logged_in()
  • wp-includes/general-template.php: wp_default_editor()
  • wp-includes/class-wp.php: WP::init()
  • wp-includes/admin-bar.php: wp_admin_bar_my_account_item()
  • wp-includes/admin-bar.php: wp_admin_bar_my_account_menu()
  • wp-includes/user.php: wp_update_user()
  • wp-includes/user.php: get_current_user_id()
  • wp-includes/ms-functions.php: is_user_spammy()
  • wp-includes/ms-functions.php: is_user_option_local()
  • wp-includes/ms-blogs.php: switch_to_blog()
  • wp-includes/ms-blogs.php: restore_current_blog()
  • wp-includes/comment-template.php: comment_form()
  • Show 31 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: 1You must log in to vote on the helpfulness of this note Contributed by Codex

    Default Usage
    The call to wp_get_current_user() returns the WP_User object.

    
    <?php
    $current_user = wp_get_current_user();
    
    /*
     * @example Safe usage: $current_user = wp_get_current_user();
     * if ( ! ( $current_user instanceof WP_User ) ) {
     *     return;
     * }
     */
    printf( __( 'Username: %s', 'textdomain' ), esc_html( $current_user->user_login ) ) . '<br />';
    printf( __( 'User email: %s', 'textdomain' ), esc_html( $current_user->user_email ) ) . '<br />';
    printf( __( 'User first name: %s', 'textdomain' ), esc_html( $current_user->user_firstname ) ) . '<br />';
    printf( __( 'User last name: %s', 'textdomain' ), esc_html( $current_user->user_lastname ) ) . '<br />';
    printf( __( 'User display name: %s', 'textdomain' ), esc_html( $current_user->display_name ) ) . '<br />';
    printf( __( 'User ID: %s', 'textdomain' ), esc_html( $current_user->ID ) );
    
  2. Checking Other User Attributes
    This example demonstrates how to manually determine if a user is logged in.

    IMPORTANT NOTE: This is for demonstration purposes ONLY. The correct way to determine whether a user is logged in is to use the function is_user_logged_in().

    
    function wpdocs_check_logged_in() {
    	$current_user = wp_get_current_user();
    	if ( 0 == $current_user->ID ) {
    	    // Not logged in.
    	} else {
    	    // Logged in.
    	}
    }
    add_action( 'init', 'wpdocs_check_logged_in' );
    

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

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

发布评论

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