- shares
- Facebook Messenger
- Gmail
- Viber
- Skype
– Lấy thông tin hiện tại của user logined vào biến global.
global $user_ID, //user IDa $user_identity //return user role ie: admin,.. $userdata; //user data, <a href="http://codex.wordpress.org/Class_Reference/WP_User" target="_blank">WP_User</a> object get_currentuserinfo(); //call this function to update global user meta including $user_ID,$user_identity,$userdata.
User data
$user=get_user_by('email',$email); //get user data by email print_r($user); //print out an array of user fields //get user meta $user_id = 9; $key = 'last_name'; $single = true; $user_last = get_user_meta( $user_id, $key, $single ); echo '<p>The '. $key . ' value for user id ' . $user_id . ' is: ' . $user_last . '</p>'; //Getting all meta data $all_meta_for_user = get_user_meta( 9 ); print_r( $all_meta_for_user ); #get last name $last_name = $all_meta_for_user['last_name'][0]; //return img tag as user avatar with dimension of 60x60 pixel get_avatar($userdata->ID,60);
Get Current user
//get current user $user=wp_get_current_user(); $user->user_login;$user->user_email,...
– Get users matchs criteria.
$data=get_users(array( //custom meta fields 'meta_query'=>array( array( 'key' => 'location', 'value' => $location, //'compare' => 'NOT LIKE' ), array( 'key' => 'bank', 'value' => $bank, ), ), 'orderby'=>'login', //ID 'order'=>'ASC' )); foreach($data as $user){ $user->display_name; }
User role
//check current user role current_user_can('manage_options'); //user are admin current_user_can('edit_user',$user_id); //user can update them profile $user=wp_get_current_user(); if($user->has_cap('edit_posts')) { //do somthing }
Author of post
– Get author link.
//get author link <h4>Author: <a href="<?php the_author_url(); ?>"><?php the_author_firstname(); ?> <?php the_author_lastname(); ?></a></h4> #try it the_author_meta('user_url',$user_id) #other way //cách khác <p><a href="<?php bloginfo('url'); ?>/?author=<?php the_author_ID(); ?>"> <?php the_author_firstname(); ?> <?php the_author_lastname(); ?> has wrote <?php the_author_posts(); ?> articles for us.</a></p>
Để nhận được bài viết mới vui lòng đăng ký kênh kiến thức WordPress từ A-Z ở Form bên dưới. Bạn cũng có thể nhận được sự trợ giúp trên Twitter và Facebook
- shares
- Facebook Messenger
- Gmail
- Viber
- Skype