WP_List_Table is a class in wordpress which is used by many plugins, Posts and pages to display table data in wordpress admin.Sometime we also need to display data from custom tables in wordpress table in the list form.Today i am going to explain how to use this class in wordpress.
Let’s get started with WP_List_Table class :-

Initiate Class

create a file in your theme folder for example mycustomtabel.php

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

}

/** * Create a new table class that will extend the WP_List_Table */
class CodingbinCustom_tabel extends WP_List_Table

{
	public function __construct()
	{
		parent::__construct(array(
			'singular' => 'singular_form',
			'plural' => 'plural_form',
			'ajax' => true
		));
	}
}

Get Your data from database

prepare_items() is function which is used by WP_List_Table to display data into table.So you can customize this function in your class.

/** * Prepare the items for the table to process 
* * @return Void 
*/
public function prepare_items()
{
        // $this->_column_headers = $this->get_column_info();
	$columns = $this->get_columns();
	$hidden = $this->get_hidden_columns();
	$sortable = $this->get_sortable_columns();
	$this->_column_headers = array(
		$columns,
		$hidden,
		$sortable
	);
	/** Process bulk action */
	$this->process_bulk_action();
	$per_page = $this->get_items_per_page('records_per_page', 10);
	$current_page = $this->get_pagenum();
	$total_items = self::record_count();
	$data = self::get_records($per_page, $current_page);
	$this->set_pagination_args(
                      ['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
	              ]);
	$this->items = $data;
}

get_records() function

This is the custom table you can use your logic to get the data from the database,Like display above

/** * 
Retrieve records data from the database
 * * @param int $per_page
 * @param int $page_number
 * * @return mixed
*/
public static function get_records($per_page = 10, $page_number = 1)
{
	global $wpdb;
	$sql = "select * from yourtablename";
	if (isset($_REQUEST['s'])) {
	$sql.= ' where column1 LIKE "%' . $_REQUEST['s'] . '%" or column2 LIKE "%' . $_REQUEST['s'] . '%"';
	}
	if (!empty($_REQUEST['orderby'])) {
         	$sql.= ' ORDER BY ' . esc_sql($_REQUEST['orderby']);
		$sql.= !empty($_REQUEST['order']) ? ' ' . esc_sql($_REQUEST['order']) : ' ASC';
	}
	$sql.= " LIMIT $per_page";
	$sql.= ' OFFSET ' . ($page_number - 1) * $per_page;
	$result = $wpdb->get_results($sql, 'ARRAY_A');
	return $result;
}

Get your table columns

you can overwrite function get_columns() to customize your table columns if you want to show custom columns

/** 
* Override the parent columns method. Defines the columns to use in your listing table 
* * @return Array 
*/
function get_columns()
{
	$columns = [
		'cb' => '<input type="checkbox" />', 
		'first_column_name' => __('First Column Name', 'ux') , 
		'second_column_name' => __('Second Column Name', 'ux') , 
		'third_column_name' => __('Third Column Name', 'ux') , 
		'fourth_column_name' => __('Fourth Column Name', 'ux') , 
		'fifth_column_name' => __('Fifth Column Name', 'ux') , 
		'sicth_column_name' => __('Sixth Column Name', 'ux') , 
		'created' => __('Date', 'ux')  
	];
	return $columns;
}

Setup the Hidden Columns

public function get_hidden_columns()
{
        // Setup Hidden columns and return them
        return array();
}

Setting Sortable Columns

/** 
* Columns to make sortable. 
* * @return array 
*/
public function get_sortable_columns()
{
	$sortable_columns = array(
		'first_column_name' => array('first_column_name',true) ,
		'second_column_name' => array('second_column_name',false) ,
		'fifth_column_name' => array('fifth_column_name',false) ,
		'created' => array('created',false) 
	);
	return $sortable_columns;
}

Setup Actions for particular Column

You can customize each column in your table in your way.You can use html, css classes in your column by this function.If you don’t define it will display by the parent class function.

/** 
* Render the bulk edit checkbox 
* * @param array $item 
* * @return string 
*/
function column_cb($item)
{
	return sprintf('<input type="checkbox" name="bulk-delete[]" value="%s" />', $item['id']);
}

/** 
* Render the bulk edit checkbox 
* * @param array $item 
* * @return string 
*/
function column_first_column_name($item)
{
	return sprintf('<a href="%s" class="btn btn-primary"/>Link Title</a>', $item['first_column_name']);
}

So with this you can customize your columns just use column as prefix with your column name.

Setup Bulk Actions and Process Bulk Action for Columns

/** 
* Returns an associative array containing the bulk action 
* * @return array */
public function get_bulk_actions()
{
	$actions = ['bulk-delete' => 'Delete'];
	return $actions;
}
public function process_bulk_action()
{
	// Detect when a bulk action is being triggered...
	if ('delete' === $this->current_action()) {
	// In our file that handles the request, verify the nonce.
         	$nonce = esc_attr($_REQUEST['_wpnonce']);
		if (!wp_verify_nonce($nonce, 'bx_delete_records')) {
			die('Go get a life script kiddies');
        	}
		else {
			self::delete_records(absint($_GET['record']));
			$redirect = admin_url('admin.php?page=codingbin_records');
			wp_redirect($redirect);
			exit;
		}
	}

	// If the delete bulk action is triggered
	if ((isset($_POST['action']){
		$_POST['action'] == 'bulk-delete') || (isset($_POST['action2']) & amp; & amp;
		$_POST['action2'] == 'bulk-delete')) {
		$delete_ids = esc_sql($_POST['bulk-delete']);
		// loop over the array of record IDs and delete them
		foreach($delete_ids as $id) {
			self::delete_records($id);
		}

		$redirect = admin_url('admin.php?page=codingbin_records');
		wp_redirect($redirect);
		exit;
		exit;
	}
}
/** 
* Delete a record record. 
* * @param int $id customer ID 
*/
public static function delete_records($id)
{
	global $wpdb;
	$wpdb->delete("custom_records", ['id' => $id], ['%d']);
}

No Record Text

/** 
*Text displayed when no record data is available 
*/
public function no_items()
{
	_e('No record found in the database.', 'bx');
}

Count Records in databse

/** 
* Returns the count of records in the database. 
* * @return null|string 
*/
public static function record_count()
{
	global $wpdb;
	$sql = "SELECT COUNT(*) FROM custom_records";
	return $wpdb->get_var($sql);
}

How to use this class

This is very easy and handy to use this class.You can use display() function to display the content of the table.
Here is an example

Create Menu In admin

if ( current_user_can('edit_files') ) {
	add_action( 'admin_menu','register_my_custom_menu_page');
}
function register_my_custom_menu_page(){
 
    global $new_menu_page;
    // creating admin menu 
	add_menu_page('Career', 'Career', 'edit_posts','career','custom_list_page', get_template_directory_uri().'/images/custom.png', 8 );    
}
function custom_list_page(){
	// include your custom 
	require_once 'path to class/mycustomtabel.php';
	$class=new CodingbinCustom_tabel;
	$class->display();
}
http://codingbin.com/wp-content/uploads/2017/05/1431357672image-1-WordPress-post-and-user-admin-pages.pnghttp://codingbin.com/wp-content/uploads/2017/05/1431357672image-1-WordPress-post-and-user-admin-pages-150x150.pngManoj DhimancmsWordPresswordpressWP_List_Table is a class in wordpress which is used by many plugins, Posts and pages to display table data in wordpress admin.Sometime we also need to display data from custom tables in wordpress table in the list form.Today i am going to explain how to use this class in...coding, php, wordpress, yii, cakephp, and  jquery

Are you want to get implementation help, or modify or extend the functionality of this script? Submit paid service request