admin管理员组

文章数量:1392086

I'm trying that :

<?php $phone = get_user_meta($current_user->ID,'phone_number',true); echo $phone; ?>

But it's not working

I'm trying that :

<?php $phone = get_user_meta($current_user->ID,'phone_number',true); echo $phone; ?>

But it's not working

Share Improve this question asked Dec 23, 2015 at 10:06 Vincent RoyeVincent Roye 1211 gold badge1 silver badge3 bronze badges 3
  • What have you tried regarding to debugging it ? Have you e.g. verified the input? – birgire Commented Dec 23, 2015 at 10:12
  • 1 1. Check if phone number is entered to user profile 2. Check if phone number is in database 3. Check if your meta_key is correct 4. Echo user ID to make sure it exists and is correct 5. Write the phone number down and if page has loaded, open dev window, select source code, press ctr+F and type in your number - maybe you just echoed it to hidden element or it's just hard to see – N00b Commented Dec 23, 2015 at 10:21
  • 1 "Not working" is simply not enough information. Do you have debugging enabled? Do you get any errors? Are you sure $current_user is a valid user object and in scope? Is the field name definitely phone_number? – TheDeadMedic Commented Dec 23, 2015 at 11:25
Add a comment  | 

5 Answers 5

Reset to default 4
<?php
  // number 9 will be user ID
  $all_meta_for_user = get_user_meta( 9 );
  print_r( $all_meta_for_user );

// find the key that you want
Array ( 
    [first_name] => Array ( [0] => Tom ) 
    [last_name] => Array ( [0] => Auger) 
    [nickname] => Array ( [0] => tomauger ) 
    [description] => etc.... 
)

// store it in a variable 
$last_name = $all_meta_for_user['last_name'][0];

// display it 
echo $last_name;

If you are not looping all users and only want to get current user phone_number meta then you can try the below.

$current_user_id = get_current_user_id();
$phone = get_user_meta($current_user_id,'phone_number',true);
echo $phone;

NOTE: This will work only for logged in user.

Also Check phone_number meta_key. By default wordpress doesn't have such meta key I guess.

WordPress has a shortcut for getting the current user ID, which it sounds like you need-- get_current_user_id(). Using that you should be able to get the information you need. The following is a proof of concept block of code that will check for the return values of the functions and apply some conditional logic in case you need to:

$uid = get_current_user_id();
if (!empty($uid)) {
  $phone = get_user_meta($uid,'phone_number',true); 
  if (!empty($phone)) {
    echo $phone;
  } else {
    echo 'User does not have a phone number stored in the database';
  }
} else {
  echo 'User is not logged in';
}
<?php $phone = get_user_meta($current_user->ID,'phone',true); echo $phone;?>

it was "phone" and not "phone_number"

$phone = get_user_meta($user_id, 'billing_phone', true);
echo $phone;

本文标签: contactHow to get current user39s phone number