WordPress用户网格自定义列排序

时间:2017-09-02 10:43:08

标签: php wordpress add-filter wp-admin

我将此代码添加到functions.php

,为WordPress管理员用户网格区域添加了一个自定义列
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 );

它显示表格末尾的列,如下所示

enter image description here

但它不允许我排序。

我添加了此代码以添加到表标题列的排序链接

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

    return $columns;
}

还找到了触发排序的位置

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 

    }

}

有人可以帮我编写一个代码,将排序设置为网格

0 个答案:

没有答案