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).

enter image description here

Here I want to select terms and finally, by clicking 'Update Button', bulk assign terms to attachments.

How can I achieve this?

1 Answer 1


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

  1. PHP: create a function that output the html markup for the select terms UI. We can copy a lot from the standard bulk edit UI for posts see
  2. PHP: hook the function created at point 1. into an ajax action
  3. JS: create a code that add an option to the bulk select menu
  4. JS: Intercept the click on 'Apply' buttons (one in table header one in footer) and if our action is selected, 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
  5. PHP: create a function that accept from $_POST an array of attachments ids and some array of terms (an array for every taxonomy) and just assign the terms to the given attachments
  6. PHP: hook the function created at point 5. in a second ajax action
  7. JS: create an ajax call that runs when the Update button in our UI is clicked. (We have to remember to use jQuery.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 messages
  8. PHP: after we have saved all js in a file, enqueue it only for 'upload.php' and using wp_localize_script to pass to it some variables like nonces and translation strings

We 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:

enter image description here

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:

  1. first part register a taxonomy for attachments, using some standard arguments
  2. second part implements the bulk terms assignment as described here for all taxonomies registered for attachments (so not only for the one registered by plugin)

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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