seravo / wp-custom-bulk-actions
为WordPress中任何类型的帖子添加自定义批量操作。
0.1.4
2018-04-01 20:16 UTC
This package is not auto-updated.
Last update: 2024-09-15 10:02:09 UTC
README
目前WordPress不允许您添加自定义批量操作。请参阅 codex。使用此插件添加它们非常简单。
这基于在这里找到的解决方案 here,但使其更易于使用。
安装
通过 Composer via Packagist 或 GitHub Plugin Search 安装。
插件
此插件添加了一个名为 Seravo_Custom_Bulk_Action 的类
类函数
构造函数,默认帖子类型为 post
new Seravo_Custom_Bulk_Action(array('post_type' => $custom_post));
添加操作。您必须定义至少 menu_text 和回调函数。
register_bulk_action(array( 'menu_text' => $your_menu_text, 'admin_notice' => $display_text_for_admin, 'action_name' => $optional_action_name, 'callback' => $anonymous_function ));
admin_notice 参数也接受数组形式的复数文本(感谢 @cyberwani)。例如
register_bulk_action(array( 'menu_text' => $your_menu_text, 'admin_notice' => 'admin_notice'=>array( 'single' => '%s Appointment cancelled.', 'plural' => '%s Appointments cancelled.', ), 'action_name' => $optional_action_name, 'callback' => $anonymous_function ));
您的匿名回调函数需要 post_ids 作为参数
function($post_ids) { //Do something here }; $post_ids //Array of post IDs selected by user in admin panel
初始化函数到 WordPress
init();
示例及使用方法
安装插件并在 functions.php
中定义您的批量操作。
在这个例子中,我们将更新自定义帖子名为 property 的 _property_status 元数据
//Define bulk actions for custom-post-type property $bulk_actions = new Seravo_Custom_Bulk_Action(array('post_type' => 'property')); //ACTION EXAMPLE 1: $bulk_actions->register_bulk_action(array( 'menu_text'=>'Mark as sold (Myyty)', 'admin_notice'=>'Properties marked as sold', 'callback' => function($post_ids) { //Do something with $post_ids here //In this example properties are marked as sold foreach ($post_ids as $post_id) { update_post_meta($post_id,"_property_status", "sold"); } return true; })); //ACTION EXAMPLE 2, non-ascii chars in menutext: //Defining the action_name is optional but useful if you want to have non-ascii chars in menu_text $bulk_actions->register_bulk_action(array( 'menu_text'=>'Mark for sale (Myytäväksi)', 'admin_notice'=>'Properties marked for sale', 'action_name'=>'for_sale', 'callback' => function($post_ids) { //Do something with $post_ids here //In this example properties are marked for sale foreach ($post_ids as $post_id) { update_post_meta($post_id,"_property_status", "sale"); } return true; })); //Finally init actions $bulk_actions->init();