Using WordPress ‘handle_bulk_actions-{$screen}’ PHP filter

The handle_bulk_actions-{$screen} WordPress PHP filter fires when a custom bulk action should be handled. The redirect link should be modified with success or failure feedback from the action to be used to display feedback to the user. The dynamic portion of the hook name, $screen, refers to the current screen ID.

Usage

add_filter('handle_bulk_actions-{$screen}', 'my_custom_bulk_action_handler', 10, 3);

function my_custom_bulk_action_handler($sendback, $doaction, $items) {
  // your custom code here
  return $sendback;
}

Parameters

  • $sendback (string) – The redirect URL.
  • $doaction (string) – The action being taken.
  • $items (array) – The items to take the action on. Accepts an array of IDs of posts, comments, terms, links, plugins, attachments, or users.

More information

See WordPress Developer Resources: handle_bulk_actions-{$screen}

Examples

Delete multiple posts

To delete multiple posts with a custom bulk action:

add_filter('handle_bulk_actions-edit-post', 'my_bulk_delete_posts', 10, 3);

function my_bulk_delete_posts($sendback, $doaction, $items) {
  if ($doaction == 'delete_posts') {
    foreach ($items as $post_id) {
      // Delete the post
      wp_delete_post($post_id, true);
    }
    $sendback = add_query_arg('deleted', count($items), $sendback);
  }
  return $sendback;
}

Update multiple user roles

To update multiple user roles with a custom bulk action:

add_filter('handle_bulk_actions-users', 'my_bulk_update_user_roles', 10, 3);

function my_bulk_update_user_roles($sendback, $doaction, $items) {
  if ($doaction == 'update_roles') {
    $updated = 0;
    foreach ($items as $user_id) {
      // Update the user role
      $user = new WP_User($user_id);
      $user->set_role('editor');
      $updated++;
    }
    $sendback = add_query_arg('updated', $updated, $sendback);
  }
  return $sendback;
}

Activate multiple plugins

To activate multiple plugins with a custom bulk action:

add_filter('handle_bulk_actions-plugins', 'my_bulk_activate_plugins', 10, 3);

function my_bulk_activate_plugins($sendback, $doaction, $items) {
  if ($doaction == 'activate_plugins') {
    $activated = 0;
    foreach ($items as $plugin) {
      // Activate the plugin
      activate_plugin($plugin);
      $activated++;
    }
    $sendback = add_query_arg('activated', $activated, $sendback);
  }
  return $sendback;
}

Unapprove multiple comments

To unapprove multiple comments with a custom bulk action:

add_filter('handle_bulk_actions-edit-comments', 'my_bulk_unapprove_comments', 10, 3);

function my_bulk_unapprove_comments($sendback, $doaction, $items) {
  if ($doaction == 'unapprove_comments') {
    $unapproved = 0;
    foreach ($items as $comment_id) {
      // Unapprove the comment
      wp_set_comment_status($comment_id, '0');
      $unapproved++;
    }
    $sendback = add_query_arg('unapproved', $unapproved, $sendback);
}
return $sendback;
}

Delete multiple terms

To delete multiple terms with a custom bulk action

add_filter('handle_bulk_actions-edit-category', 'my_bulk_delete_terms', 10, 3);

function my_bulk_delete_terms($sendback, $doaction, $items) {
  if ($doaction == 'delete_terms') {
    $deleted = 0;
    foreach ($items as $term_id) {
      // Delete the term
      wp_delete_term($term_id, 'category');
      $deleted++;
    }
    $sendback = add_query_arg('deleted', $deleted, $sendback);
  }
  return $sendback;
}