Skip to content
pakainfo

Pakainfo

Web Development & Good Online education

  • Home
  • Blog
  • Categories
  • Tools
  • Full Form
  • Guest Post
  • Advertise
  • About
  • Contact Us

how to get current login user id in php wordpress?

June 8, 2020 Pakainfo wordpress Leave a comment

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?

Contents

  • get current login user id in php wordpress?
  • 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?
  • Add the User ID column to the WordPress Users Table
  • 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
    • Way #1 – Update password for existing user
    • Way #2 – create new user
    • Way #3 Using USER define function
    • Related posts

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.

Also Read This 👉   how to create registration form in wordpress without plugin?
How to get current user details in wordpress
How to get current user details in wordpress

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”:

Also Read This 👉   WooCommerce REST API : Auto generating API keys using our Application Authentication Endpoint

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 '

<style>.column-user_id{width: 5%}</style>

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

<!--?php //Get user ID by email $current_logged_user = get_user_by('email', '[email protected]'); $user_id = $current_logged_user->ID;<br ?--> $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:

Also Read This 👉   How to get current user details in wordpress?

// 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('[email protected]'), '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;
}

Related posts:

  1. How to get the Get current user has role in WordPress?
  2. How to Get current User details in WordPress woocommerce? | WP_User() | wp_get_current_user() | get_user_by() | get_userdata()
  3. How to get current user details in wordpress?
  4. Laravel 6 Get Current User Details using Controller and blade
  5. WordPress set and get User last login Date and time
  6. how to Get Database Table Prefix In WordPress?
  7. Get Current login Auth user details using Laravel 5.7
  8. How To Get Last Insert Id In wpdb WordPress?
  9. WordPress Next Previous Article/post using CSS
how to get current login user id in php wordpresshow to get user session in wordpresswordpress get current sessionwordpress get current user passwordwordpress get current user rolewordpress get user metawordpress get user role by idwordpress user object

Post navigation

Previous Post:Textsheet Alternative – 8 Best websites Like Textsheet (2020)
Next Post:wordpress Download – free – latest version

Advertise With Us

Increase visibility and sales with advertising. Let us promote you online.
Click Here

Write For Us

We’re accepting well-written informative guest posts and this is a great opportunity to collaborate.
Submit a guest post to [email protected]
Contact Us

Freelance web developer

Do you want to build a modern, lightweight, responsive website quickly?
Need a Website Or Web Application Contact : [email protected]
Note: Paid Service
Contact Me

Categories

3movierulz (64) Ajax (464) AngularJS (377) ASP.NET (61) Bio (109) Bollywood (108) Codeigniter (175) CSS (98) Earn Money (69) Education (61) Entertainment (130) fullform (86) Google Adsense (63) Highcharts (77) History (40) Hollywood (109) JavaScript (1358) Jobs (42) jQuery (1423) Laravel (1088) LifeStyle (53) movierulz4 (63) Mysql (1029) Mysqli (890) php (2121) Programming (2332) Python (98) Software (167) Software (88) Stories (98) tamilrockers (104) Tamilrockers kannada (64) Tamilrockers telugu (61) Tech (143) Technology (2392) Tips and Tricks (119) Tools (207) Top10 (484) Trading (91) Trending (71) VueJs (250) Web Technology (105) webtools (192) wordpress (166) World (325)

A To Z Full Forms

Access a complete full forms list with the meaning, definition, and example of the acronym or abbreviation.
Click Here
  • Home
  • About Us
  • Terms And Conditions
  • Write For Us
  • Advertise
  • Contact Us
  • Youtube Tag Extractor
  • Info Grepper
  • Guest Posting Sites
  • Increase Domain Authority
  • Social Media Marketing
  • Freelance web developer
  • Tools
Pakainfo 9-OLD, Ganesh Sco, Kothariya Ring Road, Chokadi, Rajkot - 360002 India
E-mail : [email protected]
Pakainfo

© 2023 Pakainfo. All rights reserved.

Top
Subscribe On YouTube : Download Source Code
We accept paid guest Posting on our Site : Guest Post Chat with Us On Skype Guest Posting Sites