查看 user-edit.php 中的所有用户元数据或如何覆盖
Viewing all user metadata in user-edit.php or how to override
我在用户的帐户页面中添加了一些元数据。
我希望此数据在显示用户信息的 user-edit.php
页面中显示和编辑。
我想到修改这个文件,后来发现如果有WordPress更新,这个文件会被覆盖。
我应该怎么做?
这很简单,应该与用于添加自定义用户配置文件字段的代码一起完成。
下面的代码块会将自定义字段添加到用户个人资料中:
add_action( 'show_user_profile', 'my_custom_user_profile_field' );
add_action( 'edit_user_profile', 'my_custom_user_profile_field' );
function my_custom_user_profile_field( $user ) { ?>
<h3>Custom Field</h3>
<table class="form-table">
<tr>
<th><label for="my-custom-user-profile-field">Input Label:</label></th>
<td>
<input name="my-custom-user-profile-field" id="my-custom-user-profile-field" value="<?php echo esc_attr( get_the_author_meta( 'my-custom-user-profile-field', $user->ID ) ); ?>" class="regular-text" type="text">
</td>
</tr>
</table>
<?php }
然后您需要确保可以保存您添加的字段。您可以像这样连接到 personal_options_update
和 edit_user_profile_update
来做到这一点:
add_action( 'personal_options_update', 'save_my_custom_user_profile_field' );
add_action( 'edit_user_profile_update', 'save_my_custom_user_profile_field' );
function save_my_custom_user_profile_field( $user_id ) {
if ( !current_user_can( 'edit_user', $user_id ) )
return false;
update_user_meta( absint( $user_id ), 'my-custom-user-profile-field', wp_kses_post( $_POST['my-custom-user-profile-field'] ) );
}
我在用户的帐户页面中添加了一些元数据。
我希望此数据在显示用户信息的 user-edit.php
页面中显示和编辑。
我想到修改这个文件,后来发现如果有WordPress更新,这个文件会被覆盖。
我应该怎么做?
这很简单,应该与用于添加自定义用户配置文件字段的代码一起完成。
下面的代码块会将自定义字段添加到用户个人资料中:
add_action( 'show_user_profile', 'my_custom_user_profile_field' );
add_action( 'edit_user_profile', 'my_custom_user_profile_field' );
function my_custom_user_profile_field( $user ) { ?>
<h3>Custom Field</h3>
<table class="form-table">
<tr>
<th><label for="my-custom-user-profile-field">Input Label:</label></th>
<td>
<input name="my-custom-user-profile-field" id="my-custom-user-profile-field" value="<?php echo esc_attr( get_the_author_meta( 'my-custom-user-profile-field', $user->ID ) ); ?>" class="regular-text" type="text">
</td>
</tr>
</table>
<?php }
然后您需要确保可以保存您添加的字段。您可以像这样连接到 personal_options_update
和 edit_user_profile_update
来做到这一点:
add_action( 'personal_options_update', 'save_my_custom_user_profile_field' );
add_action( 'edit_user_profile_update', 'save_my_custom_user_profile_field' );
function save_my_custom_user_profile_field( $user_id ) {
if ( !current_user_can( 'edit_user', $user_id ) )
return false;
update_user_meta( absint( $user_id ), 'my-custom-user-profile-field', wp_kses_post( $_POST['my-custom-user-profile-field'] ) );
}