We need to made few changes in the code so that it works properly. Here is the updated code.
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'WP_List_Table' ) ) {
require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
}
class Transactions_List_Table extends WP_List_Table {
public function __construct() {
parent::__construct( array(
'singular' => 'transaction',
'plural' => 'transactions',
'ajax' => false,
) );
}
// Here are preparing our items.
public function prepare_items() {
// Here we would normally retrieve data from the database.
$this->items = array(
array( 'id' => 1, 'name' => 'Transaction 1', 'amount' => '$100' ),
array( 'id' => 2, 'name' => 'Transaction 2', 'amount' => '$200' ),
);
$this->_column_headers = array( $this->get_columns(), array(), array() );
}
public function get_columns() {
return array(
'id' => __( 'ID', 'textdomain' ),
'name' => __( 'Transaction Name', 'textdomain' ),
'amount' => __( 'Amount', 'textdomain' ),
);
}
public function column_default( $item, $column_name ) {
switch ( $column_name ) {
case 'id':
case 'name':
case 'amount':
return $item[ $column_name ];
default:
return '';
}
}
public function display() {
echo '<div class="wrap">';
echo '<h2>' . __( 'Transactions', 'textdomain' ) . '</h2>';
parent::display();
echo '</div>';
}
}
function render_transactions_list_page() {
$transactions_table = new Transactions_List_Table();
$transactions_table->prepare_items();
ob_start();
$transactions_table->display();
return ob_get_clean();
}
// We have added this to a shortcode to display it on a regular page.
add_shortcode( 'transactions_table', 'render_transactions_list_page' );