钩子文档

bulk_actions-{$this->screen->id}

💡 云策文档标注

概述

bulk_actions-{$this->screen->id} 是一个动态过滤器钩子,用于修改 WordPress 后台列表表格中的批量操作菜单项。它允许开发者根据当前屏幕 ID 添加、移除或修改批量操作。

关键要点

  • 钩子名称格式为 bulk_actions-{screenid},其中 screenid 是目标管理屏幕的 ID。
  • 参数 $actions 是一个关联数组,表示可用的批量操作。
  • 从 WordPress 4.7 开始,可通过此过滤器添加自定义批量操作,并使用 handle_bulk_actions-{screenid} 钩子处理功能。
  • WordPress 5.6.0 支持在批量操作中使用选项数组以创建 optgroup。

代码示例

// 示例:从用户页面移除批量操作
add_filter( 'bulk_actions-users', 'my_custom_bulk_actions' );
function my_custom_bulk_actions( $actions ) {
    unset( $actions['delete'] ); // 移除删除操作
    return $actions;
}

注意事项

  • 修改时需注意实际选项值,而非仅下拉菜单中显示的文本,例如插件列表页的删除选项值为 "delete-selected"。
  • 钩子名称使用连字符而非下划线,需遵循 WordPress 命名约定。

📄 原文内容

Filters the items in the bulk actions menu of the list table.

Description

The dynamic portion of the hook name, $this->screen->id, refers to the ID of the current screen.

Parameters

$actionsarray
An array of the available bulk actions.

More Information

  • This hook allows you to remove items from the bulk actions dropdown on any specified admin screen.
  • Bulk actions are a simple associative array.
  • The filter hook follows the format ‘bulk_actions-screenid‘, where screenid is the id of the admin screen that you want to affect.
  • As of version 4.7, custom bulk actions can be added using this filter. You can add functionality to custom bulk actions using ‘handle_bulk_actions-screenid‘, where screenid is the id of the admin screen that you want to affect.

Source

$this->_actions = apply_filters( "bulk_actions-{$this->screen->id}", $this->_actions ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores

Changelog

Version Description
5.6.0 A bulk action can now contain an array of options in order to create an optgroup.
3.1.0 Introduced.

User Contributed Notes

  1. Skip to note 2 content

    Example migrated from Codex:

    The following example removes an action from the Bulk Actions drop-down on the Users page:

    Pay attention to the actual option values, not just the text displayed in the dropdown, on the page you wish to modify. For example, the delete option on the Plugins list page is “delete-selected” (as of 4.2.2).