如果添加批量操作,则必须对此操作作出反应。简单地添加函数没有任何作用,您必须调用它:
class WPSE_List_Table extends WP_List_Table
{
public function __construct() {
parent::__construct(
array(
\'singular\' => \'singular_form\',
\'plural\' => \'plural_form\',
\'ajax\' => false
)
);
}
public function prepare_items() {
$columns = $this->get_columns();
$sortable = $this->get_sortable_columns();
$hidden = array( \'id\' );
$this->_column_headers = array( $columns, $hidden, $sortable );
$this->process_bulk_action();
}
public function get_columns() {
return array(
\'cb\' => \'<input type="checkbox" />\', // this is all you need for the bulk-action checkbox
\'id\' => \'ID\',
\'date\' => __( \'Date\', \'your-textdomain\' ),
\'title\' => __( \'Title\', \'your-textdomain\' ),
);
}
public function get_sortable_columns() {
return array(
\'date\' => array( \'date\', false ),
\'title\' => array( \'title\', false ),
);
}
public function get_bulk_actions() {
return array(
\'delete\' => __( \'Delete\', \'your-textdomain\' ),
\'save\' => __( \'Save\', \'your-textdomain\' ),
);
}
public function process_bulk_action() {
// security check!
if ( isset( $_POST[\'_wpnonce\'] ) && ! empty( $_POST[\'_wpnonce\'] ) ) {
$nonce = filter_input( INPUT_POST, \'_wpnonce\', FILTER_SANITIZE_STRING );
$action = \'bulk-\' . $this->_args[\'plural\'];
if ( ! wp_verify_nonce( $nonce, $action ) )
wp_die( \'Nope! Security check failed!\' );
}
$action = $this->current_action();
switch ( $action ) {
case \'delete\':
wp_die( \'Delete something\' );
break;
case \'save\':
wp_die( \'Save something\' );
break;
default:
// do nothing or something else
return;
break;
}
return;
}
}
在中
prepare_items()
我们打电话
process_bulk_action()
. 因此,在您的后端页面上,您将看到如下内容:
$table = new WPSE_List_Table();
printf( \'<div class="wrap" id="wpse-list-table"><h2>%s</h2>\', __( \'Your List Table\', \'your-textdomain\' ) );
echo \'<form id="wpse-list-table-form" method="post">\';
$page = filter_input( INPUT_GET, \'page\', FILTER_SANITIZE_STRIPPED );
$paged = filter_input( INPUT_GET, \'paged\', FILTER_SANITIZE_NUMBER_INT );
printf( \'<input type="hidden" name="page" value="%s" />\', $page );
printf( \'<input type="hidden" name="paged" value="%d" />\', $paged );
$table->prepare_items(); // this will prepare the items AND process the bulk actions
$table->display();
echo \'</form>\';
echo \'</div>\';
首先,创建list table类的实例。然后创建一个公式并调用
prepare_items()
. 通过此调用,将处理批量操作,因为我们调用了方法
process_bulk_action()
在…内
prepare_items()
.
在上面的示例中,我们使用post
作为发送数据的方法。因此,如果我们不想在类内处理批量操作(或出于其他原因),我们可以从post数组中获取批量操作。
// this is the top bulk action!!
$action = ( isset( $_POST[\'action\'] ) ) ?
filter_input( INPUT_POST, \'action\', FILTER_SANITIZE_STRIPPED ) : \'default_top_bulk_action\';
// this is the bottom bulk action!!
$action2 = ( isset( $_POST[\'action2\'] ) ) ?
filter_input( INPUT_POST, \'action2\', FILTER_SANITIZE_STRIPPED ) : \'default_bottom_bulk_action\';
switch ( $action ) {}
switch ( $action2 ) {}
您可以从post/get数组中的任意位置获取批量操作(取决于发送数据所使用的方法)。