During the development of wordpress theme, the frequency of obtaining user ID is very high. You can view user information in WordPress or obtain it directly through code.
1, Find the user ID in the WordPress background area
This is a very simple method, which can only be viewed by users with background management permission.
1. Log in to WordPress # background
2. Go to users - all users list page
3. Edit user
4. User in the current page link_ The number after id = is the user's ID
2, Get the current user ID (you can also get the user name, Email, etc.)
The best way to get the currently logged in user ID is to use get_current_user_id() function.
$current_user_id = get_current_user_id();
And WP_ get_ current_ The same function as the user() method:
$current_user = wp_get_current_user();$current_user_id = $current_user->ID;
get_ current_ user_ The use of ID () seems simpler to me, but you can use whatever you want, because they are the same in the code.
Obtain other user information:
$user_email = $current_user->user_email;$first_name = $current_user->first_name;$display_name = $current_user->display_name;
3, Get user ID via Email
You can use get_ user_ Get user information by() function
$the_user = get_user_by('email', 'admin@salongweb.com');$the_user_id = $the_user->ID;
Conversely, we can also get the user's Email through ID
$the_user = get_user_by( 'id', 1 ); // The user ID is 1echo $the_ user->user_ email;
4, Get user ID by user name
Also use get_ user_ Get user information by() function
$the_user = get_user_by('login', 'salongweb');$the_user_id = $the_user->ID;
Conversely, we can also obtain the user name through the ID, which is the same as obtaining the user Email through the ID.
5, Get user ID by name
Print the ID s of all users whose last name is "sa":
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 the ID s of all users with the name "longlong":
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 that name.';}
The above code works if you want to find the user ID through any user meta value. Just put the meta_key and meta_ Replace value with the one you need.
Of course, in get_ user_ With the help of meta ($ID, $meta_key, true) function, you can also get the user's last name and first name and any meta.
6, Get author ID by article ID
In this case, you can from WP_ Get the user ID from the post object.
$my_post = get_post( $post_id ); // Get 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 directly
$author_id = get_post_field('post_author', $post_id);
7, Get customer ID from WooCommerce's order
There are two different methods. The first is to obtain the customer ID through the subscription unit data:
$customer_id = get_post_meta( 123, '_customer_user', true); // 123 is the order ID
The second can be through WC_Order class, WooCommerce version needs 3.0 +.
$order = wc_get_order( 123 ); // 123 is the order ID $customer_ id = $order->get_ customer_ id(); // Or $order - > get_ user_ ID () is the same
8, Add the user ID to the column of the WordPress user list
This is quite convenient and fast for administrators to view user ID s. You can add the following code to the WordPress Theme function file functions PHP
/* * 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);