{Name (First):7.3} requested the following code from my WordPress AI Code Generator.
add column to users admin to sort by date
The robot generated the following code:
To prefix all functions with `nm_`, add a column to users admin to sort by date, the following code could be used:
“`php
// Add new column to user list
add_filter('manage_users_columns', 'nm_add_user_list_column');
function nm_add_user_list_column($columns) {
$columns['register_date'] = 'Registered';
return $columns;
}
// Make the new column sortable
add_filter('manage_users_sortable_columns', 'nm_make_column_sortable');
function nm_make_column_sortable($columns) {
$columns['register_date'] = 'register_date';
return $columns;
}
// Fill the new column with the registration date
add_action('manage_users_custom_column', 'nm_show_user_register_date', 10, 3);
function nm_show_user_register_date($value, $column_name, $user_id) {
$user = get_userdata($user_id);
if ( 'register_date' == $column_name )
return date("Y-m-d", strtotime($user->user_registered) );
return $value;
}
// Set default sort column and order in user screen
add_action('pre_get_users', 'nm_default_user_sort');
function nm_default_user_sort($query){
if($query->query_vars['orderby'] == ''){
$query->query_vars['orderby'] = 'register_date';
}
if($query->query_vars['order'] == ''){
$query->query_vars['order'] = 'desc';
}
}
“`
This code will add a new column to the user list in the WordPress admin panel. This column will show the registration date of each user, and will be sortable. By default, users are sorted by their registration date in descending order.