Created
May 4, 2018 07:53
-
-
Save kartick14/30d2d2740f147978897f7266716f9ac7 to your computer and use it in GitHub Desktop.
ADD AND SORT CUSTOM COLUMN IN USERS ADMIN PAGE
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//add additional columns to the users.php admin page | |
add_filter('manage_users_columns', 'project_add_user_id_column'); | |
function project_add_user_id_column($columns) { | |
$columns['church'] = 'Church name'; | |
// unset($columns['pmpro_membership_level']); //For remove a column | |
/*$columns = array( | |
"cb" => "", | |
"username" => "Username", | |
"church" => "Church",//the new column | |
"email" => "E-mail", | |
"role" => "Role" | |
);*/ // For ordering | |
return $columns; | |
} | |
//add content to your new custom column | |
add_action('manage_users_custom_column', 'project_show_user_id_column_content', 10, 3); | |
function project_show_user_id_column_content($value, $column_name, $user_id) { | |
$user = get_userdata( $user_id ); | |
if ( 'church' == $column_name ) | |
return $user->church_name; | |
return $value; | |
} | |
//make the new column sortable | |
function user_sortable_columns( $columns ) { | |
$columns['church'] = 'Church name'; | |
return $columns; | |
} | |
add_filter( 'manage_users_sortable_columns', 'user_sortable_columns' ); | |
//set instructions on how to sort the new column | |
if(is_admin()) {//prolly not necessary, but I do want to be sure this only runs within the admin | |
add_action('pre_user_query', 'my_user_query'); | |
} | |
function my_user_query($userquery){ | |
if('church'==$userquery->query_vars['orderby']) {//check if church is the column being sorted | |
global $wpdb; | |
//$userquery->query_orderby = " ORDER BY ID ".($userquery->query_vars["order"] == "ASC" ? "asc " : "desc ");//set sort order | |
$userquery->query_from .= " LEFT OUTER JOIN $wpdb->usermeta AS alias ON ($wpdb->users.ID = alias.user_id) ";//note use of alias | |
$userquery->query_where .= " AND alias.meta_key = 'church_name' ";//which meta are we sorting with? | |
$userquery->query_orderby = " ORDER BY alias.meta_value ".($userquery->query_vars["order"] == "ASC" ? "asc " : "desc ");//set sort order | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment