Using WordPress ‘manage_taxonomies_for_attachment_columns’ PHP filter

The manage_taxonomies_for_attachment_columns WordPress PHP filter allows you to modify the taxonomy columns displayed for attachments in the Media list table.

Usage

add_filter('manage_taxonomies_for_attachment_columns', 'your_custom_function', 10, 2);

function your_custom_function($taxonomies, $post_type) {
    // your custom code here
    return $taxonomies;
}

Parameters

  • $taxonomies (string[]): An array of registered taxonomy names to show for attachments.
  • $post_type (string): The post type, default is ‘attachment’.

More information

See WordPress Developer Resources: manage_taxonomies_for_attachment_columns

Examples

Adding a custom taxonomy to attachment columns

To add a custom taxonomy called ‘media_category’ to the attachment columns:

add_filter('manage_taxonomies_for_attachment_columns', 'add_media_category_to_columns', 10, 2);

function add_media_category_to_columns($taxonomies, $post_type) {
    if ($post_type == 'attachment') {
        $taxonomies[] = 'media_category';
    }
    return $taxonomies;
}

Removing a taxonomy from attachment columns

To remove the ‘category’ taxonomy from the attachment columns:

add_filter('manage_taxonomies_for_attachment_columns', 'remove_category_from_columns', 10, 2);

function remove_category_from_columns($taxonomies, $post_type) {
    if (($key = array_search('category', $taxonomies)) !== false) {
        unset($taxonomies[$key]);
    }
    return $taxonomies;
}

Replacing one taxonomy with another in attachment columns

To replace the ‘category’ taxonomy with ‘media_category’ taxonomy in the attachment columns:

add_filter('manage_taxonomies_for_attachment_columns', 'replace_category_with_media_category', 10, 2);

function replace_category_with_media_category($taxonomies, $post_type) {
    if (($key = array_search('category', $taxonomies)) !== false) {
        $taxonomies[$key] = 'media_category';
    }
    return $taxonomies;
}

Showing only specific taxonomies for attachment columns

To show only ‘media_category’ and ‘media_tag’ taxonomies in the attachment columns:

add_filter('manage_taxonomies_for_attachment_columns', 'show_specific_taxonomies', 10, 2);

function show_specific_taxonomies($taxonomies, $post_type) {
    if ($post_type == 'attachment') {
        $taxonomies = array('media_category', 'media_tag');
    }
    return $taxonomies;
}

Adding a custom taxonomy conditionally

To add a custom taxonomy called ‘media_location’ to the attachment columns only for administrators:

add_filter('manage_taxonomies_for_attachment_columns', 'add_media_location_for_admins', 10, 2);

function add_media_location_for_admins($taxonomies, $post_type) {
    if ($post_type == 'attachment' && current_user_can('manage_options')) {
        $taxonomies[] = 'media_location';
    }
    return $taxonomies;
}