Hướng dẫn sử dụng class WP_List_Table để tùy biến table comment admin dự án (oec.try0.xyz) (ok)

C:\xampp\htdocs\reset\wp-content\themes\twentytwentyone\functions.php

require get_template_directory() . '/abc.php';

C:\xampp\htdocs\reset\wp-content\themes\twentytwentyone\abc.php

<?php
if (!class_exists('WP_List_Table')) {
  require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
}
class TT_Example_List_Table_CT extends WP_List_Table {
	protected $example_data = array(
		array(
			'ID'       => 1,
			'title'    => '300',
			'rating'   => 'R',
			'director' => 'Zach Snyder',
		),
		array(
			'ID'       => 2,
			'title'    => 'Eyes Wide Shut',
			'rating'   => 'R',
			'director' => 'Stanley Kubrick',
		),
		array(
			'ID'       => 3,
			'title'    => 'Moulin Rouge!',
			'rating'   => 'PG-13',
			'director' => 'Baz Luhrman',
		),
		array(
			'ID'       => 4,
			'title'    => 'Snow White',
			'rating'   => 'G',
			'director' => 'Walt Disney',
		),
		array(
			'ID'       => 5,
			'title'    => 'Super 8',
			'rating'   => 'PG-13',
			'director' => 'JJ Abrams',
		),
		array(
			'ID'       => 6,
			'title'    => 'The Fountain',
			'rating'   => 'PG-13',
			'director' => 'Darren Aronofsky',
		),
		array(
			'ID'       => 7,
			'title'    => 'Watchmen',
			'rating'   => 'R',
			'director' => 'Zach Snyder',
		),
		array(
			'ID'       => 8,
			'title'    => '2001',
			'rating'   => 'G',
			'director' => 'Stanley Kubrick',
		),
	);
	public function __construct() {
		parent::__construct( array(
			'singular' => 'movie',     // Singular name of the listed records.
			'plural'   => 'movies',    // Plural name of the listed records.
			'ajax'     => false,       // Does this table support ajax?
		) );
	}
	public function get_columns() {
		$columns = array(
			'cb'       => '<input type="checkbox" />', // Render a checkbox instead of text.
			'title'    => _x( 'Title', 'Column label', 'wp-list-table-example' ),
			'rating'   => _x( 'Rating', 'Column label', 'wp-list-table-example' ),
			'director' => _x( 'Director', 'Column label', 'wp-list-table-example' ),
		);
		return $columns;
	}
	protected function get_sortable_columns() {
		$sortable_columns = array(
			'title'    => array( 'title', false ),
			'rating'   => array( 'rating', false ),
			'director' => array( 'director', false ),
		);
		return $sortable_columns;
	}
	protected function column_default( $item, $column_name ) {
		switch ( $column_name ) {
			case 'rating':
			case 'director':
				return $item[ $column_name ];
			default:
				return print_r( $item, true );
		}
	}
	protected function column_cb( $item ) {
		return sprintf('<input type="checkbox" name="%1$s[]" value="%2$s" />',$this->_args['singular'],$item['ID']);
	}
	protected function column_title( $item ) {
		$page = wp_unslash( $_REQUEST['page'] ); // WPCS: Input var ok.
		// Build edit row action.
		$edit_query_args = array(
			'page'   => $page,
			'action' => 'edit',
			'movie'  => $item['ID'],
		);
		$actions['edit'] = sprintf(
			'<a href="%1$s">%2$s</a>',
			esc_url( wp_nonce_url( add_query_arg( $edit_query_args, 'admin.php' ), 'editmovie_' . $item['ID'] ) ),
			_x( 'Edit', 'List table row action', 'wp-list-table-example' )
		);
		// Build delete row action.
		$delete_query_args = array(
			'page'   => $page,
			'action' => 'delete',
			'movie'  => $item['ID'],
		);
		$actions['delete'] = sprintf(
			'<a href="%1$s">%2$s</a>',esc_url( wp_nonce_url( add_query_arg( $delete_query_args, 'admin.php' ), 'deletemovie_' . $item['ID'] ) ),_x( 'Delete', 'List table row action', 'wp-list-table-example' )
		);
		// Return the title contents.
		return sprintf( '%1$s <span style="color:silver;">(id:%2$s)</span>%3$s',
			$item['title'],
			$item['ID'],
			$this->row_actions( $actions )
		);
	}
	protected function get_bulk_actions() {
		$actions = array(
			'delete' => _x( 'Delete', 'List table bulk action', 'wp-list-table-example' ),
		);
		return $actions;
	}
	protected function process_bulk_action() {
		// Detect when a bulk action is being triggered.
		if ( 'delete' === $this->current_action() ) {
			wp_die( 'Items deleted (or they would be if we had items to delete)!' );
		}
	}
	function prepare_items() {
		global $wpdb; //This is used only if making any database queries
		$per_page = 5;
		$columns  = $this->get_columns();
		$hidden   = array();
		$sortable = $this->get_sortable_columns();
		$this->_column_headers = array( $columns, $hidden, $sortable );
		$this->process_bulk_action();
		$data = $this->example_data;
		usort( $data, array( $this, 'usort_reorder' ) );
		$current_page = $this->get_pagenum();
		$total_items = count( $data );
		$data = array_slice( $data, ( ( $current_page - 1 ) * $per_page ), $per_page );
		$this->items = $data;
		$this->set_pagination_args( array(
			'total_items' => $total_items,                     // WE have to calculate the total number of items.
			'per_page'    => $per_page,                        // WE have to determine how many items to show on a page.
			'total_pages' => ceil( $total_items / $per_page ), // WE have to calculate the total number of pages.
		) );
	}
	protected function usort_reorder( $a, $b ) {
		// If no sort, default to title.
		$orderby = ! empty( $_REQUEST['orderby'] ) ? wp_unslash( $_REQUEST['orderby'] ) : 'title'; // WPCS: Input var ok.
		// If no order, default to asc.
		$order = ! empty( $_REQUEST['order'] ) ? wp_unslash( $_REQUEST['order'] ) : 'asc'; // WPCS: Input var ok.
		// Determine sort order.
		$result = strcmp( $a[ $orderby ], $b[ $orderby ] );
		return ( 'asc' === $order ) ? $result : - $result;
	}
}
add_action( 'admin_menu', 'tt_add_menu_items_ct' );
function tt_add_menu_items_ct() {
	add_menu_page(__( 'Example Class List Table', 'wp-list-table-example-ct' ),__( 'List Table Example', 'wp-list-table-example-ct' ),'activate_plugins','tt_list_test_ct','tt_render_list_page_ct');
}
function tt_render_list_page_ct() {
	$test_list_table = new TT_Example_List_Table_CT();
	$test_list_table->prepare_items();
	require get_template_directory() . '/pagemenu.php';
}

C:\xampp\htdocs\reset\wp-content\themes\twentytwentyone\pagemenu.php

<div class="wrap">
	<h1><?php echo esc_html( get_admin_page_title() ); ?></h1>
	<div style="background:#ececec;border:1px solid #ccc;padding:0 10px;margin-top:5px;border-radius:5px;">
		<p>This page demonstrates the use of the <code>WP_List_Table</code> class in plugins.</p>
		<p>For a detailed explanation of using the <code>WP_List_Table</code> class in your own plugins, simply open <code>class-tt-example-list-table.php</code> in the PHP editor of your choice.</p>
		<p>Additional class details are available on the</p>
	</div>
	<form id="movies-filter" method="get">
		<input type="hidden" name="page" value="<?php echo $_REQUEST['page'] ?>" />
		<?php $test_list_table->display() ?>
	</form>
</div>

Last updated