Bulk action hook for admin pages which uses WP_List_Table

You have to use “process_bulk_action” function like this:

public function process_bulk_action() {

   //nonce validations,etc

    $action = $this->current_action();

    switch ( $action ) {

        case 'export_action':

            // Do whatever you want
            wp_redirect( esc_url( add_query_arg() ) );
            break;

        default:
             // do nothing or something else
            return;
            break;
        }

        return;

    }
}

If you need a complete example this is the code I’m using for a plugin:

   public function process_bulk_action() {

        if ( isset( $_POST['_wpnonce'] ) && ! empty( $_POST['_wpnonce'] ) ) {
            echo '<pre>';
            print_r($_POST);
            echo '</pre>';

            $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':
                self::delete_activity( absint( $_GET['activity'] ) );

                wp_redirect( esc_url( add_query_arg() ) );

                break;

            case 'bulk-delete':
                $ids = esc_sql( $_POST['bulk-selected'] );
                foreach ( $ids as $id ) {
                    self::delete_activity( $id );
                }

                wp_redirect( esc_url( add_query_arg() ) );

                break;

            case 'bulk-export':
                $ids = esc_sql( $_POST['bulk-selected'] );

                self::export_activities( $ids );

                wp_redirect( esc_url( add_query_arg() ) );
                break;

            default:
                // do nothing or something else
                return;
                break;
        }

        return;

    }

I hope this helps you 😉

Leave a Comment