删除/添加/调用WordPress用户个人资料的联系信息

在自己的设置页添加几个设置项目=

如果你要折腾主题或者将WordPress站点开放注册,你可能需要自定义WordPress用户个人资料信息。下面倡萌将简单说一下如何删除、添加和调用自定义用户信息字段。

来源于:

https://www.wpdaxue.com/add-remove-display-wordpress-user-profile-fields.html

/**
 * 自定义用户个人资料信息
 * https://www.wpdaxue.com/add-remove-display-wordpress-user-profile-fields.html
 */
add_filter( 'user_contactmethods', 'wpdaxue_add_contact_fields' );
function wpdaxue_add_contact_fields( $contactmethods ) {
	$contactmethods['qq'] = 'QQ';
	$contactmethods['qm_mailme'] = 'QQ邮箱“邮我”';
	$contactmethods['qq_weibo'] = '腾讯微博';
	$contactmethods['sina_weibo'] = '新浪微博';
	$contactmethods['twitter'] = 'Twitter';
	$contactmethods['google_plus'] = 'Google+';
	$contactmethods['donate'] = '赞助链接';
	unset( $contactmethods['yim'] );
	unset( $contactmethods['aim'] );
	unset( $contactmethods['jabber'] );
	return $contactmethods;
}

以上代码通过 user_contactmethods 这个钩子添加了QQ等多个自定义字段,同时移除了 yim、aim和jabber,用法一目了然,就不多说,效果如下图所示:

删除/添加/调用WordPress用户个人资料的联系信息

如果要调用上面的字段,只需要使用 the_author_meta() 或 get_the_author_meta() 这两个函数即可。

the_author_meta() 直接打印输出字段值

get_the_author_meta() 返回字段值给其他函数调用

注:一般而言,WordPress大多数函数都有类似这两种,一个带 get_ 前缀,一个没有,两者用法的区别如上所说。

比如我们要调用QQ字段,可以使用使用下面的代码:

<?php
//打印输出QQ字段的值
the_author_meta( 'qq' );
//或者下面的
echo get_the_author_meta( 'qq' );
?>

我们在实际使用的时候,最好先通过 IF 语句判断用户是否填写了 QQ 这个字段(即判断QQ字段是否存在值),如果填写了,就输出,否者不输出

<?php if ( get_the_author_meta( 'qq' ) ){
	echo '作者QQ:'.get_the_author_meta( 'qq' );
}

关于调用更多默认的字段,建议大家自己参考 the_author_meta() 和 get_the_author_meta()

注:在没有指定用户ID等明确信息时,以上两个函数只能在循环(Loop)内才能正常使用。

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

来源于:

https://www.wpdaxue.com/extra-user-profile-fields.html

在以上的方法中,我们可以非常方便地自定义“联系信息”表单,但是那个方法有些弊端:

只能新增到“联系信息”那里,不能添加自定义的描述文字(提示文本),只能是 input 表单。

今天分享的方法就可以弥补这几个弊端,可以将字段添加到所有资料的最下面,支持添加描述文字,可以使用 input、textarea、select 等多种表单(前提是你会用)。

下面是一个简单的样例,添加标题和两个 input 表单。

删除/添加/调用WordPress用户个人资料的联系信息

在主题的 functions.php 里添加下面的代码:

/**
 * WordPress 个人资料添加额外的字段
 * https://www.wpdaxue.com/extra-user-profile-fields.html
 */
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="facebook"><?php _e("Facebook URL"); ?></label></th>
		<td>
			<input type="text" name="facebook" id="facebook" value="<?php echo esc_attr( get_the_author_meta( 'facebook', $user->ID ) ); ?>" class="regular-text" /><br />
			<span class="description"><?php _e("请输入您的 Facebook 地址"); ?></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, 'facebook', $_POST['facebook'] );
	update_usermeta( $user_id, 'twitter', $_POST['twitter'] );
}

代码中使用了 show_user_profile 和 edit_user_profile 这两个钩子将表单挂载到个人资料页面,然后使用 ‘personal_options_update’ 和 ‘edit_user_profile_update’ 这两个钩子挂载新添加的字段到更新操作,其中使用 update_usermeta() 这个函数来更新字段信息。

如果你是开发者,相信你自己可以添加其他的表单类型,倡萌就不献丑了。

教程

WordPress 禁止用户注册某些用户名

2019-5-1 0:12:30

教程

RevolverMaps - 给网站添加一个小地球

2019-5-8 20:21:52

⚠️
Npcink上的部份代码及教程来源于互联网,仅供网友学习交流,若您喜欢本文可附上原文链接随意转载。
无意侵害您的权益,请发送邮件至 1355471563#qq.com 或点击右侧 私信:Muze 反馈,我们将尽快处理。
0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索