修改自定义用户管理页面

时间:2013-04-02 作者:Giraldi

我想创造一个submenu page 在\'Users\' 与“所有用户”页面类似,但for listing users of specific roles only. 让我们调用此子菜单页\'Customer\'.

到目前为止,我设法创建了一个具有自己列的列,但仍有一些内容我没有设法修改to make the contents more relevant:

Custom User Page

以下是我需要做的:我该怎么做change the page title 要反映子菜单名称,在本例中为“Customer”filter links above the list to show ONLY \'All\', \'Subscriber\', & \'Pending\'? (b) 还有\'Change role to..\' dropdown menu?\'All\' to direct to the \'Customer\' page (哪个是自定义用户页)\'Customer\' submenu link to hilite 是否指示当前查看的页面NOTE:
默认\'All Users\' 因此,页面应为the opposite 本页的filter links.

以下是我创建上述页面的大致方式:

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

    }
}

1 个回复
最合适的回答,由SO网友:Giraldi 整理而成

下面是我如何修改页面的。对于submenu highlighting, 然而,我还没有弄明白,所以我resort to 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);

结束

相关推荐

我可以将两个角色传递给GET_USERS函数吗?

我想获得角色学术和学生的用户。我已经尝试使用下面的代码(传递了一系列角色,但显示了所有用户,因此显然不起作用。$roles=array(\'academic\',\'student\'); $args =array(\'role\'=>$roles); $users=get_users( $args ); foreach ($users as $user) { echo \'<li>\' . $user->user_email .