要将字段添加到配置文件/用户编辑页面,需要使用edit_user_profile
挂钩和show_user_profile
钩住so:
add_action( \'show_user_profile\', \'my_extra_user_fields\' );
add_action( \'edit_user_profile\', \'my_extra_user_fields\' );
function my_extra_user_fields( $user )
{ ?>
<h3>User avatar</h3>
<table class="form-table">
<tr>
<th><label for="user_avatar">User avatar</label></th>
<td>
<input id="user_avatar" name="user_avatar" type="text" value="
<?php $user_avatar = get_the_author_meta( \'user_avatar\', $user->ID );
echo $user_avatar ? $user_avatar : \'\';?>" />
<span class="description"><?php _e("Please enter Avatar URL."); ?></span>
</td>
</tr>
</table>
<?php }
然后要保存该字段,需要使用
personal_options_update
挂钩和
edit_user_profile_update
钩住so:
add_action( \'personal_options_update\', \'save_my_extra_user_fields\' );
add_action( \'edit_user_profile_update\', \'save_my_extra_user_fields\' );
function save_my_extra_user_fields( $user_id )
{
if ( !current_user_can( \'edit_user\', $user_id ) ) { return false; }else{
if(isset($_POST[\'user_avatar\']) && $_POST[\'user_avatar\'] != ""){
update_usermeta( $user_id, \'user_avatar\', $_POST[\'user_avatar\'] );
}
}
}
正如您所知,您可以使用
the_author_meta()
或
get_user_meta()