Adding Custom Bulk Actions in WordPress Plugin
Posted on 16th June 2023
In this article, we will learn how to add custom bulk actions in a WordPress plugin. WordPress has a very useful feature called bulk actions. Bulk actions allow you to perform an action on multiple items at once. For example, you can bulk edit posts or delete multiple posts at once.
In a WordPress plugin, you can add your own custom bulk actions. This can be very useful if you want to add a feature to your plugin that allows users to perform an action on multiple items at once.
Creating a Custom Bulk Action
To create a custom bulk action, you first need to add a new action to the bulk action dropdown menu. This can be done with the following code:
add_filter( ‘bulk_actions-edit-post’, ‘my_custom_bulk_actions’ );
function my_custom_bulk_actions( $bulk_actions ) {
$bulk_actions[‘my_action’] = ‘My Action’;
return $bulk_actions;
}
The code above adds a new action to the bulk action dropdown menu with the label “My Action”.
Processing the Custom Bulk Action
The next step is to process the custom bulk action. This is done with the following code:
add_filter( ‘handle_bulk_actions-edit-post’, ‘my_custom_bulk_action_handler’, 10, 3 );
function my_custom_bulk_action_handler( $redirect_to, $doaction, $post_ids ) {
if ( $doaction !== ‘my_action’ ) {
return $redirect_to;
}
foreach ( $post_ids as $post_id ) {
// Perform actions here
}
$redirect_to = add_query_arg( ‘bulk_posts_updated’, count( $post_ids ), $redirect_to );
return $redirect_to;
}
The code above hooks into the “handle_bulk_actions” action and checks if the action being processed is our custom action. If it is, it will perform the actions we want.
Adding a Custom Bulk Action to a Plugin
Adding a custom bulk action to a plugin is a two-step process.
First, you need to add the action to the bulk action dropdown menu. This can be done with the following code:
add_filter( ‘bulk_actions-edit-post’, ‘my_custom_bulk_actions’ );
function my_custom_bulk_actions( $bulk_actions ) {
$bulk_actions[‘my_action’] = ‘My Action’;
return $bulk_actions;
}
The code above adds a new action to the bulk action dropdown menu with the label “My Action”.
Next, you need to process the custom bulk action. This is done with the following code:
add_filter( ‘handle_bulk_actions-edit-post’, ‘my_custom_bulk_action_handler’, 10, 3 );
function my_custom_bulk_action_handler( $redirect_to, $doaction, $post_ids ) {
if ( $doaction !== ‘my_action’ ) {
return $redirect_to;
}
foreach ( $post_ids as $post_id ) {
// Perform actions here
}
$redirect_to = add_query_arg( ‘bulk_posts_updated’, count( $post_ids ), $redirect_to );
return $redirect_to;
}
The code above hooks into the “handle_bulk_actions” action and checks if the action being processed is our custom action. If it is, it will perform the actions we want.
Bulk actions are a great way to manage a large number of posts or pages at once. In WordPress, bulk actions are typically used to delete, move, or publish a large number of items.
In this tutorial, we will show you how to add a custom bulk action to your WordPress plugin.
Adding a custom bulk action is a two-step process:
First, you need to add a custom bulk action to the bulk action dropdown menu.
Second, you need to handle the form submission when the custom bulk action is selected.
Let’s take a look at each step in detail.
Adding a Custom Bulk Action
The first step is to add a custom bulk action to the bulk action dropdown menu.
This can be done using the ‘bulk_actions’ filter hook.
The ‘bulk_actions’ filter hook is a WordPress filter hook that is used to add custom bulk actions to the bulk action dropdown menu.
The ‘bulk_actions’ filter hook takes two arguments:
$actions: An array of the default bulk actions.
$screen: The current screen ID.
To add a custom bulk action, we need to add a new item to the $actions array.
The new item should be an array with two keys:
Key 1: The key should be the value of the custom bulk action.
Key 2: The value should be the label of the custom bulk action.
For example, let’s say we want to add a custom bulk action called ‘My Custom Bulk Action’.
The code would look like this:
function my_custom_bulk_action( $actions, $screen ) {
$actions[‘my_custom_bulk_action’] = __( ‘My Custom Bulk Action’, ‘textdomain’ );
return $actions;
}
add_filter( ‘bulk_actions-{$screen}’, ‘my_custom_bulk_action’, 10, 2 );
In the code above, we are using the ‘bulk_actions-{$screen}’ filter hook.
The ‘bulk_actions-{$screen}’ filter hook is a WordPress filter hook that is used to add custom bulk actions to a specific screen.
The ‘bulk_actions-{$screen}’ filter hook takes two arguments:
$actions: An array of the default bulk actions.
$screen: The current screen ID.
In the code above, we are adding a custom bulk action to the bulk action dropdown menu on the ‘edit-post’ screen.
You can add a custom bulk action to any screen by replacing ‘edit-post’ with the desired screen ID.
For example, if you want to add a custom bulk action to the ‘edit-page’ screen, you would use the ‘bulk_actions-edit-page’ filter hook.
Handling the Form Submission
The second step is to handle the form submission when the custom bulk action is selected.
This can be done using the ‘handle_bulk_actions-{$screen}’ action hook.
The ‘handle_bulk_actions-{$screen}’ action hook is a WordPress action hook that is used to handle the form submission when a custom bulk action is selected.
The ‘handle_bulk_actions-{$screen}’ action hook takes two arguments:
$redirect_to: The URL to redirect to after the form is submitted.
$doaction: The action to perform.
To handle the form submission, we need to check if the custom bulk action is selected.
If the custom bulk action is selected, we need to perform our custom action.
For example, let’s say we want to handle the form submission for our ‘My Custom Bulk Action’.
The code would look like this:
function my_custom_bulk_action_handler( $redirect_to, $doaction ) {
if ( $doaction !== ‘my_custom_bulk_action’ ) {
return $redirect_to;
}
// Perform custom action here…
$redirect_to = add_query_arg( ‘my_custom_bulk_action’, ‘1’, $redirect_to );
return $redirect_to;
}
add_action( ‘handle_bulk_actions-edit-post’, ‘my_custom_bulk_action_handler’, 10, 2 );
In the code above, we are using the ‘handle_bulk_actions-edit-post’ action hook.
The ‘handle_bulk_actions-edit-post’ action hook is a WordPress action hook that is used to handle the form submission when a custom bulk action is selected on the ‘edit-post’ screen.
The ‘handle_bulk_actions-edit-post’ action hook takes two arguments:
$redirect_to: The URL to redirect to after the form is submitted.
$doaction: The action to perform.
In the code above, we are checking if the custom bulk action is selected.
If the custom bulk action is selected, we are performing our custom action.
You can add a custom bulk action to any screen by replacing ‘edit-post’ with the desired screen ID.
For example, if you want to add a custom bulk action to the ‘edit-page’ screen, you would use the ‘handle_bulk_actions-edit-page’ action hook.
That’s all you need to do to add a custom bulk action to your WordPress plugin.
We hope this article helped you learn how to add a custom bulk action to your WordPress plugin.
You may also want to check out our guide on how to create a custom WordPress plugin.
If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.