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

在主题的 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 ) { ?>
<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'] );
} |
/**
* 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 ) { ?>
<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用户个人资料的联系信息》
来源:
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