I’m trying to create a submenu page under ‘Users‘ which is like the ‘All Users’ page but for listing users of specific roles only. Let’s just call this submenu page ‘Customer’.
So far I managed to create one with its own columns, but there are still some things that I haven’t managed to modify to make the contents more relevant:
Here’s what I need to do:
- How do I change the page title to reflect the submenu name, in this case ‘Customer’?
- (a) How do I change the filter links above the list to show ONLY ‘All’, ‘Subscriber’, & ‘Pending’? (b) And also the ‘Change role to..’ dropdown menu?
- How do I change the filter link ‘All’ to direct to the ‘Customer’ page (which is the custom users page)?
- How do I make the ‘Customer’ submenu link to hilite to indicate the current page viewed?
NOTE:
The default ‘All Users‘ page should therefore be the
opposite of this page in terms of the filter links.
Here’s roughly how I created the above page:
/* Create new user submenu with custom parameter in slug */
add_action('admin_menu', 'my_custom_user_submenu');
function my_custom_user_submenu() {
add_users_page(
'Customer',
'Customer',
list_users,
'users.php?user=custom'
);
}
/* Filter users according to the users list page */
add_action('pre_user_query','my_user_customer_list');
function my_user_customer_list($user_search) {
$user = $_GET['custom'];
$role = $_GET['role'];
if ( $user == 'custom' OR $role == 'subscriber' OR $role == 'pending' ) {
global $wpdb;
/* For Customers Page:
* Get only 'Subscriber' & 'Pending' users
* */
$user_search->query_where =
str_replace('WHERE 1=1',
"WHERE 1=1 AND {$wpdb->users}.ID IN (
SELECT {$wpdb->usermeta}.user_id FROM $wpdb->usermeta
WHERE {$wpdb->usermeta}.meta_key = '{$wpdb->prefix}user_level'
AND {$wpdb->usermeta}.meta_value = 0)",
$user_search->query_where
);
} else {
global $wpdb;
/* For User Admin Page:
* Get all apart from 'Subscriber' & 'Pending' users
* */
$user_search->query_where =
str_replace('WHERE 1=1',
"WHERE 1=1 AND {$wpdb->users}.ID IN (
SELECT {$wpdb->usermeta}.user_id FROM $wpdb->usermeta
WHERE {$wpdb->usermeta}.meta_key = '{$wpdb->prefix}user_level'
AND {$wpdb->usermeta}.meta_value >= 1)",
$user_search->query_where
);
}
}
Here’s how I managed to modify the page. For the submenu highlighting, however, I haven’t managed to figure it out, so I resort to jQuery.
Here’s the code: