Come creare opzioni di filtro personalizzate in wp_list_table?
Ho utilizzato la classe wp_list_table
per creare la mia tabella personalizzata nel backend. Funziona correttamente.
Ora, voglio aggiungere dei filtri come nell'immagine qui sotto.
Ecco il mio codice esistente per rendere la tabella admin con le mie informazioni personalizzate.
if( ! class_exists( 'WP_List_Table' ) ) {
require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );
}
class Kv_subscribers_list extends WP_List_Table {
function __construct(){
global $status, $page;
parent::__construct( array(
'singular' => 'notifica', // nome singolare
'plural' => 'notifiche', // nome plurale
'ajax' => false
) );
}
function column_default($item, $column_name){
switch($column_name){
case 'email':
case 'date':
case 'common':
case 'unit_id':
return $item[$column_name];
default:
return print_r($item,true); // Mostra l'intero array per scopi di debug
}
}
function column_email($item){
$actions = array(
'email' => sprintf('<a href="?page=%s&action=%s&email_id=%s">E-mail</a>',$_REQUEST['page'],'email',$item['id']),
'delete' => sprintf('<a href="?page=%s&action=%s&delete_id=%s">Elimina</a>',$_REQUEST['page'],'delete',$item['id']),
);
// Restituisce il contenuto del titolo
return sprintf('%1$s %2$s',
/*$1%s*/ $item['email'],
/*$2%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'],
/*$2%s*/ $item['id']
);
}
function get_columns(){
$columns = array(
'cb' => '<input type="checkbox" />', // Renderizza una checkbox invece del testo
'email'=>__('Data'),
'date'=>__('Data'),
'common'=>__('Avviso Comune'),
'unit_id'=>__('ID Univoco')
);
return $columns;
}
public function get_sortable_columns() {
$sortable_columns = array(
'email' => array('wp_user_id',false), // true significa che è già ordinato
'date' => array('date',false),
'common' => array('common',false)
);
return $sortable_columns;
}
public function get_bulk_actions() {
$actions = array(
'delete' => 'Elimina',
'email' => 'Email'
);
return $actions;
}
public function process_bulk_action() {
global $wpdb;
$notifications_tbl = $wpdb->prefix.'newsletter';
if( 'delete'===$this->current_action() ) {
foreach($_POST['notification'] as $single_val){
$wpdb->delete( $notifications_tbl, array( 'id' => (int)$single_val ) );
}
$redirect_url = get_admin_url( null, 'admin.php?page=subscribers' );
wp_safe_redirect($redirect_url);
wp_die('Elementi eliminati!');
}
if( 'email'===$this->current_action() ) {
$result_email_ar = implode("-",$_POST['notification']);
$redirect_url = get_admin_url( null, 'admin.php?page=kvcodes&ids='.$result_email_ar );
wp_safe_redirect($redirect_url);
wp_die(' ');
}
}
function prepare_items() {
global $wpdb; // Usato solo per query al database
$database_name = $wpdb->prefix.'newsletter' ;
$per_page = 10;
$query = "SELECT * FROM $database_name ORDER BY id DESC";
$columns = $this->get_columns();
$hidden = array();
$sortable = $this->get_sortable_columns();
$this->_column_headers = array($columns, $hidden, $sortable);
$this->process_bulk_action();
$data = $wpdb->get_results($query, ARRAY_A );
function usort_reorder($a,$b){
$orderby = (!empty($_REQUEST['orderby'])) ? $_REQUEST['orderby'] : 'title'; // Se non ordinato, default al titolo
$order = (!empty($_REQUEST['order'])) ? $_REQUEST['order'] : 'asc'; // Se nessun ordine, default a asc
$result = strcmp($a[$orderby], $b[$orderby]); // Determina l'ordine
return ($order==='asc') ? $result : -$result; // Invia la direzione finale dell'ordinamento a 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, // Dobbiamo calcolare il numero totale di elementi
'per_page' => $per_page, // Dobbiamo determinare quanti elementi mostrare per pagina
'total_pages' => ceil($total_items/$per_page) // Dobbiamo calcolare il numero totale di pagine
) );
}
}//class
e
echo '<form method="post">';
$mydownloads = new Kv_subscribers_list();
echo '</pre><div class="wrap"><h2>Iscritti<a href="'."http://".$_SERVER["SERVER_NAME"].$_SERVER['REQUEST_URI'].'&add_new=true" class="add-new-h2">Aggiungi Nuovo</a></h2>';
$mydownloads->prepare_items();
$mydownloads->display();
echo '</div></form>';
Ora, voglio creare i filtri come
Tutti || Pubblicati || Cestino
Ho bisogno di creare un filtro personalizzato come questo.

Codice
class Kv_subscribers_list extends WP_List_Table {
function __construct(){
global $status, $page;
parent::__construct( array(
'singular' => 'notification',
'plural' => 'notifications',
'ajax' => false
) );
}
protected function get_views() {
$status_links = array(
"all" => __("<a href='#'>All</a>",'my-plugin-slug'),
"published" => __("<a href='#'>Published</a>",'my-plugin-slug'),
"trashed" => __("<a href='#'>Trashed</a>",'my-plugin-slug')
);
return $status_links;
}
function column_default($item, $column_name){
switch($column_name){
case 'email':
case 'date':
case 'common':
case 'unit_id':
return $item[$column_name];
default:
return print_r($item,true);
}
}
function column_email($item){
$actions = array(
'email' => sprintf('<a href="?page=%s&action=%s&email_id=%s">E-mail</a>',$_REQUEST['page'],'email',$item['id']),
'delete' => sprintf('<a href="?page=%s&action=%s&delete_id=%s">Delete</a>',$_REQUEST['page'],'delete',$item['id']),
);
return sprintf('%1$s %2$s',
/*$1%s*/ $item['email'],
/*$2%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'],
/*$2%s*/ $item['id']
);
}
function get_columns(){
$columns = array(
'cb' => '<input type="checkbox" />',
'email'=>__('Email'),
'date'=>__('Date'),
'common'=>__('Common Alert'),
'unit_id'=>__('Unique ID')
);
return $columns;
}
public function get_sortable_columns() {
$sortable_columns = array(
'email' => array('wp_user_id',false),
'date' => array('date',false),
'common' => array('common',false)
);
return $sortable_columns;
}
public function get_bulk_actions() {
$actions = array(
'delete' => 'Delete',
'email' => 'Email'
);
return $actions;
}
public function process_bulk_action() {
global $wpdb;
$notifications_tbl = $wpdb->prefix.'newsletter';
if( 'delete'===$this->current_action() ) {
foreach($_POST['notification'] as $single_val){
$wpdb->delete( $notifications_tbl, array( 'id' => (int)$single_val ) );
}
$redirect_url = get_admin_url( null, 'admin.php?page=subscribers' );
wp_safe_redirect($redirect_url);
wp_die('Items deleted (or they would be if we had items to delete)!');
}
if( 'email'===$this->current_action() ) {
$result_email_ar = implode("-",$_POST['notification']);
$redirect_url = get_admin_url( null, 'admin.php?page=kvcodes&ids='.$result_email_ar );
wp_safe_redirect($redirect_url);
wp_die(' ');
}
}
function prepare_items() {
global $wpdb;
$database_name = $wpdb->prefix.'newsletter' ;
$per_page = 10;
$query = "SELECT * FROM $database_name ORDER BY id DESC";
$columns = $this->get_columns();
$hidden = array();
$sortable = $this->get_sortable_columns();
$this->_column_headers = array($columns, $hidden, $sortable);
$this->process_bulk_action();
$data = $wpdb->get_results($query, ARRAY_A );
function usort_reorder($a,$b){
$orderby = (!empty($_REQUEST['orderby'])) ? $_REQUEST['orderby'] : 'title';
$order = (!empty($_REQUEST['order'])) ? $_REQUEST['order'] : 'asc';
$result = strcmp($a[$orderby], $b[$orderby]);
return ($order==='asc') ? $result : -$result;
}
$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,
'per_page' => $per_page,
'total_pages' => ceil($total_items/$per_page)
) );
}
}//class
Pagina di Amministrazione & Esempio di Rendering
function o_add_menu_items(){
add_menu_page('Plugin List Table', 'Sub', 'activate_plugins', 'subscribers', 'o_render_list_page');
}
add_action('admin_menu', 'o_add_menu_items');
function o_render_list_page() {
$mydownloads = new Kv_subscribers_list();
$title = __("Subscribers","my_plugin_slug");
?>
<div class="wrap">
<h1>
<?php echo esc_html( $title );?>
<a href="<?php echo admin_url( 'admin.php?page=subscribers&add_new=true' ); ?>" class="page-title-action">
<?php echo esc_html_x('Add New', 'my-plugin-slug'); ?>
</a>
<?php
?>
</h1>
</div>
<?php $mydownloads->views(); ?>
<form method="post">
<?php
$mydownloads->prepare_items();
$mydownloads->display();
?>
</form>
<?php
}
Spiegazione
We need to override WP_List_Table
class method get_views to get status links on the top.By default it is an empty array.
views method of WP_List_Table
uses get_views to display list of those links that we return in the get_views as an associative array with separator |
.
We can also override views method to have more control , for example if we want to change separator.
Take a look at how other list tables are using that. For instance check WP_Posts_List_Table.
After you have overridden the method then place $mydownloads->views();
.be sure to escape all and internationalize strings.
Using Filter
We can use views_{$this->screen->id}
filter once we have added get_views method.
Assuming toplevel_page_subscribers
as screen id
add_filter('views_toplevel_page_subscribers','my_plugin_slug_status_links',10, 1);
function my_plugin_slug_status_links($views) {
$views['scheduled'] = "<a href='#'>Scheduled</a>";
return $views;
}

Bene, so che è un po' in ritardo, ma dato che questo è il primo risultato su Google quando si cerca "WP_List_Table filtering", devo dirti che c'è una funzione extra_tablenav
che può essere sovrascritta nella tua classe estesa:
class Kv_subscribers_list extends WP_List_Table {
function extra_tablenav( $which )
{
switch ( $which )
{
case 'top':
// Il tuo codice HTML da outputtare
break;
case 'bottom':
// Il tuo codice HTML da outputtare
break;
}
}
}
Se vuoi mostrare il contenuto in entrambe le sezioni, puoi semplicemente ignorare lo switch e inserire il tuo codice HTML direttamente all'inizio della funzione.

Puoi utilizzare
<?php
class Kv_subscribers_list extends WP_List_Table {
function extra_tablenav( $which )
{
switch ( $which )
{
case 'top':
// Il tuo codice HTML da visualizzare
global $wpdb, $wp_locale;
$extra_checks = "AND status != 'auto-draft'";
if ( ! isset( $_GET['status'] ) || 'trash' !== $_GET['status'] ) {
$extra_checks .= " AND status != 'trash'";
} elseif ( isset( $_GET['status'] ) ) {
$extra_checks = $wpdb->prepare( ' AND status = %s', $_GET['status'] );
}
$sql = "
SELECT DISTINCT YEAR( create_date ) AS year, MONTH( create_date ) AS month
FROM ".$wpdb->prefix."audio_record
WHERE 1 = 1
$extra_checks
ORDER BY create_date DESC";
$months = $wpdb->get_results(
$wpdb->prepare(
$sql
)
);
$month_count = count( $months );
if ( ! $month_count || ( 1 == $month_count && 0 == $months[0]->month ) ) {
return;
}
$m = isset( $_GET['m'] ) ? (int) $_GET['m'] : 0;
$wp_query = add_query_arg();
$wp_query = remove_query_arg('m');
$link = esc_url_raw($wp_query);
?>
<div class="alignleft actions">
<select name="m" id="filter-by-date">
<option<?php selected( $m, 0 ); ?> value="0" data-rc="<?php _e($link); ?>"><?php _e( 'Tutte le date' ); ?></option>
<?php
foreach ( $months as $arc_row ) {
if ( 0 == $arc_row->year ) {
continue;
}
$month = zeroise( $arc_row->month, 2 );
$year = $arc_row->year;
$wp_query = add_query_arg('m', $arc_row->year . $month);
$link = esc_url_raw($wp_query);
printf(
"<option %s value='%s' data-rc='%s'>%s</option>\n",
selected( $m, $year . $month, false ),
esc_attr( $arc_row->year . $month ),
esc_attr( $link),
/* traduttori: 1: Nome mese, 2: Anno a 4 cifre. */
sprintf( __( '%1$s %2$d' ), $wp_locale->get_month( $month ), $year )
);
}
?>
</select>
<a href="javascript:void(0)" class="button" onclick="window.location.href = jQuery('#filter-by-date option:selected').data('rc');">Filtra</a>
</div>
<?php
break;
break;
case 'bottom':
// Il tuo codice HTML da visualizzare
break;
}
}
}
e per la query di azione, puoi aggiungere il codice in
public function prepare_items()
o nella funzione get_customers
$this->items = self::get_customers($per_page, $current_page);
<?php
//public static function get_customers($per_page = 20, $page_number = 1)
if (!empty($_REQUEST['m'])) {
$search = $_REQUEST['m'];
$year = substr($search,0,4);
$month = substr($search,4,5);
if(!empty($year)){
$sql .= ' And YEAR(create_date)="' . $year . '"';
}
if(!empty($month)){
$sql .= ' And MONTH(create_date)="' . $month . '"';
}
}
Questo è un esempio per ottenere dal mio database
Quando clicchi il pulsante Filtra verrà reindirizzato all'URL da data-rc
