wp_list_tables bulk actions

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

Read More

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

}

Related posts

Leave a Reply

3 comments

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

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