Thêm column và sortable column vào bảng thành viên

Nếu bạn muốn thêm một cột bất kỳ vào bảng danh sách người dùng, bạn có thể tham khảo đoạn code bên dưới.

function hpxf_manage_users_sortable_columns_filter( $column ) {
    $column['date']        = 'date';
    $column['collections'] = 'collections';
    $column['posts']       = 'posts';
    $column['forum_posts'] = 'forum_posts';
    $column['media_files'] = 'media_files';
    $column['description'] = 'description';

    return $column;
}

add_filter( 'manage_users_sortable_columns', 'hpxf_manage_users_sortable_columns_filter' );

function hpxf_manage_users_columns_filter( $column ) {
    $date = $column['date'] ?? '';

    if ( ! isset( $column['posts'] ) ) {
        $column['posts'] = __( 'Posts', 'pixelify' );
    }

    $lists = array();

    foreach ( $column as $key => $text ) {
        $lists[ $key ] = $text;

        if ( 'posts' == $key ) {
            $lists['forum_posts'] = __( 'Forum Posts', 'pixelify' );
        }
    }

    $column = $lists;
    $column['description'] = __( 'Description', 'pixelify' );
    $column['collections'] = __( 'Collections', 'pixelify' );
    $column['media_files'] = __( 'Media Files', 'pixelify' );

    if ( ! empty( $date ) ) {
        $column['date'] = $date;
    } else {
        $column['date'] = __( 'Date', 'pixelify' );
    }

    return $column;
}

add_filter( 'manage_users_columns', 'hpxf_manage_users_columns_filter' );

function hpxf_manage_users_custom_column_action( $val, $column_name, $user_id ) {
    global $wpdb;

    switch ( $column_name ) {
        case 'date' :
            return get_the_author_meta( 'registered', $user_id );
        case 'collections':
            return count_user_posts( $user_id, 'collection' );
        case 'description':
            return get_the_author_meta( 'description', $user_id );
        case 'forum_posts':
            return count_user_posts( $user_id, 'font_forum' );
        case 'media_files':
            $count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts WHERE post_type = 'attachment' AND post_author = $user_id" );

            return sprintf( '<a href="%s" target="_blank">%s</a>', esc_url( admin_url( 'upload.php?author=' . $user_id ) ), $count );
    }

    return $val;
}

add_filter( 'manage_users_custom_column', 'hpxf_manage_users_custom_column_action', 10, 3 );

function pxf_pre_user_query_action( $query ) {
    if ( $query instanceof WP_User_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;
        }

        $orderby = $_GET['orderby'] ?? '';

        // Only filter if orderby is set to 'media_files'
        if ( 'media_files' == $orderby ) {
            // We need the order - default is ASC
            $order = $_GET['order'] ?? 'ASC';

            // Order the posts by product count
            $query->query_orderby = "ORDER BY ( SELECT COUNT(*) FROM {$wpdb->posts} posts WHERE posts.post_type = 'attachment' AND posts.post_author = {$wpdb->users}.id GROUP BY posts.post_author ) {$order}";
        } elseif ( 'collections' == $orderby ) {
            // We need the order - default is ASC
            $order = $_GET['order'] ?? 'ASC';

            // Order the posts by product count
            $query->query_orderby = "ORDER BY ( SELECT COUNT(*) FROM {$wpdb->posts} posts WHERE posts.post_type = 'collection' AND posts.post_author = {$wpdb->users}.id GROUP BY posts.post_author ) {$order}";
        } elseif ( 'posts' == $orderby ) {
            // We need the order - default is ASC
            $order = $_GET['order'] ?? 'ASC';

            // Order the posts by product count
            $query->query_orderby = "ORDER BY ( SELECT COUNT(*) FROM {$wpdb->posts} posts WHERE posts.post_type = 'post' AND posts.post_author = {$wpdb->users}.id GROUP BY posts.post_author ) {$order}";
        } elseif ( 'forum_posts' == $orderby ) {
            // We need the order - default is ASC
            $order = $_GET['order'] ?? 'ASC';

            // Order the posts by product count
            $query->query_orderby = "ORDER BY ( SELECT COUNT(*) FROM {$wpdb->posts} posts WHERE posts.post_type = 'font_forum' AND posts.post_author = {$wpdb->users}.id GROUP BY posts.post_author ) {$order}";
        } elseif ( 'date' == $orderby ) {
            $order = $_GET['order'] ?? 'ASC';

            $query->query_orderby = "ORDER BY user_registered " . $order;
        } elseif ( 'description' == $orderby ) {
            $order = $_GET['order'] ?? 'ASC';

            // Join the usermeta table to get the 'description' field
            $query->query_from .= " LEFT JOIN {$wpdb->usermeta} ON {$wpdb->users}.ID = {$wpdb->usermeta}.user_id AND meta_key = 'description'";

            // Set the orderby parameter to sort by 'description'
            $query->query_orderby = "ORDER BY {$wpdb->usermeta}.meta_value {$order}";
        }
    }
}

add_action( 'pre_user_query', 'pxf_pre_user_query_action', 1 );

Cái này cũng tương tự như cách Thêm column và sortable column vào bảng danh sách bài viết. Chỉ khác là dùng các tên hook khác nhau áp dụng cho bảng users.

Không có bình luận.

Bạn có thể trở thành người đầu tiên để lại bình luận.

Để lại một bình luận

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *