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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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' ); |
This code adds a new item in the Bulk Actions dropdown in the posts list. It does nothing yet.
After it, add this code:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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 ); |
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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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' ); |
Now you know how to add custom bulk actions, what will you use them for?
Leave a Reply