当前位置:网站首页>8 methods of getting user ID in WordPress

8 methods of getting user ID in WordPress

2022-06-21 08:14:00 Ashi

stay WordPress During the development of the theme , Get users ID The frequency of is very high , Can be found in WordPress View user information in , It can also be obtained directly through code .

One 、 stay WordPress User found in background area ID

It's a very simple way , Users with background management permission are required to view .

1、 Sign in WordPress  backstage

2、 Go to user —— All user list pages

3、 Edit user

4、 In the current page link user_id= The following number is the user's ID

Two 、 Get the current user ID( You can also get the user name ,Email etc. )

Get the current login user ID The best way is to use get_current_user_id() function .

$current_user_id = get_current_user_id();

And wp_get_current_user() Functions with the same method :

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

get_current_user_id() It seems simpler for me to use , But you can use whatever you want , Because they are the same in the code .

Get other user information :

$user_email = $current_user->user_email;$first_name = $current_user->first_name;$display_name = $current_user->display_name;

3、 ... and 、 adopt Email Get users ID

You can use get_user_by() Function to get user information

$the_user = get_user_by('email', '[email protected]');$the_user_id = $the_user->ID;

In turn, , We can also pass ID Get the user's Email

$the_user = get_user_by( 'id', 1 ); //  user  ID  by 1echo $the_user->user_email;

Four 、 Get the user through the user name ID

Also use get_user_by() Function to get user information

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

In turn, , We can also pass ID Get the user name , And through the ID Get users Email It's the same .

5、 ... and 、 Get users by name ID

Print all surnames “sa” Of users ID:

global $wpdb;$users = $wpdb->get_results("SELECT user_id FROM $wpdb->usermeta WHERE meta_key = 'first_name' AND meta_value = 'sa'");if ($users) {    foreach ($users as $user) {        echo $user->user_id;    }} else {    echo ' There is no user with this last name .';}

Print all names as “longlong” Of users ID:

global $wpdb;$users = $wpdb->get_results("SELECT user_id FROM $wpdb->usermeta WHERE meta_key = 'first_name' AND meta_value = 'longlong'");if ($users) {    foreach ($users as $user) {        echo $user->user_id;    }} else {    echo ' There is no user with this name .';}

If you want to find a user by any user meta value ID, So the above code can work . Just put the meta_key and meta_value Replace with the one you need .

Of course , stay get_user_meta( $id, $meta_key, true) Function , You can also get the user's last name and first name and any meta.

6、 ... and 、 Through the article ID Get the author ID

under these circumstances , You can learn from WP_Post Object to get the user ID.

$my_post = get_post( $post_id ); //  Access to the article  ID  Get article data echo $my_post->post_author; //  Print out author  ID

You can also use get_post_field() Function to get the author's ID

$author_id = get_post_field('post_author', $post_id);

7、 ... and 、 from WooCommerce Get the customer from the order ID

There are two different ways , The first method is to obtain customers by ordering unit data ID:

$customer_id = get_post_meta( 123, '_customer_user', true); // 123  It's an order  ID

The second one can be passed WC_Order Class to get the ,WooCommerce Version required 3.0 +.

$order = wc_get_order( 123 ); // 123  It's an order  ID$customer_id = $order->get_customer_id(); //   perhaps  $order->get_user_id()  It's the same 

8、 ... and 、 Will the user ID Add to WordPress Columns in the user list

For Administrators , To view the user ID Is quite convenient , You can add the following code to WordPress Topic function file functions.php in

/* *  Add user list column  */function salong_user_id_column($columns){    $columns['user_id'] = 'ID';    return $columns;}add_filter('manage_users_columns', 'salong_user_id_column'); /* *  Column content  */function salong_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',  'salong_user_id_column_content', 10, 3);
原网站

版权声明
本文为[Ashi]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202221502095818.html