wp_get_current_user()

Retrieve the current user object.


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


Return Return

(WP_User) Current WP_User instance.


Top ↑

Source Source

File: wp-includes/pluggable.php

function wp_get_current_user() {
	return _wp_get_current_user();
}

Top ↑

Changelog Changelog

Changelog
Version Description
2.0.3 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note content
    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. Skip to note content
    Contributed by Codex

    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.