Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- add_action('admin_menu', 'add_example_menues');
- function add_example_menues() {
- add_menu_page('test', 'test', 'administrator', 'test-top', 'build_test_page');
- add_submenu_page('test-top', 'All tests', 'All tests', 'administrator', 'test-top');
- }
- if(!class_exists('WP_List_Table')){
- require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );
- }
- class test_List_Table extends WP_List_Table {
- function __construct() {
- parent::__construct( array(
- 'singular' => 'test',
- 'plural' => 'tests',
- 'ajax' => false
- ));
- }
- function extra_tablenav ($which) {
- if ($which == "top") {
- echo "Top";
- }
- if ($which == "bottom") {
- echo "Bottom";
- }
- }
- function get_columns() {
- $columns = array(
- 'id' => 'ID',
- 'title' => 'Title',
- 'user_id' => 'User ID',
- 'description' => 'Description'
- );
- return $columns;
- }
- function get_sortable_columns() {
- return $sortable = array(
- 'id' => array('id',false),
- 'title' => array('title',false),
- 'user_id' => array('user_id',false)
- );
- }
- function prepare_items() {
- global $wpdb;
- //data normally gotten from non-wp database. Wordpress db user has access, as per this SE post:
- //http://wordpress.stackexchange.com/questions/1604/using-wpdb-to-connect-to-a-separate-database
- $query = "SELECT `id`, `title`, `user_id`, `description` FROM otherdb.example_table";
- //pagination stuff
- $orderby = !empty($_GET["orderby"]) ? mysql_real_escape_string($_GET["orderby"]) : 'ASC';
- $order = !empty($_GET["order"]) ? mysql_real_escape_string($_GET["order"]) : '';
- if(!empty($orderby) & !empty($order)){ $query.=' ORDER BY '.$orderby.' '.$order; }
- $totalitems = $wpdb->query($query);
- echo "$totalitems";
- $per_page = 5;
- $paged = !empty($_GET["paged"]) ? mysql_real_escape_string($_GET["paged"]) : '';
- if(empty($paged) || !is_numeric($paged) || $paged<=0 ){ $paged=1; }
- $totalpages = ceil($totalitems/$perpage);
- if(!empty($paged) && !empty($perpage)){
- $offset=($paged-1)*$perpage;
- $query.=' LIMIT '.(int)$offset.','.(int)$perpage;
- }
- $this->set_pagination_args( array(
- "total_items" => 4,
- "total_pages" => 1,
- "per_page" => 5,
- ) );
- $columns = $this->get_columns();
- $hidden = array();
- $sortable = $this->get_sortable_columns();
- $this->_column_headers = array($columns, $hidden, $sortable);
- //actual data gotten from database, but for SE use hardcoded array
- //$data = $wpdb->get_results($query, ARRAY_N);
- $example_data = array(
- array(
- 'id' => 1,
- 'title' => 'nonsense',
- 'user_id' => 1,
- 'description' => 'asdf'
- ),
- array(
- 'id' => 2,
- 'title' => 'notanumber',
- 'user_id' => 2,
- 'description' => '404'
- ),
- array(
- 'id' => 3,
- 'title' => 'I Am A Title',
- 'user_id' => 3,
- 'description' => 'desc'
- ),
- array(
- 'id' => 4,
- 'title' => 'Example',
- 'user_id' => 4,
- 'description' => 'useless'
- ),
- array(
- 'id' => 5,
- 'title' => 'aeou',
- 'user_id' => 5,
- 'description' => 'keyboard layouts'
- ),
- array(
- 'id' => 6,
- 'title' => 'example data',
- 'user_id' => 6,
- 'description' => 'data example'
- ),
- array(
- 'id' => 7,
- 'title' => 'last one',
- 'user_id' => 7,
- 'description' => 'done'
- )
- );
- //This is the line:
- $this->items = $example_data;
- //When the above line is commented, it works as expected (except for the lack of data, of course)
- }
- }
- function build_test_page() {
- $testListTable = new test_List_Table();
- $testListTable->prepare_items();
- ?>
- <div class="wrap">
- <div id="icon-users" class="icon32"><br/></div>
- <h2>List Table Test</h2>
- <?php $testListTable->display() ?>
- </div>
- <?php
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement