9

I'm working on a custom data table to display in the WordPress dashboard. The table populates from a database table I built in a plugin.

I've been using the provided WordPress Custom List Table example for most of my coding questions in this area, but the example doesn't have anything for handling bulk actions. Here is the link for the documented example: http://wordpress.org/extend/plugins/custom-list-table-example/

For processing the bulk actions, the example only provides this:

    function process_bulk_action() {

    //Detect when a bulk action is being triggered...
    if( 'delete'===$this->current_action() ) {

        wp_die('Items deleted!');
    }

}

I want to know how to pull the items selected for the action so I can delete them or edit their database entries accordingly.

1 Answer 1

11

Assuming you're using the standard column_cb() function, the list table will pass the IDs of the selected rows in an array in $_GET, labeled as whatever you assigned to 'singular' in the list table's constructor.

Here's a typical column_cb():

function column_cb($item){
        return sprintf(
            '<input type="checkbox" name="%1$s[]" value="%2$s" />',
            /*$1%s*/ $this->_args['singular'],  //Let's simply repurpose the table's singular label ("video")
            /*$2%s*/ $item->id             //The value of the checkbox should be the record's id
        );
    }

For example, let's say I have a list table that displays videos. The constructor would look like:

function __construct(){
        global $status, $page;

        //Set parent defaults
        parent::__construct( array(
            'singular'  => 'video',     //singular name of the listed records
            'plural'    => 'videos',    //plural name of the listed records
            'ajax'      => false        //does this table support ajax?
        ) );

    }

So, if you check three rows in the list table, select "Delete" from the bulk actions list, and hit apply, you could access the selected rows by using $_GET['video'].

function process_bulk_action() {

        //Detect when a bulk action is being triggered...
        if( 'delete'===$this->current_action() ) {
            foreach($_GET['video'] as $video) {
                //$video will be a string containing the ID of the video
                //i.e. $video = "123";
                //so you can process the id however you need to.
                delete_this_video($video);
            }
        }

    }
2
  • Thanks! Any ideas on how to implement an "edit" bulk action that works the same way as other WordPress tables?
    – Chiubaka
    Commented Aug 22, 2011 at 18:14
  • You could handle it in a similar manner, except the UI would be a lot more involved. I probably wouldn't use the default process_bulk_actions() method in a case like this, because the most usable interface would probably be one that changes the grid rows to editable fields. In that case, you still get the selected items in the $_GET array, but you'd have to override the way the HTML gets outputted in the list table class. Much more complex of a scenario, but the way you access the IDs is the same.
    – Nate Dudek
    Commented Aug 22, 2011 at 20:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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