Introduction
A core table in a WordPress plugin is a custom database table that stores basic data particular to the plugin's functionality. These tables are made during plugin activation and are separate from WordPress's default tables.Â
Many times when we develop custom plugin we need a listing table for records. Wordpress has an inbuilt table template class for listing and it can be inherited in our WordPress plugin to use the 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.
Common uses for core tables
Storing custom post relationships
Tracking user activities
Maintaining plugin-specific settings
Logging data
Managing custom forms or submission data
Best practices
Always use $wpdb->prefix
for table names
Create tables during plugin activation
Include proper indexes for performance
Follow WordPress database character set and collation
Let's get started
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 WPclass My_Store_Table extends WP_List_Table {
}
Prepare Columns and get rows
Please make sure you have modified db table name. In this example 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_defaultfunction 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 has 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 '
Store Listing Table
';
$storeTable->prepare_items();
$storeTable->display();
echo '
';