2

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']
        );    

}
6

If 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).

  • Thank you Ralf912 I'm using your approach in my WPSeed plugin and added a link to your profile in the code. I've done this before but I want to ensure I'm going with the latest approach. I'm curious as to why you've not written it to require a nonce and instead the extra security is bypassed? I'm going to force a nonce but wanted your opinion. – Ryan Bayne Apr 11 '17 at 13:30
  • This answer to the same question also allows the nonce not to exist. wordpress.stackexchange.com/questions/194450/… – Ryan Bayne Apr 11 '17 at 13:32
0

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)!');
    }

}
0

WP List w/ bulk actions using an "Array" DATA

https://wpengineer.com/2426/wp_list_table-a-step-by-step-guide/ (Make it as a plugin) https://gist.github.com/Latz/7f923479a4ed135e35b2 (for functions.php)

WP List w/ bulk actions using an "SQL" DATA

https://www.smashingmagazine.com/2011/11/native-admin-tables-wordpress/ (for functions.php)

Cheers!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.