Hoàng Web

Thiết Kế Website WordPress

  • Kho giao diện
  • Dịch Vụ
    • Thiết kế web giá rẻ
    • Thiết kế website WordPress
    • Hosting Miễn Phí 100GB
    • Tích hợp thanh toán MoMo, ViettelPay, Vietcombank, MB..
    • Tối ưu Google PageSpeed
    • Sửa lỗi nâng cấp website
    • Viết plugin WordPress
    • Code Tool theo yêu cầu
  • Bảng giá
  • Quy trình làm việc
  • Giới thiệu
  • Liên Lạc
Trang chủ » Wordpress » Lấy thông tin user meta fields – WordPress

Lấy thông tin user meta fields – WordPress

Thứ Năm, 03/07/2014 by Hoàng Quách

Nội dung

  • 1 User data
  • 2 Get Current user
  • 3 User role
  • 4 Author of post
  • shares
  • Facebook
  • 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
  • Facebook Messenger
  • Gmail
  • Viber
  • Skype

Chuyên mục: Wordpress Tìm kiếm: author post, user data

Tôi giúp gì cho bạn?

HOÀNG WEB

Địa chỉ: Tây Sơn, Phường Quang Trung, Quận Đống Đa, Hà Nội

Hotline: 0987 342 124 – 0868 292 303 (8h:00 – 21h:00)

Email: [email protected]

Website: www.hoangweb.com

KẾT NỐI VỚI TÔI

  • Facebook
  • GitHub
  • YouTube

SẢN PHẨM

  • Plugin Thanh Toán Quét Mã QR Code Tự Động
  • WP2Speed – Tối ưu Google Speed
  • 23WebHost – Hosting Miễn Phí 100GB

LIÊN KẾT

  • Có nên thuê thiết kế website giá rẻ?
  • Hướng dẫn thanh toán
  • Chính sách hoàn tiền
  • Trung tâm hỗ trợ

Copyright © 2023 | All rights reserved | HOANG WEB
Mọi hình thức sao chép nội dung trên website này mà chưa được sự đồng ý đều là trái phép.