Custom bulk_action

I think the latest major release warrants a new answer to this question, considering the popularity of this question.

Since WordPress 4.7 (released December 2016) it is possible to add custom bulk actions without using JavaScript.

The filter bulk_actions-{$screen} (e.g. bulk_actions-edit-page for the pages overview) now allows you to add custom bulk actions. Furthermore, a new action called handle_bulk_actions-{$screen} (e.g. handle_bulk_actions-edit-page) allows you to handle execution of the action.

This is all explained pretty well in this blog post.
For example, let’s say we want to add a bulk action to email the titles of the selected items on the pages overview. We could do it like this:

For a small example, where we add an action to the bulk actions dropdown and add a handler function to it.

Adding the bulk action to the dropdown:

function wpse29822_page_bulk_actions( $actions ) {
    // Add custom bulk action
    $actions['my-action-handle'] = __( 'My Custom Bulk Action' );
    return $actions;
}
add_action( 'bulk_actions-edit-page', 'wpse29822_page_bulk_actions' );

Adding a handler for the bulk action:

function wpse29822_page_bulk_actions_handle( $redirect_to, $doaction, $post_ids ) {
    // Check whether action that user wants to perform is our custom action
    if ( $doaction == 'my-action-handle' ) {
        // Do stuff
    }
    return $redirect_to;
}
add_action( 'handle_bulk_actions-edit-page', 'wpse29822_page_bulk_actions_handle', 10, 3 );

Leave a Comment