I make WordPress and MyBB plugins on request! Check my Plugin Development page.

How to Extend Bulk Actions in WordPress

Programming,WordPress 15 August 2009 | 4 Comments

While developing WordBB, at a certain point I needed to extend the Users panel in the wp-admin area. I wanted to add a custom column with a dropdown menu for each user row. And so I did, but then I quickly realized that there’s no hook that allows to override the behaviour of the bulk “Apply” button. I felt like I needed some sort of hack, so I looked at some WordPress core code and managed to accomplish the task.

wpbulkactions

The possible solution I’m using for my plugin is handling the admin_head hook and then checking for my custom form field array in $_GET. This is the simplest and only method I know, I hope this little tip will help any plugin developer with this same problem.

add_action(‘admin_head’, ‘admin_users_update’);

function admin_users_update()
{
        $user_values=$_GET[‘user_values’];
        if(!isset($user_values))
                return;

        // do something with the var
}

Tagged in ,

4 Responses on “How to Extend Bulk Actions in WordPress”

  1. Richard Tape says:

    Hi there! This is an excellent little article, however, would you be able to embellish a little further? I’m trying to add a check box to this page (where you have added a drop down).

    I’ve already created the user meta and it appears on the user’s profile page as you would expect (and it works fine). However, I’m trying to make it a little easier for the site admin to check the status of one particular piece of user meta – therefore wanting it to be displayed on the users.php page.

    For example, my extra piece of usermeta is called “is_this_a_client” and it’s a simple checkbox. Would it be possible for me to add this particular checkbox to the users.php page and allow the admin to simply check it for several users at once instead of having to go into each individual user’s profile? I hope I’m making sense, if I’m not, please do let me know and I’ll explain further.

    Thank you for a great little snippet of code and I hope to hear from you soon! Kind regards from a rainy, dreary Manchester, England.

  2. Hangman says:

    @Richard: You can have a look at those hooks: manage_users_columns, manage_users_custom_column.

    < ?php
    add_filter('manage_users_columns', 'my_users_columns');
    add_action('manage_users_custom_column', 'my_users_custom_column', 8, 3);
    ?>

    First you add a new user column through manage_users_column:

    < ?php
    function my_users_columns($defaults) {
    $defaults['is_this_a_client'] = __('Client?');
    return $defaults;
    }
    ?>

    Then you add content to this new column for each user through manage_users_custom_column.

    < ?php
    function wordbb_users_custom_column($value, $column_name, $id) {
    if($column_name!='is_this_a_client')
    return;

    // TODO: check if this user ($id) is a client in wp_usermeta
    $is_client=true;

    return '< input type="checkbox" name="my_clients['.$id.']" checked=”checked”< ?php endif ?> value=”< ?php echo ($is_client)?'1':'0' ?>” />’;
    }
    ?>

    Note that in the return string we’re creating an array of users (name attribute) which contains the boolean values for each checkbox, which you can now handle in your admin_users_update function.

    < ?php
    function admin_users_update()
    {
    $clients=$_GET['my_clients'];
    foreach($clients as $uid=>$client)
    {
    // update wp_usermeta with new values
    }
    }
    ?>

    There may be errors in this code as I wrote it down right now without trying it.
    Cheers!

  3. Richard Tape says:

    H! thanks for the reply. I’ve sent you an e-mail also, to say thanks. … plus there’s a little request in there, too. :)

  4. Richard Tape says:

    Actually, you can ignore my e-mail. I’ve managed to do what I wanted – I utilised some JavaScript to do it – through a bit of AJAX. If you’re interested in finding out what I did, let me know

Leave a Reply