We use cookies to improve our services and provide a better experience. By continuing to use this site, you agree with our Cookies Policy.

Tutorials
More than 400 step-by-step articles to guide you through online project development.
HomeTutorialsCMS TutorialsWordPressOther

How to make extra columns in users.php sortable

Linux KVM Hosting
KVM VPS
Linux Cloud Server
cPanel KVM
Germany Cloud Servers
Brazil VPS
Ubuntu VPS
OpenVZ VPS
Windows Cloud Server
USA VPS

In order to make extra columns in user.php sortable, we will need to use additional PHP class to achieve this functionality. The class looks as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
if(!function_exists(load_sortable_user_meta_columns)){
add_action('admin_init', 'load_sortable_user_meta_columns');
function load_sortable_user_meta_columns(){
//THIS IS WHERE YOU ADD THE meta_key => display-title values
$args = array('nickname'=>'Nickname', 'user_registered'=>'Date Registered');
new sortable_user_meta_columns($args);
}
}
if(!class_exists(sortable_user_meta_columns)):
class sortable_user_meta_columns{
var $defaults = array('nicename', 'email', 'url', 'registered','user_nicename', 'user_email', 'user_url', 'user_registered','display_name','name','post_count','ID','id','user_login');
function __construct($args){
$this->args = $args;
add_action('pre_user_query', array(&$this, 'query'));
add_action('manage_users_custom_column', array(&$this, 'content'), 10, 3);
add_filter('manage_users_columns', array(&$this, 'columns'));
add_filter( 'manage_users_sortable_columns', array(&$this, 'sortable') );
}
function query($query){
$vars = $query->query_vars;
if(in_array($vars['orderby'], $this->defaults)) return;
$title = $this->args[$vars['orderby']];
if(!empty($title)){
$query->query_from .= " LEFT JOIN wp_usermeta m ON (wp_users.ID = m.user_id AND m.meta_key = '$vars[orderby]')";
$query->query_orderby = "ORDER BY m.meta_value ".$vars['order'];
}
}
function columns($columns) {
foreach($this->args as $key=>$value){
$columns[$key] = $value;
}
return $columns;
}
function sortable($columns){
foreach($this->args as $key=>$value){
$columns[$key] = $key;
}
return $columns;
}
function content($value, $column_name, $user_id) {
$user = get_userdata( $user_id );
return $user->$column_name;
}
}
endif;

The mentioned code needs to be added in the file users.php which is located in WordPress installation wp-admin subfolder.

Windows Cloud Server
cPanel KVM
USA VPS
Brazil VPS
Ubuntu VPS
Linux Cloud Server
OpenVZ VPS
KVM VPS
Linux KVM Hosting
Germany Cloud Servers
Rate this Tutorial:
No Comments Leave a Comment
Leave a Comment

Other (182)

Popular Keywords