Ways to get user ID in WordPress – Learn WordPress from a to z

Tutorials 0 lượt xem

Are you a WordPress developer and need to develop user-related features? This article will show you how to get the user ID in WordPress.

get user ID in WordPress

Search in WordPress admin page

This is a very simple method, but it also has some disadvantages.

First – you have to log in, second – you cannot get your own id.

  1. Login to your WordPress admin
  2. Go to Members > All Users
  3. Select and access a user’s profile
  4. Look at the page’s URL:

Current user, logged in

The way to get the current logged in user ID is to use the get_current_user_id() function .

$current_user_id = get_current_user_id();<div><span>1</span></div>

Or use the wp_get_current_user() function to get the current user’s object:

$current_user = wp_get_current_user();
$current_user_id = $current_user-&gt;ID;
<div>
<span>1</span><span>2</span>
</div>

Since this function will return the object of the current user, you can use it to get email address, last name, first name, username, etc.

$current_user-&gt;user_email
$current_user-&gt;first_name
$current_user-&gt;last_name
$current_user-&gt;user_login<div>
<span>1</span><span>2</span><span>3</span><span>4</span>
</div>

Based on user’s email address

To get the user ID based on the email address, you will have to use the get_user_by() function .

$the_user = get_user_by('email', 'misha@rudrastyh.com');
$the_user_id = $the_user-&gt;ID;
<div>
<span>1</span><span>2</span>
</div>

Based on username (username)

In the example below, my username will be huykira. You also still use the get_user_by() function to get the user ID.

$the_user = get_user_by('login', 'huykira');
$the_user_id = $the_user-&gt;ID;
<div>
<span>1</span><span>2</span>
</div>

Get author ID based on article ID

In this case, you can get the user ID from the WP_Post object.

$my_post = get_post( $id ); // $id - Post ID
echo $my_post-&gt;post_author; // print post author ID
<div>
<span>1</span><span>2</span>
</div>

Get customer ID from order in WooCommerce

There are two different ways to do it, the first is to get the customer ID from the order:

$customer_id = get_post_meta( 541, '_customer_user', true); // 541 là ID đơn hàng<div><span>1</span></div>

The second way is with the help of the WC_Order object. This method will only work for WC 3.0+.

$order = wc_get_order( 541 ); // 541 là ID đơn hàng
$customer_id = $order-&gt;get_customer_id();
<div>
<span>1</span><span>2</span>
</div>

Add User ID Column to WordPress User List Table

This is a very useful way to get user ID through WordPress admin. Finished Result:
Add User ID column
All you need to do is copy 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');
<div>
<span>1</span><span>2</span><span>3</span><span>4</span><span>5</span><span>6</span><span>7</span><span>8</span><span>9</span><span>10</span><span>11</span><span>12</span><span>13</span><span>14</span><span>15</span><span>16</span><span>17</span><span>18</span><span>19</span><span>20</span><span>21</span><span>22</span><span>23</span><span>24</span><span>25</span><span>26</span>
</div>

summary

This article is a collection of tips for you to work with user related functions. If everything works as expected, please comment and share this article.

In addition, you can follow the WordPress Tips section and follow Facebook for more new knowledge.

Bài viết liên quan