I’ve created a custom taxonomy for attachments.
Everything ok, but now I want the ability to bulk assign its terms to attachments using the default wordpress media admin (‘upload.php’) bulk actions.
Here in wpse I’ve found a couple of questions/answers regarding bulk actions for admin media table.
Like this one and this other.
These give me a direction on how to add the bulk action on the select menu (there is no filter for it, so I’ve to use javascript) but my case is different:
In linked questions/answers when a bulk action is selected the page form is immediately submitted, instead I want that when I select my bulk action (e.g. “Assign Terms”) and I press the “Apply” button instead of submit the form, I want to show an UI very similar to the one shown when on standard posts the bulk ‘Edit’ action is chose (see image below).
Here I want to select terms and finally, by clicking ‘Update Button’, bulk assign terms to attachments.
How can I achieve this?
As said in the question there is no filter to add bulk actions to the standard select menu, or better, there is one, but it can be used only for remove actions and not for adding.
So, even if for php/wp developers (like me) it seems ugly (and probably is ugly) only chance is use js.
Bad news is that to achieve the requests in the question, we need a lot of javascript, too.
There are no alternatives: once there is no filter or actions, or we use javascript or we modify core files.
Between the two evils let’s choose the lesser.
The workflow
prevent_default()
and make an ajax request that call the function create at point 1. and put the html given as result in the right place on the table$_POST
an array of attachments ids and some array of terms (an array for every taxonomy) and just assign the terms to the given attachmentsjQuery.on()
because UI is added via ajax). It call the function created at point 5 and when done, redirect page to upload.php adding a query var that help us to use admin notices to show success or error messagesWe have to remind that functions that handle ajax requests must do some security checks, like checking the nonce and if the current user can perform the required action.
The following is a preview of how our UI for bulk terms assignment should look like:
Note the different implementation for hierachical and non hierachical taxonomies and the suggest implementation for non hierachical ones.
If someone is interested, I’ve created a plugin that does what is said here and a bit more, in fact plugin is parted in 2:
If someone want to use the second part but not the first (because has already all needed taxonomies registered) a filter is given to block the registration.
If someone want let plugin register the default taxonomy but doesn’t like default args, another filter is provided to change defaults args.
As a bonus feature, using a third filter is possible to use plugin for register other attachment taxonomies, it’s easier than use register_taxonomy because in the plugin
there are a lot of default arguments setted thinking at attachment taxonomies, on the contrary, defaults arguments for register_taxonomy are setted with post taxonomies in mind.
Plugin also implement its own text domain for easy localization.
Plugin code is on GitHub