Get logged in user info in WordPress

Get logged in user info in WordPress

Use wp_get_current_user() function to get logged in user info.  We can use wp_get_current_user() function without any argument. It will return an object, if user is logged in else return 0. We can check if user is logged in using function is_user_logged_in(). I'm using the following code to get logged in user.

<?php 
    if ( is_user_logged_in() ) { 
        $getLoggedInUser = wp_get_current_user();
        echo '<pre>';
        print_r($getLoggedInUser);
    } 
?>

Also Read: Get custom post types in WordPress
Also Read: How to get all UK addresses by postal code

Use wp_get_current_user() function to get logged in user info.  We can use wp_get_current_user() function without any argument. It will return an object, if user is logged in else return 0. We can check if user is logged in using function is_user_logged_in(). I'm using the following code to get logged in user.

<?php 
    if ( is_user_logged_in() ) { 
        $getLoggedInUser = wp_get_current_user();
        echo '<pre>';
        print_r($getLoggedInUser);
    } 
?>

Also Read: Get custom post types in WordPress
Also Read: How to get all UK addresses by postal code

Please let me know what your thoughts or comments are on this article. If you have any suggestion or found any mistake in this article then please let us know.

Latest Comments

Cordial
Cordial
08 Oct 2022

I need a shortcode that I can insert anywhere on my WordPress Post or Page, and it will show the Username of the current Logged in User. I tried this code: -------------------------------------------------------------------------------------- function show_loggedin_function( $atts ) { global $current_user, $user_login; get_currentuserinfo(); add_filter('widget_text', 'do_shortcode'); if ($user_login) return 'Welcome ' . $current_user->display_name . '!'; else return '<a href="' . wp_login_url() . ' ">Login</a>'; } add_shortcode( 'show_loggedin_as', 'show_loggedin_function' ); ------------------------------------------------------------------------------------ But it only outputted the shortcode instead of the Username as you can see here--https://prnt.sc/m5T4fJwLkAN6 How can I use your code to output the Logged in Username in a shortcode? Regards.

Admin
Admin
08 Oct 2022

Use below code function show_loggedin_function() { global $current_user; wp_get_current_user(); if ( is_user_logged_in() ) { echo $current_user->display_name; } else { echo '<a href="' . wp_login_url() . ' ">Login</a>'; } } add_shortcode( 'show_loggedin_as', 'show_loggedin_function' );

Add your comment

Close