Nisham Mahsin Nisham Mahsin - 1 month ago 10
PHP Question

How to add custom bulk actions in WordPress list tables?

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.

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?

Answer

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

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.

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.

Comments