首页 论坛 如何给WordPress网站用户添加自定义资料(电话、微信等信息)
帖子详情

通常情况下,Wordpress网站用户资料比较少,有时为了做网站的需要,我们要给Wordpress网站用户添加自定义资料,例如:电话、微信等信息项。如下图:

Wordpress网站用户添加自定义资料

如何给Wordpress网站用户添加自定义资料呢?下面学做网站论坛介绍一下添加方法。

第一步:将以下的代码放到自己用的Wordpress模板的模板函数文件functions.php里面;

//设置个人资料相关选项https://www.52diyhome.com/
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');

第二步:将functions.php保存,保存后,后台用户资料就会多出这些信息填写项了。

第三步:在模板文件里调用。如果你想把这些信息显示在网站前台,可以使用以下的代码进行调用。

微信号:<?php the_author_meta('weixinhao'); ?>

也可以用下面的代码来调用信息:

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

第四步:判断输出,判断当前用户是否填写了些项数据,如果填写了就输出,如果没有填写就不输出。

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

补充,除了以上的代码来调用用户信息之外,还可以使用WordPress 获取当前/指定用户资料信息。

还可以使用以下的代码来添加用户资料自定义项。

/**
* WordPress 个人资料添加额外的字段
*/

add_action( 'show_user_profile', 'extra_user_profile_fields' );
add_action( 'edit_user_profile', 'extra_user_profile_fields' );

function extra_user_profile_fields( $user ) { ?>
<h3><?php _e("用户信息", "blank"); ?></h3>

<table class="form-table">
<tr>
<th><label for="weibo"><?php _e("weibo URL"); ?></label></th>
<td>
<input type="text" name="weibo" id="weibo" value="<?php echo esc_attr( get_the_author_meta( 'weibo', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description"><?php _e("请输入您的 weibo 地址"); ?></span>
</td>
</tr>
<tr>
<th><label for="twitter"><?php _e("Twitter"); ?></label></th>
<td>
<input type="text" name="twitter" id="twitter" value="<?php echo esc_attr( get_the_author_meta( 'twitter', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description"><?php _e("请输入您的 Twitter 用户名"); ?></span>
</td>
</tr>
</table>
<?php }

add_action( 'personal_options_update', 'save_extra_user_profile_fields' );
add_action( 'edit_user_profile_update', 'save_extra_user_profile_fields' );

function save_extra_user_profile_fields( $user_id ) {

if ( !current_user_can( 'edit_user', $user_id ) ) { return false; }

update_usermeta( $user_id, 'weibo', $_POST['weibo'] );
update_usermeta( $user_id, 'twitter', $_POST['twitter'] );
}

版权:言论仅代表个人观点,不代表官方立场。转载请注明出处:https://www.52diyhome.com/forum/65523.html

发表评论
暂无评论
  • 0 +

    访问总数

  • 0 +

    会员总数

  • 0 +

    资源总数

  • 0 +

    今日发布

  • 0 +

    本周发布

  • 0 +

    运行天数

资源在于分享,创作来源想象