如何在 WordPress 列表中添加自定义批量操作? [英] How to add custom bulk actions in WordPress list tables?

查看:30
本文介绍了如何在 WordPress 列表中添加自定义批量操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个插件,我必须将用户列入黑名单,因此我需要在 Bulk Actions 下拉菜单中显示另一个名为 Black List 的下拉项目用户页面,在删除选项之后.但是我无法看到这两个操作的来源以及如何将特定用户列入黑名单.

I am developing a plugin for that I have to black list users, so I need to be display one more dropdown item called Black List inside the Bulk Actions dropdown in the Users page, after the Delete option. But I'm unable to see from where these two actions are coming from and also how to black list a particular user.

我的想法是在 user table 中添加一个字段 is_blacklisted 作为 Boolean 和默认值 false 和当应用 Black List 操作时,它会更改为 true.还有其他想法吗?

My idea is to add one more field is_blacklisted in user table as Boolean with default value false and when apply Black List action it changes to true. Any other thoughts?

推荐答案

有一个过滤器,但它只对删除批量操作有用.

There's a filter, but it's only useful to remove bulk actions.

这个 WPSE 问题、答案和评论,有以下解决方法:向下拉列表添加自定义选项使用 jQuery 并钩入 admin_action_$your-action 以捕获提交.

From this WPSE question, answer and comments, there's the following workaround: add a custom option to the dropdown with jQuery and hook into admin_action_$your-action to catch the submission.

钩子 admin_footer-$current_page 用于在特定的管理页面上打印我们的 JavaScript(调整为在其他屏幕中使用).

The hook admin_footer-$current_page is used to print our JavaScript on a specific admin page (adjust to use in other screens).

add_action( 'admin_footer-users.php', 'bulk_footer_so_23541269' );
add_action( 'admin_action_black_list', 'bulk_request_so_23541269' );

function bulk_footer_so_23541269() 
{
    # global $typenow; if( $typenow != 'page' ) return; // if used on edit.php screen
    ?>
    <script type="text/javascript">
        jQuery(document).ready(function($) {
            $('<option>').val('black_list').text('Black list')
                .appendTo("select[name='action'], select[name='action2']");
        });
    </script>
    <?php
}

function bulk_request_so_23541269() 
{
    # Array with the selected User IDs
    wp_die( '<pre>' . print_r( $_REQUEST['users'], true ) . '</pre>' ); 
    // $_REQUEST['post'] if used on edit.php screen
}

您对阻止用户的怀疑值得另一个问题,但我会先在这里开始研究.

Your doubt about blocking a user deserves another question, but I'd start a research here first.

这篇关于如何在 WordPress 列表中添加自定义批量操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆