How to Create a Custom Bulk Action

WordPress 4.7 introduced a way to create your own custom bulk actions for your post types.

Before that, there were plugins to do it, but now that it’s in the core, it’s way easier to add a new bulk action, potentially you only need three functions!

Let’s see an example together about how to add a Move to Drafts bulk action, I know that is already possible if you bulk edit posts, but it involves many clicks, while with a bulk action is only a couple clicks!

Open the file functions.php in wp-content/themes/your-child-theme-name/ and add this code at the end of it:


/**
* Adds a new item into the Bulk Actions dropdown.
*/
function register_my_bulk_actions( $bulk_actions ) {
$bulk_actions['draft_posts'] = __( 'Move to Draft', 'domain' );
return $bulk_actions;
}
add_filter( 'bulk_actions-edit-post', 'register_my_bulk_actions' );

view raw

functions.php

hosted with ❤ by GitHub

This code adds a new item in the Bulk Actions dropdown in the posts list. It does nothing yet.

After it, add this code:


/**
* Handles the bulk action.
*/
function my_bulk_action_handler( $redirect_to, $action, $post_ids ) {
if ( $action !== 'draft_posts' ) {
return $redirect_to;
}
foreach ( $post_ids as $post_id ) {
wp_update_post( array(
'ID' => $post_id,
'post_status' => 'draft',
) );
}
$redirect_to = add_query_arg( 'bulk_draft_posts', count( $post_ids ), $redirect_to );
return $redirect_to;
}
add_filter( 'handle_bulk_actions-edit-post', 'my_bulk_action_handler', 10, 3 );

view raw

functions.php

hosted with ❤ by GitHub

This function handles instead the behaviour of your custom bulk action. In this case we first make sure that the action we are running is the custom one, not another. Then we cycle through the posts and we update their status.

In the end, we redirect back adding the total count of posts we edited in the URL.

The last part of the code is the following, which simply adds a notice that shows how many posts have been affected:


/**
* Shows a notice in the admin once the bulk action is completed.
*/
function my_bulk_action_admin_notice() {
if ( ! empty( $_REQUEST['bulk_draft_posts'] ) ) {
$drafts_count = intval( $_REQUEST['bulk_draft_posts'] );
printf(
'<div id="message" class="updated fade">' .
_n( '%s post moved to drafts.', '%s posts moved to drafts.', $drafts_count, 'domain' )
. '</div>',
$drafts_count
);
}
}
add_action( 'admin_notices', 'my_bulk_action_admin_notice' );

view raw

functions.php

hosted with ❤ by GitHub

Now you know how to add custom bulk actions, what will you use them for?


More Posts That You Might Like…


4 responses to “How to Create a Custom Bulk Action”

  1. Baikare Sandeep Avatar
    Baikare Sandeep

    How can I add custom bulk action for Users? I could not add bulk action for users table.

    1. Hi Baikare,
      The hook is handle_bulk_actions-{screen-id}. The screen ID for the Users page is users.

      I’m guessing that you simply need to change the add_action to handle_bulk_actions-users and define your custom actions there.

      I didn’t test this, but according to the documentation, it should work.

      1. Yes It was worked for me. Thanks.

  2. How to add bulk action to cancel bulk orders?

Leave a Reply

Categories

Newsletter

Receive new articles from this blog directly in your inbox!

No spam guaranteed!

Blog at WordPress.com.

%d bloggers like this: