我在我的网站中创建了一个特殊表单,允许我的用户输入密钥.我add_user_meta()用来将元数据添加到数据库中.我希望能够在管理中心点击用户时看到此密钥.
我该如何添加到此列?
以下是使用的元数据信息
add_user_meta($userId,'code','12345');
Run Code Online (Sandbox Code Playgroud)
我们只是希望能够将其添加到显示用户名电子邮件和角色的表中users.php的视图中.
我使用这样的代码来显示用户ID,但我无法弄清楚如何显示他们的元.
add_filter('manage_users_columns', 'pippin_add_user_id_column');
function pippin_add_user_id_column($columns) {
$columns['user_id'] = 'User ID';
return $columns;
}
add_action('manage_users_custom_column', 'pippin_show_user_id_column_content', 10, 3);
function pippin_show_user_id_column_content($value, $column_name, $user_id) {
$user = get_userdata( $user_id );
if ( 'user_id' == $column_name )
return $user_id;
return $value;
}
Run Code Online (Sandbox Code Playgroud)
Dan*_*jel 17
这个例子是在WordPress codex的这两个页面的帮助下创建的.
https://codex.wordpress.org/Plugin_API/Action_Reference/edit_user_profile https://codex.wordpress.org/Plugin_API/Action_Reference/personal_options_update
它用于显示和更新自定义用户元数据.
<?php
// Hooks near the bottom of profile page (if current user)
add_action('show_user_profile', 'custom_user_profile_fields');
// Hooks near the bottom of the profile page (if not current user)
add_action('edit_user_profile', 'custom_user_profile_fields');
// @param WP_User $user
function custom_user_profile_fields( $user ) {
?>
<table class="form-table">
<tr>
<th>
<label for="code"><?php _e( 'Custom Meta' ); ?></label>
</th>
<td>
<input type="text" name="code" id="code" value="<?php echo esc_attr( get_the_author_meta( 'code', $user->ID ) ); ?>" class="regular-text" />
</td>
</tr>
</table>
<?php
}
// Hook is used to save custom fields that have been added to the WordPress profile page (if current user)
add_action( 'personal_options_update', 'update_extra_profile_fields' );
// Hook is used to save custom fields that have been added to the WordPress profile page (if not current user)
add_action( 'edit_user_profile_update', 'update_extra_profile_fields' );
function update_extra_profile_fields( $user_id ) {
if ( current_user_can( 'edit_user', $user_id ) )
update_user_meta( $user_id, 'code', $_POST['code'] );
}
?>
Run Code Online (Sandbox Code Playgroud)