首页 > WordPress > WordPress网站用户添加自定义资料(电话、微信等信息)调用当前/指定用户资料信息

WordPress网站用户添加自定义资料(电话、微信等信息)调用当前/指定用户资料信息

时间:2022年6月14日 分类:WordPress 浏览量:516

WordPress的网站用户资料比较少,比如手机号码、微信号等,我们可以给用户增加自定义字段,和此前给网站基础设置增加自定义字段是类似的,参考文章:WordPress设置常规下添加自定义参数

自定义用户资料

那么在本文,我们可以通过以下方式给用户增加自定义字段,functions.php里添加:

//设置个人资料相关选项
function my_profile( $contactmethods ) {
$contactmethods['tellphone'] = '电话号码'; //增加
$contactmethods['qqhao'] = 'QQ号';
$contactmethods['weixinhao'] = '微信号';
unset($contactmethods['aim']); //删除
unset($contactmethods['yim']);
unset($contactmethods['jabber']);
return $contactmethods;
}
add_filter('user_contactmethods','my_profile');

如此我们可以在后台给每个用户添加信息了。

那么怎么调用呢?比如微信号:

<?php the_author_meta('weixinhao'); ?>

当然,也可以用下面的方式

电话号码: <?php echo $current_user->tellphone; ?>,
Q Q号码: <?php echo $current_user->qqhao; ?>,
微信号码: <?php echo $current_user->weixinhao; ?>,

如果没有填写的,调用方式则是:

<?php if (get_the_author_meta('weixinhao')!=""){ ?>
<?php echo "微信号:" . get_the_author_meta('weixinhao') ; ?>
<?php } ?>

调用当前用户信息

<?php global $current_user;
get_currentuserinfo();
echo '用户名: ' . $current_user->user_login . "\n";
echo '用户邮箱: ' . $current_user->user_email . "\n";
echo '名字: ' . $current_user->user_firstname . "\n";
echo '姓氏: ' . $current_user->user_lastname . "\n";
echo '公开显示名: ' . $current_user->display_name . "\n";
echo '用户 ID:' . $current_user->ID . "\n";
?>

这样,我们可以调用到当前用户的各类信息,如果需要指定用户,则是

$user = get_user_by( 'login', 'admin8' );//获取用户admin8所有信息数组
$tellphone =$user->tellphone;//用户电话
$qqhao =$user->qqhao;//QQ号
$weixinhao =$user->weixinhao;//微信号
$dailititle =$user->dailititle;//代理网站名

这样,可以调用指定用户信息

觉得文章有用就打赏一下文章作者

微信扫一扫打赏

标签: