Different methods for obtaining user ID in WordPress

lấy user ID trong WordPress

This article provides tips for WordPress developers on how to get user IDs in WordPress. The methods include searching in the WordPress admin, using functions like get_current_user_id() and get_user_by(), and getting the author ID based on post ID. It also shows how to get customer IDs from orders in WooCommerce and add a user ID column to the WordPress user list table. The article concludes by encouraging readers to share their feedback and follow the WordPress Tips section for more knowledge.

Are you a WordPress developer looking to add user-related features? This guide will help you understand how to "get user ID" in WordPress.

Search in WordPress admin

It’s a simple method, but there are limitations. Firstly, you need to log in. Secondly, you can’t retrieve your own ID.

  1. Log in to your WordPress admin.
  2. Navigate to Members > All Users.
  3. Select and access user profiles.
  4. Check the page URL.

Current user, currently logged in

To retrieve the ID of the currently logged-in user, use the function get_current_user_id():

$current_user_id = get_current_user_id();

Alternatively, you can use wp_get_current_user() to get the current user’s object:

$current_user = wp_get_current_user();
$current_user_id = $current_user->ID;

This function returns the current user object, allowing you to access email address, first name, last name, username, etc.

Based on user email address

To fetch the user ID based on the email address, utilize get_user_by():

$the_user = get_user_by('email', 'misha@rudrastyh.com');
$the_user_id = $the_user->ID;

Based on username

For example, with the username "huykira," you can still use get_user_by() to obtain the user ID:

$the_user = get_user_by('login', 'huykira');
$the_user_id = $the_user->ID;

Get Author ID Based on Post ID

Retrieve the user ID from the WP_Post object:

$my_post = get_post($id); // $id - Post ID
echo $my_post->post_author; // prints post author ID

Get Customer ID from Order in WooCommerce

There are two methods to achieve this. Initially, get the customer ID from the order:

$customer_id = get_post_meta(541, '_customer_user', true); // 541 is the order ID

Secondly, with the WC_Order object (for WC 3.0+):

$order = wc_get_order(541); // 541 is the order ID
$customer_id = $order->get_customer_id();

Add User ID Column to WordPress User List Table

Enhance user ID visibility through the WordPress admin by adding a column. Simply insert the following code into the functions.php file:

/* Adding the column */
function hk_user_id_column($columns) {
    $columns['user_id'] = 'ID';
    return $columns;
}
add_filter('manage_users_columns', 'hk_user_id_column');

/* Column content */
function hk_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', 'hk_user_id_column_content', 10, 3);

/* Column style (optional) */
function hk_user_id_column_style() {
    echo '';
}
add_action('admin_head-users.php', 'hk_user_id_column_style');

Summary

This article provides various methods to interact with user-related functions. If everything works as intended, feel free to comment and share this article. For more WordPress tips, follow the Facebook page in the WordPress Tips section.

See also  Displaying products added to cart in WooCommerce.

Feel free to rate this article as well! 🌟

Rate this post

Related posts