Skip to content

Instantly share code, notes, and snippets.

@getmanzooronline
Created February 2, 2015 09:44
Show Gist options
  • Save getmanzooronline/1fb1131498a926f73000 to your computer and use it in GitHub Desktop.
Save getmanzooronline/1fb1131498a926f73000 to your computer and use it in GitHub Desktop.
WordPress admin style table with sorting and pagination
<?php
if (!class_exists('WP_List_Table'))
{
require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );
}
class My_Example_List_Table extends WP_List_Table
{
function __construct()
{
global $status, $page;
parent::__construct(array(
'singular' => __('book', 'mylisttable'), //singular name of the listed records
'plural' => __('books', 'mylisttable'), //plural name of the listed records
'ajax' => false //does this table support ajax?
));
}
function column_default($item, $column_name)
{
return $item[$column_name];
}
function prepare_items($headers, $data, $sortable_columns = array(), $total_items = 100, $per_page = 5)
{
$columns = $headers;
$hidden = array();
$this->_column_headers = array($columns, $hidden, $sortable_columns);
$this->items = $data;
$this->set_pagination_args( array(
'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
'total_pages' => ceil($total_items/$per_page) //WE have to calculate the total number of pages
) );
}
}
/**
* In plugin class
*
*/
function listBinders()
{
$content = array(
array('ID' => 1, 'booktitle' => 'Quarter Share', 'author' => 'Nathan Lowell',
'isbn' => '978-0982514542'),
array('ID' => 2, 'booktitle' => '7th Son: Descent', 'author' => 'J. C. Hutchins',
'isbn' => '0312384378'),
array('ID' => 3, 'booktitle' => 'Shadowmagic', 'author' => 'John Lenahan',
'isbn' => '978-1905548927'),
array('ID' => 4, 'booktitle' => 'The Crown Conspiracy', 'author' => 'Michael J. Sullivan',
'isbn' => '978-0979621130'),
array('ID' => 5, 'booktitle' => 'Max Quick: The Pocket and the Pendant', 'author' => 'Mark Jeffrey',
'isbn' => '978-0061988929'),
array(' ID' => 6, 'booktitle' => 'Jack Wakes Up: A Novel', 'author' => 'Seth Harwood',
'isbn' => '978-0307454355')
);
$headers = array(
'booktitle' => __('Title', 'mylisttable'),
'author' => __('Author', 'mylisttable'),
'isbn' => __('ISBN', 'mylisttable'),
'actions' => __('Actions')
);
$sortable_columns = array(
'booktitle' => array('booktitle',false), //true means it's already sorted
'author' => array('author',false),
'isbn' => array('isbn',false)
);
$myListTable = new My_Example_List_Table();
echo '</pre><div class="wrap"><h2>My List Table Test</h2>';
$myListTable->prepare_items($headers, $content, $sortable_columns);
$myListTable->display();
echo '</div>';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment