Skip to content

Instantly share code, notes, and snippets.

@goodevilgenius
Last active October 26, 2017 14:15
Show Gist options
  • Save goodevilgenius/755bc2e7b78ee0b26890 to your computer and use it in GitHub Desktop.
Save goodevilgenius/755bc2e7b78ee0b26890 to your computer and use it in GitHub Desktop.
[WP Media Categories] Wordpress plugin to add categories and tags to media items #Wordpress
<?php
/**
* Plugin Name: Media Categories and Tags
* Plugin URI: https://gist.github.com/goodevilgenius/755bc2e7b78ee0b26890
* Description: Adds categories to media
* Version: 0.3.0
* Author: Dan Jones
* Author URI: http://danielrayjones.com/
* License: MIT
*/
function drj_image_taxonomy_settings_init() {
add_settings_section(
'drj_image_tax_section'
, ''
, 'drj_image_tax_section_cb'
, 'media'
);
add_settings_field(
'drj_image_tax_separate'
, 'Media Categories and Tags'
, 'drj_image_tax_cb'
, 'media'
, 'drj_image_tax_section'
);
register_setting('media', 'drj_image_tax_separate');
}
add_action('admin_init','drj_image_taxonomy_settings_init');
function drj_image_tax_section_cb() { echo "<!-- Media Settings and Categories -->"; }
function drj_image_tax_cb() {
echo '<input name="drj_image_tax_separate" id="drj_image_tax_separate" type="checkbox" value="1" '
. ' class="code" ' . (get_option('drj_image_tax_separate') == 1 ? 'checked="checked"' : '') . ' />';
echo '<label for="drj_image_tax_separate">Use separate tags and categories for media</label>';
}
/** Register taxonomy for images */
function drj_register_taxonomy_for_images() {
if (get_option('drj_image_tax_separate') == 1) {
$labels = array(
'name' => 'Media Categories',
'singular_name' => 'Media Category',
'menu_name' => 'Media Category',
'all_items' => 'All Media Categories',
'parent_item' => 'Parent Category',
'parent_item_colon' => 'Parent Category:',
'new_item_name' => 'New Media Category',
'add_new_item' => 'Add New Media Category',
'edit_item' => 'Edit Media Category',
'update_item' => 'Update Media Category',
'separate_items_with_commas' => 'Separate categories with commas',
'search_items' => 'Search Media Categories',
'add_or_remove_items' => 'Add or remove categories',
'choose_from_most_used' => 'Choose from the most used category',
'not_found' => 'Not Found',
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
);
register_taxonomy( 'media_category', 'attachment', $args );
$labels = array(
'name' => 'Media Tags',
'singular_name' => 'Media Tag',
'menu_name' => 'Media Tag',
'all_items' => 'All Media Tags',
'parent_item' => 'Parent Tag',
'parent_item_colon' => 'Parent Tag:',
'new_item_name' => 'New Media Tag',
'add_new_item' => 'Add New Media Tag',
'edit_item' => 'Edit Media Tag',
'update_item' => 'Update Media Tag',
'separate_items_with_commas' => 'Separate tags with commas',
'search_items' => 'Search Media Tags',
'add_or_remove_items' => 'Add or remove tags',
'choose_from_most_used' => 'Choose from the most used tag',
'not_found' => 'Not Found',
);
$args = array(
'labels' => $labels,
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
);
register_taxonomy( 'media_tag', 'attachment', $args );
register_taxonomy_for_object_type( 'media_category', 'attachment' );
register_taxonomy_for_object_type( 'media_tag', 'attachment' );
} else {
register_taxonomy_for_object_type( 'category', 'attachment' );
register_taxonomy_for_object_type( 'post_tag', 'attachment' );
}
}
add_action( 'init', 'drj_register_taxonomy_for_images' );
/** Add a category and tag filter to images */
function drj_add_image_category_filter() {
$screen = get_current_screen();
if ('upload' == $screen->id) {
if (get_option('drj_image_tax_separate') == 1) {
function drj_print_media_category($cat, $level = 0) {
echo '<option class="level-'.$level.'" value="'.$cat->slug.'">'.str_repeat('&nbsp;',$level*3).$cat->name."</option>\n";
$sub_cats = get_terms('media_category','parent='.$cat->term_id);
foreach($sub_cats as $c) drj_print_media_category($c, $level+1);
}
echo '<input type="hidden" name="taxonomy" value="media_category" />';
$cats = get_terms('media_category','parent=0');
echo '<select name="term" id="media-category-term" class="postform">';
echo '<option value="" selected="selected">View all categories</option>';
foreach($cats as $cat) drj_print_media_category($cat);
echo "</select>\n";
} else {
$dropdown_options = array( 'show_option_all' => __( 'View all categories', 'drj' )
, 'hide_empty' => false
, 'hierarchical' => true
, 'orderby' => 'name'
, 'hide_if_empty' => true
);
wp_dropdown_categories($dropdown_options);
}
}
}
add_action( 'restrict_manage_posts', 'drj_add_image_category_filter' );
@paaljoachim
Copy link

Hi

I happen to come across your code. In the media library I am missing having the opportunity to select en existing media category or tag. Right now it shows a text field for both. It would be nice with checkboxes similar to selecting the regular post categories through a post page Categories sidebar widget. Have a great day!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment