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 |5 Answers
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
版权声明:本文标题:contact - How to get current user's phone number 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744781041a2624714.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
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, pressctr+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$current_user
is a valid user object and in scope? Is the field name definitelyphone_number
? – TheDeadMedic Commented Dec 23, 2015 at 11:25