mko
10/17/2017 - 8:08 AM

Aggiungi colonna in lista utenti admin From http://jasonjalbuena.com/wordpress-add-and-sort-custom-column-in-users-admin-page/

//add additional columns to the users.php admin page
function modify_user_columns($column) {
    $column = array(
        "cb" => "",
        "username" => "Username",
        "church" => "Church",//the new column
        "email" => "E-mail",
        "role" => "Role"
    );
    return $column;
}
add_filter('manage_users_columns','modify_user_columns');
 
//add content to your new custom column
function modify_user_column_content($val,$column_name,$user_id) {
    $user = get_userdata($user_id);
    switch ($column_name) {
        case 'church':
            return $user->church_name;
            break;
        //I have additional custom columns, hence the switch. But am only showing one here
        default:
    }
    return $return;
}
add_filter('manage_users_custom_column','modify_user_column_content',10,3);
 
//make the new column sortable
function user_sortable_columns( $columns ) {
    $columns['church'] = 'church';
    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_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
    }
}