Thêm column và sortable column vào bảng danh sách bài viết

Lại Đình Cường

Tài liệu thiết kế website

Đoạn code ví dụ cách thêm column và sortable column vào bảng quản lý danh sách bài viết trong admin.

add_filter( 'manage_posts_columns', 'hocwp_theme_custom_post_columns_filter' );
function hocwp_theme_custom_post_columns_filter( $columns ) {
    $date = $columns['date'];
    unset( $columns['date'] );
    $columns['type'] = __( 'Type', 'rentinsingapore' );

    $columns['date'] = $date;

    return $columns;
}

add_action( 'manage_posts_custom_column', 'hocwp_theme_custom_post_column_action', 10, 2 );
function hocwp_theme_custom_post_column_action( $column, $post_id ) {
    switch ( $column ) {
        // display a thumbnail photo
        case 'type' :
            $type = get_post_meta( $post_id, 'type', true );

            if ( empty( $type ) ) {
                $type = 'room';
            }

            echo $type;
            break;
    }
}

add_filter( 'manage_edit-post_sortable_columns', 'hocwp_theme_custom_post_sortable_columns_filter' );
function hocwp_theme_custom_post_sortable_columns_filter( $columns ) {
    $columns['type'] = 'type';

    return $columns;
}

add_action( 'pre_get_posts', 'hocwp_theme_custom_sort_post_column' );
function hocwp_theme_custom_sort_post_column( $query ) {
    if ( ! is_admin() ) {
        return;
    }

    $orderby = $query->get( 'orderby' );

    if ( 'type' == $orderby ) {
        $query->set( 'meta_key', 'type' );
        $query->set( 'orderby', 'meta_value' );
    }
}

Thêm đoạn code bên trên vào tập tin functions.php của giao diện hoặc plugin.

Thêm column và sortable column vào bảng danh sách bài viết

Trong ví dụ bên trên, cột Type sẽ được thêm vào bảng danh sách bài viết. Khi nhấn chuột vào cột Type sẽ sắp xếp danh sách theo giá trị của cột.

Ở đây, tên cột sắp xếp đặt trùng với tên meta key lưu trong cơ sở dữ liệu nên giá trị type của tham số orderby và trường thiết lập meta_key của query là giống nhau.

Ví dụ này chỉ áp dụng cho post_type là post. Đối với page và các custom post type khác thì các hook sẽ thay đổi một chút thì mới có thể hoạt động.

Nếu chỉ áp dụng cho post type là post (bài viết) thì sử dụng các hook sau:

//add_filter( 'manage_posts_columns', array( $this, 'manage_posts_columns_filter' ), 99, 2 );
//add_action( 'manage_posts_custom_column', array( $this, 'manage_posts_custom_column_action' ), 99, 2 );
//add_filter( 'manage_edit-post_sortable_columns', 'hocwp_theme_custom_post_sortable_columns_filter' );

Nếu áp dụng thêm column cho tất cả các post type, bao gồm: post, page và custom post type thì áp dụng như code bên dưới:

<?php
if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly
}

class Content_To_Speech_Post_Columns {
    public function __construct() {
        $post_type = Content_To_Speech_Global()->get_option( 'post_type' );

        if ( is_array( $post_type ) ) {
            foreach ( $post_type as $pt ) {
                add_filter( 'manage_' . $pt . '_posts_columns', array( $this, 'manage_posts_columns_filter' ), 99, 2 );

                add_action( 'manage_' . $pt . '_posts_custom_column', array(
                    $this,
                    'manage_posts_custom_column_action'
                ), 99, 2 );

                add_filter( 'manage_edit-' . $pt . '_sortable_columns', array(
                    $this,
                    'manage_posts_sortable_columns_filter'
                ) );
            }
        }

        add_action( 'pre_get_posts', array( $this, 'pre_get_posts_action' ) );
    }

    public function pre_get_posts_action( $query ) {
        if ( is_admin() && $query instanceof WP_Query ) {
            global $pagenow;

            if ( 'edit.php' == $pagenow ) {
                $by    = $_GET['orderby'] ?? '';
                $order = $_GET['order'] ?? 'DESC';

                if ( 'audio_file' == $by ) {
                    $query->set( 'meta_key', CTS_CONST()::PM_AUDIO_FILE );
                    $query->set( 'orderby', 'meta_value' );
                    $query->set( 'order', $order );
                }
            }
        }
    }

    public function manage_posts_sortable_columns_filter( $columns ) {
        $columns['audio_file'] = 'audio_file';

        return $columns;
    }

    public function manage_posts_columns_filter( $posts_columns ) {
        $date = $posts_columns['date'] ?? '';
        unset( $posts_columns['date'] );
        $posts_columns['audio_file'] = __( 'Audio File', CTS_CONST()::TEXT_DOMAIN );

        if ( ! empty( $date ) ) {
            $posts_columns['date'] = $date;
        }

        return $posts_columns;
    }

    public function manage_posts_custom_column_action( $column_name, $post_id ) {
        if ( 'audio_file' == $column_name ) {
            $file = get_post_meta( $post_id, CTS_CONST()::PM_AUDIO_FILE, true );

            if ( empty( $file ) ) {
                ?>
                <button type="button"
                        class="button button-primary" data-ajax-button="1" data-do-action="generate_audio_file"
                        data-id="<?php echo esc_attr( $post_id ); ?>"><?php _e( 'Create Audio File', CTS_CONST()::TEXT_DOMAIN ); ?></button>
                <?php
            } else {
                Content_To_Speech_Global()->audio_html( $post_id );
                ?>
                <button type="button" class="button button-secondary play-audio"
                        data-target="Audio<?php echo esc_attr( $post_id ); ?>"><span
                            class="dashicons dashicons-controls-play"></span></button>
                <button type="button"
                        class="button button-primary" data-ajax-button="1" data-do-action="generate_audio_file"
                        data-id="<?php echo esc_attr( $post_id ); ?>"><?php _e( 'Update Audio File', CTS_CONST()::TEXT_DOMAIN ); ?></button>
                <?php
            }
        }
    }
}

new Content_To_Speech_Post_Columns();

 

0 0 đánh giá
Đánh giá bài viết
Theo dõi
Thông báo của
guest
0 Góp ý
Cũ nhất
Mới nhất Được bỏ phiếu nhiều nhất