wp_get_current_user(): WP_User

Retrieves 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.

See also

Return

WP_User Current WP_User instance.

Source

function wp_get_current_user() {
	return _wp_get_current_user();
}

Changelog

VersionDescription
2.0.3Introduced.

User Contributed Notes

  1. Skip to note 5 content

    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. Skip to note 6 content

    Just want the ID (and nothing else)?

    Just use get_current_user_id() instead:

       $current_user_id = get_current_user_id();
       $current_user = wp_get_current_user();
     
  3. Skip to note 7 content

    You can use it from the plugins_loaded hook on.

  4. Skip to note 8 content

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

You must log in before being able to contribute a note or feedback.