返回介绍

get_user_meta()

发布于 2017-09-11 00:45:17 字数 5874 浏览 1150 评论 0 收藏 0

get_user_meta( int $user_id,  string $key = '',  bool $single = false )

Retrieve user meta field for a user.


description


参数

$user_id

(int) (Required) User ID.

$key

(string) (Optional) The meta key to retrieve. By default, returns data for all keys.

Default value: ''

$single

(bool) (Optional) Whether to return a single value.

Default value: false


返回值

(mixed) Will be an array if $single is false. Will be value of meta data field if $single is true.


源代码

File: wp-includes/user.php

function get_user_meta($user_id, $key = '', $single = false) {
	return get_metadata('user', $user_id, $key, $single);
}

更新日志

Versiondescription
3.0.0Introduced.

相关函数

Uses

  • wp-includes/meta.php: get_metadata()

Used By

  • wp-admin/includes/class-wp-screen.php: WP_Screen::render_meta_boxes_preferences()
  • wp-includes/class-wp-user-meta-session-tokens.php: WP_User_Meta_Session_Tokens::get_sessions()
  • wp-admin/includes/ms.php: choose_primary_blog()
  • wp-admin/includes/ms.php: new_user_email_admin_notice()
  • wp-admin/includes/class-wp-internal-pointers.php: WP_Internal_Pointers::enqueue_scripts()
  • wp-admin/includes/ajax-actions.php: wp_ajax_save_user_color_scheme()
  • wp-admin/includes/ajax-actions.php: wp_ajax_dismiss_wp_pointer()
  • wp-includes/class-wp-user.php: WP_User::__get()
  • wp-includes/class-wp-user.php: WP_User::_init_caps()
  • wp-includes/user.php: wp_update_user()
  • wp-includes/user.php: get_blogs_of_user()
  • wp-includes/user.php: is_user_member_of_blog()
  • wp-includes/ms-functions.php: wpmu_create_blog()
  • wp-includes/ms-functions.php: get_active_blog_for_user()
  • wp-includes/ms-functions.php: add_user_to_blog()
  • wp-includes/ms-functions.php: remove_user_from_blog()
  • Show 11 more used by Hide more used by

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 3You must log in to vote on the helpfulness of this note Contributed by Codex

    Getting all meta data
    This example demonstrates leaving the $key argument blank, in order to retrieve all meta data for the given user (in this example, user_id = 9):

    
    <?php
      $all_meta_for_user = get_user_meta( 9 );
      print_r( $all_meta_for_user );
    ?>
    

    Results:

    Array ( [first_name] => Array ( [0] => Tom ) [last_name] => Array ( [0] => Auger)
    [nickname] => Array ( [0] => tomauger ) [description] => etc.... )

    Note: In order to access the data in this example you need to dereference the array that is returned for each key, like so:

    
    $last_name = $all_meta_for_user['last_name'][0];
    

    To avoid this, you may want to run a simple array_map() on the results of get_user_meta() in order to take only the first index of each result (thus emulating what the $single argument does when $key is provided:

    
      $all_meta_for_user = array_map( function( $a ){ return $a[0]; }, get_user_meta( $user_id ) );
      print_r( $all_meta_for_user );
    

    Results:

    Array ( [first_name] => Tom [last_name] => Auger [nickname] => tomauger [description] => etc.... )

    Additionally, if you want to return ALL meta for a specific user and filter out empty values, you can run array_filter() on the results of the array_map() above:

    
    // Get all user meta data for $user_id
    $meta = get_user_meta( $user_id );
    
    // Filter out empty meta data
    $meta = array_filter( array_map( function( $a ) {
    	return $a[0];
    }, $meta ) );
    
  2. If the key does not exist the function will return an empty string or an empty array depending on the value of the $single parameter

    This example returns and then displays the last name for user id 9.

    <?php 
      $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>'; 
    ?>
    

    Result:

    The last_name value for user id 9 is Franklin

    To check if returned value is empty, ie does not exist, you could use something like:

    
    global $current_user; 
    
    get_currentuserinfo();
    
    if ( $current_user ) {
    	$permission = get_user_meta( $current_user->ID, 'some_meta' , true );
    	
    	if ( ! empty( $permission ) {
    		// do stuff
    	}
    }
    // works for both array and single values
    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文