Create a sortable Signup Date List in User List

Hello,

to combat Spam we regularly check for the newest WordPress Users. Though you can’t sort by default by their signup date. This Code Snippet adds another row in the User List with the Signup Date in a sortable list.

// Add a new column for Registration Date
add_filter('manage_users_columns', 'add_registration_date_column');
function add_registration_date_column($columns) {
    $columns['registration_date'] = 'Signup Date';
    return $columns;
}

// Populate the Registration Date column
add_action('manage_users_custom_column', 'show_registration_date_column', 10, 3);
function show_registration_date_column($value, $column_name, $user_id) {
    if ('registration_date' === $column_name) {
        $user = get_userdata($user_id);
        $value = date('j M, Y H:i', strtotime($user->user_registered));
    }
    return $value;
}

// Make the Registration Date column sortable
add_filter('manage_users_sortable_columns', 'make_registration_date_sortable');
function make_registration_date_sortable($columns) {
    $columns['registration_date'] = 'registered';
    return $columns;
}

// Adjust query to sort by Registration Date
add_action('pre_get_users', 'sort_by_registration_date');
function sort_by_registration_date($query) {
    if (!is_admin() || !$query->is_main_query()) {
        return;
    }

    if ('registered' === $query->get('orderby')) {
        $query->set('orderby', 'registered');
    }
}

Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *