Adding Custom Bulk Actions to WordPress Plugin

Posted on 19th June 2023

Adding Custom Bulk Actions to WordPress Plugin
==============================================

One of the most powerful features of WordPress is its extensibility.

As developers, we can tap into the vast ecosystem of WordPress plugins to add features and functionality to our projects. However, sometimes the plugins available don’t quite fit our needs.

In these cases, we can create our own plugins.

In this article, we’ll walk through the process of adding a custom bulk action to a WordPress plugin.

We’ll be working with the excellent Advanced Custom Fields plugin, which we’ll use to add a custom field to our posts. We’ll then create a custom bulk action that will allow us to set the value of this custom field for multiple posts at once.

Installing Advanced Custom Fields
——————————–

The first thing we need to do is install and activate the Advanced Custom Fields plugin.

Once the plugin is activated, we need to create a new field group. A field group is a collection of custom fields that we can add to a post type.

In the field group editor, we give our field group a title and add a single field. In our case, we want to add a text field, so we select the Text field type.

We then give the field a label and a name. The label is what will be shown to the user when they’re editing a post. The name is what we’ll use to reference the field when we’re working with it in code.

![Adding a field group in Advanced Custom Fields](https://i.imgur.com/pU6oTZS.png)

Once our field group is saved, we need to assign it to our post type. In the location rules section, we select the posts checkbox and save the field group.

![Assigning a field group to our post type](https://i.imgur.com/uG2kvkT.png)

Now that our field group is created and assigned, we can add it to our posts.

Creating the Custom Bulk Action
——————————-

With our field in place, we can now create our custom bulk action.

We’ll start by registering the bulk action with WordPress. We do this by hooking into the bulk_actions-{screen} filter.

In our callback function, we return an associative array. The keys in the array are the values that will be passed to our bulk action handler, while the values are the labels that will be shown to the user.

“`php
function my_custom_bulk_action() {
$screen = get_current_screen();
if ( $screen->id != ‘edit-post’ ) {
return;
}

$actions[‘my_custom_action’] = ‘My Custom Action’;

return $actions;
}
add_filter( ‘bulk_actions-edit-post’, ‘my_custom_bulk_action’ );
“`

Next, we need to handle the bulk action when it’s submitted. We do this by hooking into the handle_bulk_actions-{screen} filter.

In our callback function, we first check that the submitted action is our custom action. We then get the posts that have been selected and loop through them.

For each post, we update the value of our custom field. We then redirect the user back to the posts screen and exit the script.

“`php
function my_custom_bulk_action_handler( $redirect_to, $action, $post_ids ) {
if ( $action != ‘my_custom_action’ ) {
return $redirect_to;
}

foreach ( $post_ids as $post_id ) {
update_field( ‘my_custom_field’, ‘My Custom Value’, $post_id );
}

$redirect_to = add_query_arg( ‘bulk_posts_updated’, count( $post_ids ), $redirect_to );

return $redirect_to;
}
add_filter( ‘handle_bulk_actions-edit-post’, ‘my_custom_bulk_action_handler’, 10, 3 );
“`

Finally, we need to provide feedback to the user after the bulk action has been executed. We do this by hooking into the bulk_admin_notices action.

In our callback function, we check if the bulk_posts_updated query variable has been set. If it has, we display a notice with the number of posts that have been updated.

“`php
function my_custom_bulk_action_admin_notice() {
if ( empty( $_REQUEST[‘bulk_posts_updated’] ) ) {
return;
}

$updated = intval( $_REQUEST[‘bulk_posts_updated’] );

printf( ‘

‘ .
_n( ‘%s post updated.’, ‘%s posts updated.’, $updated, ‘textdomain’ ) . ‘

‘, $updated
);
}
add_action( ‘admin_notices’, ‘my_custom_bulk_action_admin_notice’ );
“`

And that’s it! We now have a working custom bulk action.

Conclusion
———-

In this article, we’ve seen how to add a custom bulk action to a WordPress plugin.

We started by installing and configuring the Advanced Custom Fields plugin. We then created a custom bulk action and added it to the posts screen. Finally, we handled the bulk action and provided feedback to the user.

Although we’ve used the Advanced Custom Fields plugin in this article, the same technique can be used with any plugin.

Do you have any questions about adding custom bulk actions to WordPress? Let us know in the comments.

Custom Bulk Actions for WordPress Plugin

Adding custom bulk actions to your WordPress plugin can be a great way to add extra functionality to your site. Bulk actions can be used to perform a variety of tasks, such as deleting multiple posts or changing the author of multiple posts.

In this article, we will show you how to add custom bulk actions to your WordPress plugin.

Adding Custom Bulk Actions

First, you will need to create a new file in your plugin directory. This file will contain the code for your custom bulk action.

Next, you will need to add the following code to your new file:

current_action();

// Check if the action is our custom bulk action
if ( ‘bulk_custom_action’ === $action ) {

// Get the selected posts
$posts = $_POST[‘post’];

// Loop through the selected posts
foreach ( $posts as $post_id ) {

// Perform the action for each post

}

// Redirect to the original page
wp_redirect( $_SERVER[‘HTTP_REFERER’] );
exit;
}
}
add_action( ‘admin_init’, ‘custom_bulk_action’ );

?>

This code will check for the custom bulk action, and then perform the action for each selected post.

You can now go to the Posts screen and select multiple posts. Then, select your custom bulk action from the Bulk Actions dropdown and click Apply.

Your custom bulk action will now be performed on the selected posts.

Adding Custom Bulk Actions to the WordPress Toolbar

If you want to make your custom bulk action more easily accessible, you can add it to the WordPress toolbar.

To do this, you will need to add the following code to your plugin file:

$post_type,
‘bulk_custom_action’ => ‘1’,
), admin_url( ‘edit.php’ ) );
echo ‘‘ . __( ‘Custom Bulk Action’, ‘textdomain’ ) . ‘‘;
}
}
add_action( ‘admin_bar_menu’, ‘custom_bulk_action_toolbar’, 100 );

?>

This code will check for the custom bulk action, and then add a button to the WordPress toolbar.

When you click on the button, your custom bulk action will be performed on the selected posts.

Conclusion

In this article, we showed you how to add custom bulk actions to your WordPress plugin.

Adding custom bulk actions can be a great way to add extra functionality to your WordPress site.