您需要使用show_user_profile
, edit_user_profile
, personal_options_update
, 和edit_user_profile_update
挂钩。
您可以使用以下代码在用户部分中添加其他字段
Code for adding extra fields in Edit User Section:
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("Extra profile information", "blank"); ?></h3>
<table class="form-table">
<tr>
<th><label for="address"><?php _e("Address"); ?></label></th>
<td>
<input type="text" name="address" id="address" value="<?php echo esc_attr( get_the_author_meta( \'address\', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description"><?php _e("Please enter your address."); ?></span>
</td>
</tr>
<tr>
<th><label for="city"><?php _e("City"); ?></label></th>
<td>
<input type="text" name="city" id="city" value="<?php echo esc_attr( get_the_author_meta( \'city\', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description"><?php _e("Please enter your city."); ?></span>
</td>
</tr>
<tr>
<th><label for="postalcode"><?php _e("Postal Code"); ?></label></th>
<td>
<input type="text" name="postalcode" id="postalcode" value="<?php echo esc_attr( get_the_author_meta( \'postalcode\', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description"><?php _e("Please enter your postal code."); ?></span>
</td>
</tr>
</table>
<?php }
Code for saving extra fields details in database:
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 ( empty( $_POST[\'_wpnonce\'] ) || ! wp_verify_nonce( $_POST[\'_wpnonce\'], \'update-user_\' . $user_id ) ) {
return;
}
if ( !current_user_can( \'edit_user\', $user_id ) ) {
return false;
}
update_user_meta( $user_id, \'address\', $_POST[\'address\'] );
update_user_meta( $user_id, \'city\', $_POST[\'city\'] );
update_user_meta( $user_id, \'postalcode\', $_POST[\'postalcode\'] );
}
还有一些关于这个主题的博客帖子可能会有所帮助: