tabela list wp – WP_list_table przygotowanie_items()
Tworzę niestandardową tabelę WP_list_table z formularzem bocznym, takim jak (dodawanie nowego formularza kategorii), ale kiedy przesyłam ten formularz, wp_list_table nie odświeża się. Kiedy ponownie odświeżę stronę, pojawia się informacja o stronie. Używam wtyczki wp_list_table_example, wszystko jest takie samo, z wyjątkiem pobierania przykładowych danych i formularza.
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="https://wordpress.stackexchange.com/questions/220788/?page=%s&action=%s&movie=%s">Edit</a>',$_REQUEST['page'],'edit',$item['ID']),
'delete' => sprintf('<a href="https://wordpress.stackexchange.com/questions/220788/?page=%s&action=%s&movie=%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>
<h2>List 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" />
<p>Description</p>
</div>
<label>Description</label><input 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
}
Moje pytanie brzmi: kiedy dodam w formularzu nowy termin, dlaczego nie odświeżyć wp_list_table, czy to ze względu na sposób uzyskiwania danych lub sposób dodawania danych. Jak odświeżyć wp_table_list po przesłaniu nowego terminu. Każda pomoc jest mile widziana.