WordPress 在个人资料页面添加额外的字段
WordPress 自带的个人资料字段在中国用的比较少,而我们常用的却没有,如果你想在 WordPress 个人资料页面创建额外的自带可以参考本文。
/**
*添加自定义表单
*/
function netnote_add_extra_social_links( $user ){
?>
<h3>更多</h3>
<table class="form-table">
<tr>
<th><label for="weibo">新浪微博</label></th>
<td><input type="text" name="weibo" value="<?php echo esc_attr( get_the_author_meta( 'weibo', $user->ID ) ); ?>" class="regular-text" /></td>
</tr>
<tr>
<th><label for="t_weibo">腾讯微博</label></th>
<td><input type="text" name="t_weibo" value="<?php echo esc_attr( get_the_author_meta( 't_weibo', $user->ID ) ); ?>" class="regular-text" /></td>
</tr>
<tr>
<th><label for="qq">QQ 号码</label></th>
<td>
<input type="text" name="qq" value="<?php echo esc_attr( get_the_author_meta( 'qq', $user->ID ) ); ?>" class="regular-text" />
<br>
<span class="description" for="qq">请输入您的 QQ 号码。</span>
</td>
</tr>
</table>
<?php
}
add_action( 'show_user_profile', 'netnote_add_extra_social_links' );
add_action( 'edit_user_profile', 'netnote_add_extra_social_links' );
/**
*保存自定义表单
*/
function netnote_save_extra_social_links( $user_id ){
update_user_meta( $user_id, 'weibo', sanitize_text_field( $_POST['weibo'] ) );
update_user_meta( $user_id, 't_weibo', sanitize_text_field( $_POST['t_weibo'] ) );
update_user_meta( $user_id, 'qq', sanitize_text_field( $_POST['qq'] ) );
}
add_action( 'personal_options_update', 'netnote_save_extra_social_links' );
add_action( 'edit_user_profile_update', 'netnote_save_extra_social_links' );
如果想获取保存的资料可以使用 get_the_author_meta() 函数,比如:
echo get_the_author_meta( 'weibo' );

