WP_list_table prepare_items()
I create custom WP_list_table with side form like (adding new category form), but when i submit that form wp_list_table doesn't refresh. When I once again refresh page information appears.I use wp_list_table_example plugin everything is the same except for geting example_data and form.
class TT_Example_List_Table extends WP_List_Table {
function __construct(){
global $status, $page;
//Set parent defaults
parent::__construct( array(
'singular' = 'movie', //singular name of the listed records
'plural' = 'movies', //plural name of the listed records
'ajax' = false //does this table support ajax?
) );
}
function column_default($item, $column_name){
switch($column_name){
case 'rating':
case 'director':
return $item[$column_name];
default:
return print_r($item,true); //Show the whole array for troubleshooting purposes
}
}
function column_title($item){
//Build row actions
$actions = array(
'edit' = sprintf('a href="?page=%saction=%smovie=%s"Edit/a',$_REQUEST['page'],'edit',$item['ID']),
'delete' = sprintf('a href="?page=%saction=%smovie=%s"Delete/a',$_REQUEST['page'],'delete',$item['ID']),
);
//Return the title contents
return sprintf('%1$s span style="color:silver"(id:%2$s)/span%3$s',
/*$1%s*/ $item['title'],
/*$2%s*/ $item['ID'],
/*$3%s*/ $this-row_actions($actions)
);
}
function column_cb($item){
return sprintf(
'input type="checkbox" name="%1$s[]" value="%2$s" /',
/*$1%s*/ $this-_args['singular'], //Let's simply repurpose the table's singular label ("movie")
/*$2%s*/ $item['ID'] //The value of the checkbox should be the record's id
);
}
function get_columns(){
$columns = array(
'cb' = 'input type="checkbox" /', //Render a checkbox instead of text
'title' = 'Title',
'rating' = 'Rating',
);
return $columns;
}
function get_sortable_columns() {
$sortable_columns = array(
'title' = array('title',false), //true means it's already sorted
'rating' = array('rating',false),
'director' = array('director',false)
);
return $sortable_columns;
}
function get_bulk_actions() {
$actions = array(
'delete' = 'Delete'
);
return $actions;
}
function process_bulk_action() {
//Detect when a bulk action is being triggered...
if( 'delete'===$this-current_action() ) {
wp_die('Items deleted (or they would be if we had items to delete)!');
}
}
function prepare_items() {
$data = array();
$example_data = array();
$terms = get_terms( 'productcat', array('hide_empty' = false));
print_r($terms);
if ( ! empty( $terms ) ){
foreach ( $terms as $term ) {
$data[] = array(
'ID' = $term-term_id,
'title' = $term-name
);
}
}
$example_data = $data;
$per_page = 5;
$columns = $this-get_columns();
$hidden = array();
$sortable = $this-get_sortable_columns();
$this-_column_headers = array($columns, $hidden, $sortable);
$this-process_bulk_action();
$data = $example_data;
function usort_reorder($a,$b){
$orderby = (!empty($_REQUEST['orderby'])) ? $_REQUEST['orderby'] : 'title'; //If no sort, default to title
$order = (!empty($_REQUEST['order'])) ? $_REQUEST['order'] : 'asc'; //If no order, default to asc
$result = strcmp($a[$orderby], $b[$orderby]); //Determine sort order
return ($order==='asc') ? $result : -$result; //Send final sort direction to usort
}
usort($data, 'usort_reorder');
$current_page = $this-get_pagenum();
$total_items = count($data);
$data = array_slice($data,(($current_page-1)*$per_page),$per_page);
$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
) );
}
}
function tt_add_menu_items(){
add_menu_page('Example Plugin List Table', 'List Table Example', 'activate_plugins', 'tt_list_test', 'tt_render_list_page');
} add_action('admin_menu', 'tt_add_menu_items');
function tt_render_list_page(){
//Create an instance of our package class...
$testListTable = new TT_Example_List_Table();
//Fetch, prepare, sort, and filter our data...
$testListTable-prepare_items();
?
div class="wrap"
div id="icon-users" class="icon32"br//div
h2List Table Test/h2
form id="movies-filter" method="GET"
!-- For plugins, we also need to ensure that the form posts back to our current page --
input type="hidden" name="page" value="?php echo $_REQUEST['page'] ?" /
!-- Now we can render the completed list table --
?php $testListTable-display(); ?
/form
form id="insert_term" name="insert_term" method="post" action=""
div class="form-field term-name-wrap"
label for="term"Term/label
input type="text" value="" name="term" id="term" size="40" /
pDescription/p
/div
labelDescription/labelinput type="text" value="" name="termdesc" id="termdesc" /
input type="submit" value="Add term" id="submit" name="submit" /
input type="hidden" name="action" value="new_term" /
/form
?php
if( 'POST' == $_SERVER['REQUEST_METHOD']) $_POST['action'] == "new_term") {
if (isset($_POST['term']) !empty( $_POST['term']) ) {
$new_term = $_POST['term'];
$description = $_POST['desc'];
wp_insert_term(
$new_term,
'productcat',
array(
'description'= $description,
));
}
}
?
/div
?php
}
My question is, when I add in the form new term why not refresh wp_list_table, is it because of the way of getting data or the way data is added. How refresh wp_table_list after i submit new term. Any help is welcome.
Topic wp-list-table Wordpress
Category Web