J'essaie de créer une pagesous-menusous 'Users', qui ressemble à la page 'Tous les utilisateurs' maispour répertorier les utilisateurs de rôles seulement. Appelons simplement cette page de sous-menu'Client'.
Jusqu'ici, j'ai réussi à en créer une avec ses propres colonnes, mais il y a encore des choses que je n'ai pas encore réussi à modifierpour rendre le contenu plus pertinent:
NOTE:
La page par défaut 'All Users' doit donc êtrel'opposéde cette page en termes deliens de filtrage.
/* 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
);
}
}
Voici comment j'ai réussi à modifier la page. Pour le sous-menu soulignant , cependant, je n’ai pas réussi à le comprendre, donc je ai recours à jQuery .
/* - Set user filter links according to users pages
* - Set Role Change dropdown menu
* */
function custom_user_filter_links( $views ) {
global $wp_roles; // For Role Change dropdown menu
$amt = count_users();
$amtCustom = $amt['avail_roles']['subscriber'] + $amt['avail_roles']['pending']; // Count custom users
if ( $_GET['user'] == 'custom' || $_GET['role'] == 'subscriber' || $_GET['role'] == 'pending' ) {
/* For Customers Page: */
/* Modify url & users count for the 'All' link */
$all = preg_replace( '/(.*\.php)(.*\().*(\).*)/', '$1?user=custom${2}'.$amtCustom.'$3', $views['all'] );
/* Show only 'Subscriber' & 'Pending' user links */
$arr = array();
$arr['all'] = $all;
if ($views['subscriber'])
$arr['subscriber'] = $views['subscriber'];
if ($views['pending'])
$arr['pending'] = $views['pending'];
$views = $arr;
/* Modify Role Change dropdown menu */
$arr = array();
$arr['subscriber'] = $wp_roles->roles['subscriber'];
$arr['pending'] = $wp_roles->roles['pending'];
$wp_roles->roles = $arr;
} else {
/* For User Admin Page: */
/* Modify users count for the 'All' link*/
$views['all'] = preg_replace( '/(.*\().*(\).*)/', '${1}'.($amt['total_users'] - $amtCustom).'$2', $views['all'] );
/* Remove 'Subscriber' & 'Pending' user links */
unset($views['subscriber']);
unset($views['pending']);
/* Modify Role Change dropdown menu */
unset ( $wp_roles->roles['subscriber'] );
unset ( $wp_roles->roles['pending'] );
}
return $views;
}
add_filter( 'views_users', 'custom_user_filter_links' );
/* - Highlight custom submenu
* - Change titles of custom Users Manage & Edit pages
* */
function submenu_hilite_n_rename_title() {
global $current_screen;
// If not in specified pages than discontinue
if( !($current_screen->base == 'users' || $current_screen->base == 'user-edit') )
return;
global $title;
if ($current_screen->base == 'user-edit') {
/* For Edit User page */
$user = get_user_by('id', $_GET['user_id']);
$role = $user->roles[0];
$title = 'Edit Customer'; /* Change page title */
} else {
/* For Manage Users page */
$role = $_GET['role'];
$title = 'Customers'; /* Change page title */
}
switch (true) {
case $_GET['user'] == 'custom' :
case $role == 'subscriber' :
case $role == 'pending' :
?>
<script type="text/javascript">
jQuery(document).ready( function($) {
var reference = $('a[href$="user=custom"]').parent();
// Highlight custom submenu
reference.addClass('current');
// Remove highlight from default menu
reference.parent().find('li:first').removeClass('current');
});
</script>
<?php
break;
}
}
add_action('admin_head', submenu_hilite_n_rename_title);