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

在《删除/添加/调用WordPress用户个人资料的联系信息》,我们可以非常方便地自定义“联系信息”表单,但是那个方法有些弊端:只能新增到“联系信息”那里,不能添加自定义的描述文字(提示文本),只能是 input 表单。今天分享的方法就可以弥补这几个弊端,可以将字段添加到所有资料的最下面,支持添加描述文字,可以使用 input、textarea、select 等多种表单(前提是你会用)。下面是一个简单的样例,添加标题和两个 input 表单。

extra_user_profile_fields-wpdaxue_com

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

/**
 * WordPress <a href="https://www.wpdaxue.com/tag/%e4%b8%aa%e4%ba%ba%e8%b5%84%e6%96%99" title="查看与【个人资料】相关的文章" target="_blank" rel="noopener">个人资料</a>添加额外的字段
 * 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 ) { ?&gt;
&lt;h3&gt;&lt;?php _e(&quot;额外信息&quot;, &quot;blank&quot;); ?&gt;&lt;/h3&gt;
 
&lt;table class=&quot;form-table&quot;&gt;
	&lt;tr&gt;
		&lt;th&gt;&lt;label for=&quot;facebook&quot;&gt;&lt;?php _e(&quot;Facebook URL&quot;); ?&gt;&lt;/label&gt;&lt;/th&gt;
		&lt;td&gt;
			&lt;input type=&quot;text&quot; name=&quot;facebook&quot; id=&quot;facebook&quot; value=&quot;&lt;?php echo esc_attr( get_the_author_meta( 'facebook', $user-&gt;ID ) ); ?&gt;&quot; class=&quot;regular-text&quot; /&gt;&lt;br /&gt;
			&lt;span class=&quot;description&quot;&gt;&lt;?php _e(&quot;请输入您的 Facebook 地址&quot;); ?&gt;&lt;/span&gt;
		&lt;/td&gt;
	&lt;/tr&gt;
	&lt;tr&gt;
		&lt;th&gt;&lt;label for=&quot;twitter&quot;&gt;&lt;?php _e(&quot;Twitter&quot;); ?&gt;&lt;/label&gt;&lt;/th&gt;
		&lt;td&gt;
			&lt;input type=&quot;text&quot; name=&quot;twitter&quot; id=&quot;twitter&quot; value=&quot;&lt;?php echo esc_attr( get_the_author_meta( 'twitter', $user-&gt;ID ) ); ?&gt;&quot; class=&quot;regular-text&quot; /&gt;&lt;br /&gt;
			&lt;span class=&quot;description&quot;&gt;&lt;?php _e(&quot;请输入您的 Twitter 用户名&quot;); ?&gt;&lt;/span&gt;
		&lt;/td&gt;
	&lt;/tr&gt;
&lt;/table&gt;
&lt;?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用户个人资料的联系信息

来源:

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

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_32253.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?