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!
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:
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:
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 multipledelete_user_meta()
functions.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 thebulk_actions-{$screen_id}
filter hook.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.Source: Custom Bulk Actions