Skip to content

Instantly share code, notes, and snippets.

@mzalewski
Created January 28, 2017 20:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mzalewski/02fb5c90ec3e28c54f50b431101d663f to your computer and use it in GitHub Desktop.
Save mzalewski/02fb5c90ec3e28c54f50b431101d663f to your computer and use it in GitHub Desktop.
Create a custom WP List Table
<?php
if( ! class_exists( 'WP_List_Table' ) ) {
require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );
}
class MyPlugin_List_Table extends WP_List_Table
{
public function prepare_items()
{
$columns = $this->get_columns();
$hidden = $this->get_hidden_columns();
$sortable = $this->get_sortable_columns();
$data = array(); // Add your data here
$perPage = 20;
$currentPage = $this->get_pagenum();
$totalItems = count($data);
$this->set_pagination_args( array(
'total_items' => $totalItems,
'per_page' => $perPage
) );
$data = array_slice($data,(($currentPage-1)*$perPage),$perPage);
$this->_column_headers = array($columns, $hidden, $sortable);
$this->items = $data;
}
public function get_columns()
{
$columns = array(
'id' => 'ID',
'title' => 'Title',
'status' => 'Status',
'dateadded' => 'Date Added'
);
return $columns;
}
function column_title($item) {
$actions = array(
'edit' => sprintf('<a href="?page=%s&action=%s&id=%s">Edit</a>',$_REQUEST['page'],'edit',$item->id),
);
return sprintf('%1$s %2$s', $item['title'], $this->row_actions($actions) );
}
/**
* Define what data to show on each column of the table
*
* @param Array $item Data
* @param String $column_name - Current column name
*
* @return Mixed
*/
public function column_default( $item, $column_name )
{
switch( $column_name ) {
case 'id':
case 'title':
case 'status':
case 'dateadded':
case 'rating':
return $item[$column_name];
break;
default:
return print_r( $item, true ) ;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment