manchumahara

Sortable custom column in wordpress user list

Aug 28th, 2011
1,161
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Prepend the new column to the columns array
  2. function cb_user_extra_cols($cols) {
  3.         //$screen = get_current_screen();
  4.         //var_dump($screen);
  5.     $cols['rdrole']  = 'Relativerdes Role';        
  6.     return $cols;
  7. }
  8.  
  9. // Echo the ID for the new column
  10. function cb_user_extra_col_value($val, $column_name, $user_id) {        
  11.         $rdrole_name = '';
  12.     if ($column_name == 'rdrole'){
  13.             $rdrole = get_the_author_meta('rdrole',$user_id );
  14.             switch($rdrole){
  15.                 case 'driver':
  16.                     $rdrole_name = 'Driver';
  17.                     break;
  18.                 case 'parents':
  19.                     $rdrole_name = 'Parents';
  20.                     break;
  21.                 default:
  22.                     $rdrole_name = 'None';
  23.  
  24.             }
  25.         }
  26.         return $rdrole_name;
  27. }
  28.  
  29. function cb_user_extra_sortable_cols($columns) {
  30.   //var_dump($columns);
  31.   $custom = array(
  32.       // meta column id => sortby value used in query
  33.       'rdrole'    => 'rdrole',
  34.   );
  35.   return wp_parse_args($custom, $columns);
  36. }
  37.  
  38.  
  39.  
  40. function cb_user_extra_orderby( $vars ) {
  41.     if ( isset( $vars['orderby'] ) && 'rdrole' == $vars['orderby'] ) {
  42.             $vars = array_merge( $vars, array(
  43.                     'meta_key' => 'rdrole',
  44.                     'orderby' => 'meta_value'
  45.             ) );
  46.     }
  47.     return $vars;
  48. }
  49.  
  50. function cb_user_extra_col()
  51. {
  52.     //manage_users_sortable_columns
  53.     add_filter('manage_users_columns', 'cb_user_extra_cols');    
  54.     add_action('manage_users_custom_column', 'cb_user_extra_col_value', 10, 3);
  55.  
  56.     add_filter( 'manage_users_sortable_columns', 'cb_user_extra_sortable_cols' );
  57.     add_filter( 'request', 'cb_user_extra_orderby' );
  58. }
  59. add_action('admin_init', 'cb_user_extra_col');
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×