3

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:

Custom User Page

Here's what I need to do:

  1. How do I change the page title to reflect the submenu name, in this case 'Customer'?
  2. (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?
  3. How do I change the filter link 'All' to direct to the 'Customer' page (which is the custom users page)?
  4. 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
        );

    }
}
3

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:

/* - 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);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.