WordPress

wp_get_current_user()

Syntax#

  • wp_get_current_user()

Getting the current user

Getting all the information of current user in wordpress using the predefined function wp_get_current_user();

<?php
    
        $current_user = wp_get_current_user();
    
        echo "Username :".$current_user->user_login;
        echo "Username :".$current_user->ID;
        echo "Username :".$current_user->user_pass;
        echo "Username :".$current_user->user_nicename;
        echo "Username :".$current_user->user_email;
        echo "Username :".$current_user->user_url;
        echo "Username :".$current_user->user_registered;
        echo "Username :".$current_user->user_activation_key;
        echo "Username :".$current_user->user_status;
        echo "Username :".$current_user->display_name;
        
    ?>

Use foreach loop to get user info from wp_get_current_user()

$user = wp_get_current_user();

foreach($user->data as $key=>$user_data){
    if($key == 'user_pass' || $key == 'user_activation_key' || $key=='user_status'){}
    else{
        $nice_key = ucfirst(str_replace('_', ' ', $key));
    
        if($key == 'user_registered'){
            $user_data = date_i18n(get_option('date_format'), strtotime($user_data));
        }

    echo $nice_key . ' : ' . $user_data . '<br />';
   }
}

This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow