Here, We will let you know how to get current logged in user in WordPress. you can get current user id in PHP, display name. we will user PHP function to get current user data.
get current login user id in php wordpress?
we always require getting current logged in user data in WordPress user’s ID. We will show you how to get current user data like username, id, email, address etc by using WordPress user’s ID. We will give you Multiple 8 way to get current user data using Get WordPress admin access through database. you can easy get logged in user details in PHP or Get Author ID by Post ID. As you would like to get it.

So, we can get current user details using WordPress USER define function. You can also use simple PHP helper. So We will give you Multiple example, you can take any one as you want. So let’s display current_user, Id, names, login name, username, email, Author ID by Post ID etc Multiple examples one by one.
- Get Current User ID (and username, email etc)
- How to Get User ID by Email?
- Get User Email by ID
- Get User ID by Username (login name)
- Get User ID by First Name or by Last Name
- How to Get Author ID by Post ID?
- How to Get a Customer ID from an Order in WooCommerce?
- How To: Get the Current Logged in User ID in WordPress
- Get User ID by Email (using PHP)
- WordPress get display name
- Get WordPress admin access through database
- METHOD #1 – change password for existing user
- METHOD #2 – create new user
- METHOD #3 Using USER define function
Get Current User ID (and username, email etc)
$current_user_id = get_current_user_id(); $current_user = wp_get_current_user(); $current_user_id = $current_user->ID;
How to Get User ID by Email?
$current_logged_user = get_user_by('email', '[email protected]'); $the_user_id = $current_logged_user->ID;
Get User Email by ID
$current_logged_user = get_user_by( 'id', 41 ); // 41 is a user ID echo $current_logged_user->user_email; $current_logged_user = get_userdata( 41 ); echo $current_logged_user->user_email;
Get User ID by Username (login name)
$current_logged_user = get_user_by('login', 'krupali'); $the_user_id = $current_logged_user->ID;
Get User ID by First Name or by Last Name
global $wpdb; $userDetails = $wpdb->get_results( "SELECT user_id FROM $wpdb->usermeta WHERE meta_key = 'first_name' AND meta_value = 'Pandyabig'" ); if( $userDetails ) { foreach ( $userDetails as $user ) { echo ' ' . $user->user_id . ' '; } } else { echo 'There are no userDetails with the each first name.'; }
Print the ids of all userDetails with the last name is “krupali”:
global $wpdb; $userDetails = $wpdb->get_results( "SELECT user_id FROM $wpdb->usermeta WHERE meta_key = 'first_name' AND meta_value = 'krupali'" ); if( $userDetails ) { foreach ( $userDetails as $user ) { echo ' ' . $user->user_id . ' '; } } else { echo 'There are no userDetails with the each last name.'; }
How to Get Author ID by Post ID?
$article_detai8lks = get_post( $id ); // print $id - Post ID echo $article_detai8lks->post_author; // print post author ID
How to Get a Customer ID from an Order in WooCommerce?
$current_customer_id = get_post_meta( 69, '_customer_user', true); // 69 is your order ID $order = wc_get_order( 69 ); // 69 is your order ID $current_customer_id = $order->get_customer_id(); // or $order->get_user_id() – the each
Add the User ID column to the WordPress Users Table
function rd_user_id_column( $columns ) { $columns['user_id'] = 'ID'; return $columns; } add_filter('manage_users_columns', 'rd_user_id_column'); function rd_user_id_column_content($value, $column_name, $user_id) { if ( 'user_id' == $column_name ) return $user_id; return $value; } add_action('manage_users_custom_column', 'rd_user_id_column_content', 10, 3); function rd_user_id_column_style(){ echo ' '; } add_action('admin_head-users.php', 'rd_user_id_column_style');
How To: Get the Current Logged in User ID in WordPress
$current_user_id = get_current_user_id(); echo 'Your User ID is: ' .$current_user_id;
Get User ID by Email (using PHP)
$user_email = $current_logged_user->user_email; if(!empty($user_id)){ echo 'The user ID for '.$user_email.' is '.$user_id; }
WordPress get display name
// Different ways getting the current_logged_user object $current_logged_user = new WP_User( $user_id ); // Current current_logged_user object $current_logged_user = wp_get_current_user(); // Get current_logged_user by ID, email, slug or login $current_logged_user = get_user_by( 'ID', $user_id ); // Get current_logged_user data by current_logged_user id $current_logged_user = get_userdata( $user_id );
When the fetch user object is made you can easy to obtain the latest display name from it as bellows:
// Get users display name from user object $display_name = $user->display_name;
Get WordPress admin access through database
Way #1 – Update password for existing user
SELECT u.ID, u.user_login, u.user_nicename, u.user_email FROM wp_users u INNER JOIN wp_usermeta m ON m.user_id = u.ID WHERE m.meta_key = 'wp_capabilities' AND m.meta_value LIKE '%administrator%' ORDER BY u.user_registered
Way #2 – create new user
INSERT INTO `wp_users` (`user_login`, `user_pass`, `user_nicename`, `user_email`, `user_status`) VALUES ('ramnika', MD5('ramink@458'), 'Ramnik Calc', '[email protected]', '0'); INSERT INTO `wp_usermeta` (`umeta_id`, `user_id`, `meta_key`, `meta_value`) VALUES (NULL, (Select max(id) FROM wp_users), 'wp_capabilities', 'a:1:{s:13:"administrator";s:1:"1";}'); INSERT INTO `wp_usermeta` (`umeta_id`, `user_id`, `meta_key`, `meta_value`) VALUES (NULL, (Select max(id) FROM wp_users), 'wp_user_level', '10');
Way #3 Using USER define function
function getAdministratorId(){ global $wpdb; $table_wp_users = $wpdb->prefix . 'users'; $table_wp_usermeta = $wpdb->prefix . 'usermeta'; $current_logged_user = $wpdb->get_results("SELECT u.ID FROM $table_wp_users u INNER JOIN $table_wp_usermeta m ON m.user_id = u.ID WHERE m.meta_key = 'wp_capabilities' AND m.meta_value LIKE '%administrator%' ORDER BY u.user_registered"); if(count($current_logged_user) > 0){ $user_id = isset($current_logged_user) ? $current_logged_user[0]->ID : 5; } return $user_id; }