当前位置:网站首页>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);边栏推荐
- 1004 Counting Leaves (30 分)
- RISC-V 的MMU
- Qunhui dsm7 add kit source
- Weekly update | showmebug officially launched Tencent conference audio and video
- 虚拟机浏览器花屏空白问题
- 5 minutes to understand MySQL - row to column
- MySql 过滤查询(以字母开头,以数字开头,非数字开头,非字母开头)
- 33 Jenkins modify plug-in source
- Huasan IPSec
- How to write attractive titles for short videos? Learn these tips to make your title more convincing
猜你喜欢

测试入门——软件测试模型

PS prompts "script error -50 general Photoshop error, how to solve it?

Introduction to testing - Software Test Model

线上GO服务出现GC故障,我当时就急了

Eureka的TimedSupervisorTask类(自动调节间隔的周期性任务)

2021-06-16 STM32F103 EXTI 中斷識別 使用固件庫

Use lua+redis+openresty to realize concurrent optimization of e-commerce Homepage

MMS for risc-v

2021-06-16 STM32F103 EXTI 中断识别 使用固件库

The market value of Jinshan office fell below 100 billion yuan: the shareholder Qiwen n-dimensional cash out exceeded 1.2 billion yuan
随机推荐
/home/ljx/miniconda3/compiler_compat/ld: cannot find crtbeginS.o: 没有那个文件或目录
Le Code est correct, mais les données de la base de données ne sont pas affichées.
函数声明和函数表达式的区别
Three declaration methods of structure type
php exec、 system 、shell_ Exec cannot be executed. There is no result. The result is nulld. Solution
5 minutes to understand MySQL - row to column
2021-06-16 STM32F103 EXTI 中断识别 使用固件库
JVM memory model concepts
Global and Chinese market for packed gas chromatographic columns 2022-2028: Research Report on technology, participants, trends, market size and share
2021-07-28 STM32F103 configuration information
2022-2028 global after sales spark plug industry research and trend analysis report
There was a GC failure in the online go service. I was in a hurry
2022-2028 global boom cylinder industry research and trend analysis report
Interview experience - bytes
Weekly update | showmebug officially launched Tencent conference audio and video
(for line breaks) differences between gets and fgets, and between puts and fputs
CTF show WEB10
2021-07-28 STM32F103 I2C Hardware Transfer Include previous IO Clock EXIT USB use firmware library
PHP类与对象详细介绍
Global and Chinese market of Toro from 2022 to 2028: Research Report on technology, participants, trends, market size and share