+91 9717203377

Home » Wordpress » Use core table in custom plugin

Use core table in custom plugin

Many times when we develop custom plugin we need a listing table for records.  Wordpress has inbuilt  table template class for listing and it can be inherit in our plugin to use core style of table and pagination.
Wordpress use class WP_List_Table to display data of posts, comments etc and this class have all important methods like sorting, paging, searching.
Here we are going to understand that how can we use this useful thing in our custom plugin. Although you can download script and create database table to check it work but I will recommend to read process of this.
Add Condition
Before start creating listing table we should add condition of class include.

 if( ! class_exists( 'WP_List_Table' ) ) {
 require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );
}

Create class and extend core class of WP

class My_Store_Table extends WP_List_Table {
}

Prepare Columns and get rows
Please make sure you have modified db table name. In my case table name is listing_example.

function get_columns(){
  $columns = array(
    'store' => 'Store',
    'address'    => 'Address',
    'mobile'      => 'Mobile'
  );
  return $columns;
}

function prepare_items() {
  $columns = $this->get_columns();
  $hidden = array();
  $sortable = array();
  $response = $this->get_store_list();
  $this->_column_headers = array($columns, $hidden, $sortable);
  $this->items = $response['data'];
}

function get_store_list() {
 global $wpdb;
 $total_items = 0;
 if ( isset( $_POST['s'] ) && ( !empty( $_POST['s'] ) ) ) {
 $search = trim( $_POST['s'] );
 $result = $wpdb->get_results( 
 $wpdb->prepare( "SELECT * FROM listing_example
 WHERE fieldName LIKE %s"
 ), ARRAY_A 
 );
 } else {
 $orderby = !empty ( $_GET["orderby"] ) ? mysql_real_escape_string ( $_GET["orderby"] ) : 'fieldName';
 $order = !empty ( $_GET["order"] ) ? mysql_real_escape_string ( $_GET["order"] ) : 'ASC';
 $order_sql = $orderby.' '.$order; 

 $total_items = $wpdb->get_var( "SELECT COUNT(*) AS count FROM listing_example" );
 $paged = !empty ( $_GET["paged"] ) ? mysql_real_escape_string ( $_GET["paged"] ) : '';
 
 if ( empty( $paged ) || !is_numeric( $paged ) || $paged <= 0 ) { 
 $paged = 1; 
 }

 $totalpages = ceil( $total_items / $this->_per_page );
 
 if ( !empty( $paged ) && !empty( $this->_per_page ) ){
 $offset = ( $paged - 1 ) * $this->_per_page;
 $limit_sql = (int)$offset.',' . (int)$this->_per_page;
 }
 
 $result = $wpdb->get_results( "SELECT * FROM listing_example ORDER BY $order_sql LIMIT $limit_sql", ARRAY_A );
 }
 $response = array(
 "data" => stripslashes_deep( $result ),
 "count" => $total_items
 );
 
 return $response;
 }

Define Column
To define each column, WP_List_Table has a function column_default

function column_default( $item, $column_name ) {
 switch( $column_name ) { 
 case 'store':
 case 'address':
 case 'mobile':
 return $item[ $column_name ];
 default:
 return 'id';
}

That’s it. Now all code have been prepared and we can use it in our listing. Below is code to call function and display listing

$storeTable = new My_Store_Table();
  echo '<div class="wrap"><h2>Store Listing Table</h2>'; 
  $storeTable->prepare_items(); 
  $storeTable->display(); 
  echo '</div>';

Download Working Example