WordPress user grid custom column sort

458 views Asked by At

I added a custom column to WordPress admin user grid area by adding this codes to functions.php

function new_modify_user_table( $column ) {
    $column['progress'] = 'Progress';
    return $column;
}
add_filter( 'manage_users_columns', 'new_modify_user_table',11 );

function new_modify_user_table_row( $val, $column_name, $user_id ) {
    switch ($column_name) {
        case 'progress' :
        $course_id=7238;
        $course = learn_press_get_course( $course_id );


$force             = true;
$num_of_decimal    = 0;
$current           = $course->evaluate_course_results( $user_id, $force );
$current           = absint( $current );

            return  '<strong style="color: green">'.$current.'%</strong>';
            break;
        default:
    }
    return $val;
}
add_filter( 'manage_users_custom_column', 'new_modify_user_table_row', 11, 3 );

It shows the column at the end of table as below

enter image description here

But it doesn't allow me to sort.

I added this code to add sort link to table header column

add_filter( 'manage_users_sortable_columns', 'my_sortable_cake_column' );
function my_sortable_cake_column( $columns ) {
    $columns['progress'] = 'progress';

    return $columns;
}

And also found where to trigger sort

add_action( 'pre_user_query', 'my_pre_user_query', 1 );
function my_pre_user_query( $query ) {
    global $wpdb, $current_screen;

    // Only filter in the admin
    if ( ! is_admin() )
        return;

    // Only filter on the users screen
    if ( ! ( isset( $current_screen ) && 'users' == $current_screen->id ) )
        return;

    // Only filter if orderby is set to 'progress'
    if ( isset( $query->query_vars ) && isset( $query->query_vars[ 'orderby' ] )
        && ( 'progress' == $query->query_vars[ 'orderby' ] ) ) {

        // We need the order - default is ASC
        $order = isset( $query->query_vars ) && isset( $query->query_vars[ 'order' ] ) && strcasecmp( $query->query_vars[ 'order' ], 'desc' ) == 0 ? 'DESC' : 'ASC';

         //need to write code to apply to grid 

    }

}

can someone help me write a code which set the sort to the grid

0

There are 0 answers