2

I am working on a plugin that adds meta data for each user to the users.php table that is displayed by adding columns. I have that done but I would like to also add a button that will delete the meta data of the users when pressed. I'm not sure how I can add it. I would like it to be to the right of the 'change' button. I thought that the behavior would be to reload the page but when it does, I'll have it delete the meta data for each user.

Where would I start to add that button to the page? Is this the best way to go about this?

Thanks!

5

Okay.. you COULD add a button like you mentioned; but I think this is going to require a lot more code. The users.php page is using the WP List Table class.. which means we can hook into the bulk actions and add our custom value there.

So, let's create a function to add a new value into the bulk actions dropdown box:

add_action('admin_footer', 'my_user_del_button');
function my_user_del_button() {
    $screen = get_current_screen();
    if ( $screen->id != "users" )   // Only add to users.php page
        return;
    ?>
    <script type="text/javascript">
        jQuery(document).ready(function($) {
            $('<option>').val('del_user_meta').text('Delete User Meta').appendTo("select[name='action']");
        });
    </script>
    <?php
}

This will add the "Delete User Meta" value to the bulk actions dropdown box. Now, we need a function to actually process the data being sent:

add_action('load-users.php', 'my_users_page_loaded');
function my_users_page_loaded() {
    if(isset($_GET['action']) && $_GET['action'] === 'del_user_meta') {  // Check if our custom action was selected
        $del_users = $_GET['users'];  // Get array of user id's which were selected for meta deletion
        if ($del_users) {  // If any users were selected
            foreach ($del_users as $del_user) {
            delete_user_meta($del_user, 'YOUR_METADATA_KEY_TO_BE_REMOVED');  // Change this meta key to match the key you would like to delete; or an array of keys.
            }
        }
    }
}

Here, we iterate through each of the users we placed a checkmark next to. Then, it will delete the meta_key you specified for each of those selected users.

NOTE: You need to change the YOUR_METADATA_KEY_TO_BE_REMOVED string to the actual name of the meta_key you would like to delete. If you are wanting to delete more than a single meta key, you will need to add multiple delete_user_meta() functions.

  • josh, thanks for the code but I'm getting this error: Notice: Undefined index: users and it's from this line: $del_users = $_GET['users']; It seems that it does not know what users is. Any suggestion? – graphical_force Nov 9 '13 at 0:00
  • Nevermind, I got it to work but I notice if someone does not have a user checked, and they attempt to delete some meta data, it will throw an error. I'm guessing that I need to test to see if any users were selected. Do you have any suggestion on testing if $del_users = $_GET['users']; is empty or is there a better way to do it? Thanks again! – graphical_force Nov 9 '13 at 0:09
  • My answer has been edited above (second block). Sorry about that! – josh Nov 9 '13 at 3:25
  • My pleasure :) Glad it worked. Thanks for accepting! – josh Nov 9 '13 at 7:16
1

Although the JavaScript approach using the admin_footer hook what @josh mentions works in most cases. It's definitely not the best approach to add an option to the bulk action dropdown. Because there is a specific hook for that, wooohoee yeah! Try the bulk_actions-{$screen_id} filter hook.

add_filter('bulk_actions-users', function($actions) {
    $actions['foo'] = __('Foo bar', 'my-namespace');
    return $actions;
});

After you got the view ready, it's time to setup the controller for that action. Where we will use the handle_bulk_actions-{$screen_id} filter hook.

add_filter('handle_bulk_actions-users', function($redirect, $action, $ids) {
    foreach ($ids as $id) {
        // Perform action for specific id.
    }
    return $redirect;
}, 10, 3);

Source: Custom Bulk Actions

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.