1

I'm trying that :

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

But it's not working

  • What have you tried regarding to debugging it ? Have you e.g. verified the input? – birgire Dec 23 '15 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 Dec 23 '15 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 Dec 23 '15 at 11:25
2

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.

2
<?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;
2

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';
}
0
<?php $phone = get_user_meta($current_user->ID,'phone',true); echo $phone;?>

it was "phone" and not "phone_number"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.