Add Snippet To Project
As a WordPress user, you may have come across situations where you need to perform the same action on multiple posts or items at once. This can be time-consuming and repetitive, but fear not! WordPress provides a solution called "Bulk Actions" that allows you to apply custom actions to multiple items simultaneously. In this article, we will guide you through the process of creating your own custom bulk actions, streamlining your workflow and saving you valuable time. Let’s dive in!
function wpturbo_custom_bulk_actions( $actions ) {
$actions['wpturbo_custom_bulk_action'] = __( 'My Custom Bulk Action', 'wpturbo' );
return $actions;
}
add_filter( 'bulk_actions-edit-post', 'wpturbo_custom_bulk_actions' );
function wpturbo_handle_custom_bulk_action( $redirect_to, $doaction, $post_ids ) {
if ( $doaction !== 'wpturbo_custom_bulk_action' ) {
return $redirect_to;
}
// Perform your custom bulk action logic here
return $redirect_to;
}
add_filter( 'handle_bulk_actions-edit-post', 'wpturbo_handle_custom_bulk_action', 10, 3 );
The code snippet provided allows you to create your own custom bulk action in WordPress. A bulk action is a feature in the WordPress admin area that allows you to perform an action on multiple items at once, such as deleting or editing multiple posts.
The first part of the code snippet defines a function called wpturbo_custom_bulk_actions(). This function is responsible for adding your custom bulk action to the list of available actions. In this example, the custom action is named "My Custom Bulk Action". You can change this to any name you prefer by modifying the second parameter of the __() function.
To add your custom action, you need to use the bulk_actions-{$screen_id} hook, where {$screen_id} is the ID of the screen where you want to add the action. In this case, the edit-post screen is targeted, which is the screen for editing posts.
function wpturbo_custom_bulk_actions( $actions ) {
$actions['wpturbo_custom_bulk_action'] = __( 'My Custom Bulk Action', 'wpturbo' );
return $actions;
}
add_filter( 'bulk_actions-edit-post', 'wpturbo_custom_bulk_actions' );
The second part of the code defines a function called wpturbo_handle_custom_bulk_action(). This function is responsible for handling the logic of the custom bulk action when it is triggered.
The function first checks if the current bulk action is the custom action we defined earlier. If it’s not, it simply returns the original redirect URL without performing any action. This ensures that the function only handles the custom action and doesn’t interfere with other actions.
If the current bulk action is the custom action, you can add your own custom logic here. This is where you can perform any desired operations on the selected posts.
function wpturbo_handle_custom_bulk_action( $redirect_to, $doaction, $post_ids ) {
if ( $doaction !== 'wpturbo_custom_bulk_action' ) {
return $redirect_to;
}
// Perform your custom bulk action logic here
return $redirect_to;
}
add_filter( 'handle_bulk_actions-edit-post', 'wpturbo_handle_custom_bulk_action', 10, 3 );
You can replace the comment // Perform your custom bulk action logic here with your own custom code. This code will be executed when the custom bulk action is triggered. For example, you could loop through the $post_ids array and perform a specific action on each selected post.
Remember to return the $redirect_to URL at the end of the function. This ensures that the user is redirected to the correct page after the bulk action is performed.
That’s it! With this code snippet, you can easily create your own custom bulk actions in WordPress and implement your own logic for handling those actions.
