back_image
blog-image

Core table in wordpress plugin

Author Image By Ashish Dwivedi

October 7, 2015

2 minutes

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.

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 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_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 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 ‘

‘;


Leave a Reply

Your email address will not be published. Required fields are marked *

© OutRight Systems 2024 - We are doing IT Right! This website is property of OutRight Systems and it has copyright.

Disclaimer: The website outrightsystems.org is owned and operated by OutRight Systems Pvt Ltd and not affiliated with SugarCRM, Inc. or SuiteCRM. SugarCRM® is a registered trademark of SugarCRM, Inc.