-
-
Save Latz/7f923479a4ed135e35b2 to your computer and use it in GitHub Desktop.
<?php | |
/* | |
Plugin Name: Test List Table Example | |
*/ | |
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 { | |
var $example_data = 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' ) | |
); | |
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? | |
) ); | |
add_action( 'admin_head', array( &$this, 'admin_header' ) ); | |
} | |
function admin_header() { | |
$page = ( isset($_GET['page'] ) ) ? esc_attr( $_GET['page'] ) : false; | |
if( 'my_list_test' != $page ) | |
return; | |
echo '<style type="text/css">'; | |
echo '.wp-list-table .column-id { width: 5%; }'; | |
echo '.wp-list-table .column-booktitle { width: 40%; }'; | |
echo '.wp-list-table .column-author { width: 35%; }'; | |
echo '.wp-list-table .column-isbn { width: 20%;}'; | |
echo '</style>'; | |
} | |
function no_items() { | |
_e( 'No books found, dude.' ); | |
} | |
function column_default( $item, $column_name ) { | |
switch( $column_name ) { | |
case 'booktitle': | |
case 'author': | |
case 'isbn': | |
return $item[ $column_name ]; | |
default: | |
return print_r( $item, true ) ; //Show the whole array for troubleshooting purposes | |
} | |
} | |
function get_sortable_columns() { | |
$sortable_columns = array( | |
'booktitle' => array('booktitle',false), | |
'author' => array('author',false), | |
'isbn' => array('isbn',false) | |
); | |
return $sortable_columns; | |
} | |
function get_columns(){ | |
$columns = array( | |
'cb' => '<input type="checkbox" />', | |
'booktitle' => __( 'Title', 'mylisttable' ), | |
'author' => __( 'Author', 'mylisttable' ), | |
'isbn' => __( 'ISBN', 'mylisttable' ) | |
); | |
return $columns; | |
} | |
function usort_reorder( $a, $b ) { | |
// If no sort, default to title | |
$orderby = ( ! empty( $_GET['orderby'] ) ) ? $_GET['orderby'] : 'booktitle'; | |
// If no order, default to asc | |
$order = ( ! empty($_GET['order'] ) ) ? $_GET['order'] : 'asc'; | |
// Determine sort order | |
$result = strcmp( $a[$orderby], $b[$orderby] ); | |
// Send final sort direction to usort | |
return ( $order === 'asc' ) ? $result : -$result; | |
} | |
function column_booktitle($item){ | |
$actions = array( | |
'edit' => sprintf('<a href="?page=%s&action=%s&book=%s">Edit</a>',$_REQUEST['page'],'edit',$item['ID']), | |
'delete' => sprintf('<a href="?page=%s&action=%s&book=%s">Delete</a>',$_REQUEST['page'],'delete',$item['ID']), | |
); | |
return sprintf('%1$s %2$s', $item['booktitle'], $this->row_actions($actions) ); | |
} | |
function get_bulk_actions() { | |
$actions = array( | |
'delete' => 'Delete' | |
); | |
return $actions; | |
} | |
function column_cb($item) { | |
return sprintf( | |
'<input type="checkbox" name="book[]" value="%s" />', $item['ID'] | |
); | |
} | |
function prepare_items() { | |
$columns = $this->get_columns(); | |
$hidden = array(); | |
$sortable = $this->get_sortable_columns(); | |
$this->_column_headers = array( $columns, $hidden, $sortable ); | |
usort( $this->example_data, array( &$this, 'usort_reorder' ) ); | |
$per_page = 5; | |
$current_page = $this->get_pagenum(); | |
$total_items = count( $this->example_data ); | |
// only ncessary because we have sample data | |
$this->found_data = array_slice( $this->example_data,( ( $current_page-1 )* $per_page ), $per_page ); | |
$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 | |
) ); | |
$this->items = $this->found_data; | |
} | |
} //class | |
function my_add_menu_items(){ | |
$hook = add_menu_page( 'My Plugin List Table', 'My List Table Example', 'activate_plugins', 'my_list_test', 'my_render_list_page' ); | |
add_action( "load-$hook", 'add_options' ); | |
} | |
function add_options() { | |
global $myListTable; | |
$option = 'per_page'; | |
$args = array( | |
'label' => 'Books', | |
'default' => 10, | |
'option' => 'books_per_page' | |
); | |
add_screen_option( $option, $args ); | |
$myListTable = new My_Example_List_Table(); | |
} | |
add_action( 'admin_menu', 'my_add_menu_items' ); | |
function my_render_list_page(){ | |
global $myListTable; | |
echo '</pre><div class="wrap"><h2>My List Table Test</h2>'; | |
$myListTable->prepare_items(); | |
?> | |
<form method="post"> | |
<input type="hidden" name="page" value="ttest_list_table"> | |
<?php | |
$myListTable->search_box( 'search', 'search_id' ); | |
$myListTable->display(); | |
echo '</form></div>'; | |
} | |
How do I show this table in the Dashboard page? Or does dashboard only show widgets?
You need to create a widget to use it in the Dashboard but you can use the WP_list_Table inside that widget.
Here's if you want to use SQL version:
https://plugins.trac.wordpress.org/browser/wp-basic-crud/trunk/wp-basic-crud.php
@Latz
When defining columns, you explain it so nicely:
Before actually displaying each column WordPress looks for methods called column_{key_name}
, (e.g. function column_booktitle
). There has to be such a method for every defined column. To avoid the need to create a method for each column there is column_default that will process any column for which no special method is defined.
My question is... Is there a way to dynamically assign columns and "it's content" after referencing the data which will not include the new columns that I want to be created?
Similar to the way the default WP Post list table does when I define custom columns within my function.php?
I've attempted to look into their code at https://core.trac.wordpress.org/browser/tags/5.2.2/src//wp-admin/includes/class-wp-list-table.php . to see how it is being done and looking for the column creation code. Let me know if you can help.
The
books_per_page
screen option doesn't work correctly here, because, as with most of this example code, it has been copied and pasted all over the web and not actually tested. WordPress does not save this setting by default; if you try to set the items per page in the screen options it will simply ignore what you type and use the default. To get this to work you must add a filter toset-screen-option
. Here is an example of how to do that.
Your code does not works too. I got work solution, when I moved adding filter set-screen-option to "init" hook. It describes in first comment in documentation page https://developer.wordpress.org/reference/hooks/set-screen-option/
//$hidden = array();
$hidden = get_hidden_columns( $this->screen );
This will help to implement the col show/hide feature in screen option properly
@manchumahara: I haven't looked at this piece of code for ages. Thanks for your comment. I'll gonna take a close at it next week.
@SeanDS:
Yes I have copied some (a lot) of the code, but not from the web (there have been no examples a the time, therefore I wrote the tutorial) but from the core code. and simplified it. Maybe I did to test some parts.
Thanks for the clarification.
Of course you need do this and it's good practise. In my mind tutorial should use as few characters as possible to focus on the main problem.
Thanks for your comments, users of this code will appreciate it.