How to fire on bulk actions inside of an extended WP_List_Table.
I have been adding the following bulk actions to may table's select box but on Apply won't really happens anything
here are how I added my bulk actions
function get_bulk_actions() {
$actions = array(
'delete' => 'Delete',
'parsing' => 'Parsen'
);
return $actions;
}
and here is the check box column
function column_cb($item) {
return sprintf(
'<input type="checkbox" name="record[]" value="%d" />', $item['Round']
);
}
How to fire on bulk actions inside of an extended WP_List_Table.
I have been adding the following bulk actions to may table's select box but on Apply won't really happens anything
here are how I added my bulk actions
function get_bulk_actions() {
$actions = array(
'delete' => 'Delete',
'parsing' => 'Parsen'
);
return $actions;
}
and here is the check box column
function column_cb($item) {
return sprintf(
'<input type="checkbox" name="record[]" value="%d" />', $item['Round']
);
}
Share
Improve this question
asked Dec 17, 2012 at 13:38
fefefefe
8943 gold badges14 silver badges34 bronze badges
3 Answers
Reset to default 11If you add a bulk-action, then you have to react on this action. Simply adding a function doesn't do anything, you have to call it:
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;
}
}
In prepare_items()
we call process_bulk_action()
. So on your backend page you will have something like this:
$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>';
At first you create an instance of your list-table class. Then you create a formular and call prepare_items()
. With this call, the bulk actions will be processed because we call the method process_bulk_action()
inside prepare_items()
.
In the example above, we use post
as method to send the data. So we can grab the bulk action from the post array if we didn't want to process the bulk actions inside the class (or by other reasons).
// 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 ) {}
You can grab the bulk action anywhere you want from the post/get array (depending on what method was used to send the data).
WP List w/ bulk actions using an "Array" DATA
https://wpengineer/2426/wp_list_table-a-step-by-step-guide/ (Make it as a plugin) https://gist.github/Latz/7f923479a4ed135e35b2 (for functions.php)
WP List w/ bulk actions using an "SQL" DATA
https://www.smashingmagazine/2011/11/native-admin-tables-wordpress/ (for functions.php)
Cheers!
this is what I have found on several websites. It still isn't working for me, but it used to work when I still had the sample data in my custom list table.
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)!');
}
}