Skip to content

Instantly share code, notes, and snippets.

@willmot
Created October 11, 2011 10:33
Show Gist options
  • Save willmot/1277790 to your computer and use it in GitHub Desktop.
Save willmot/1277790 to your computer and use it in GitHub Desktop.
WordPress attachment category plugin
<?php
/*
Plugin Name: Attachment Categories
Description: Allows attachments to be categorised
Version: 1.0
Author: Human Made Limited
Author URI: http://hmn.md
*/
/**
* Add the attachment category fields to the edit taxonomy form
*
* @access public
* @param Array $fields - the edit attachment fields
* @param Object $post - the post attachment object
* @return Array $fields
*/
function hm_attachment_categories_field( $fields, $post ) {
$fields['hm_categories'] = array(
'label' => 'Categories',
'input' => 'html',
'html' => hm_attachment_categories_html( $post )
);
unset( $fields['hm_attachment_category'] );
return $fields;
}
add_filter( 'attachment_fields_to_edit', 'hm_attachment_categories_field', 11, 2 );
/**
* hm_save_attachment_categories_field function.
*
* @access public
* @param Array $post - the post attachment object
* @param Array $attachment - the attachment data to be saved
* @return Array $post
*/
function hm_save_attachment_categories_field( $post, $attachment ) {
wp_set_object_terms( $post['ID'], array_map( 'intval', (array) $attachment['hm_categories'] ), 'hm_attachment_category' );
return $post;
}
add_filter( 'attachment_fields_to_save', 'hm_save_attachment_categories_field', 10, 2 );
/**
* Creates and returns the html for the add attachment category field.
*
* @access public
* @param Object $post - the post attachment object
* @return String - the html for the attachment category field
*/
function hm_attachment_categories_html( $post ) {
$categories = get_terms( 'hm_attachment_category', array( 'hide_empty' => false ) );
$html = '
<style type="text/css">
#categorydiv { width: 460px; }
#categories-all { background-color:#FFF; -moz-border-radius: 4px; -webkit-border-radius: 4px; border-radius: 4px; border-style:solid; border-width:1px; height:200px; overflow:auto; padding:0.5em 0.9em; }
#category-tabs li { margin-bottom: 0; }
</style>
<div id="categorydiv">
<div id="categories-all" class="tabs-panel">
<ul id="categorychecklist" class="list:category categorychecklist form-no-clear">';
foreach( $categories as $category )
$html .= '<li id="category-' . $category->term_id . '"><label class="selectit"><input ' . checked( is_object_in_term( $post->ID, 'hm_attachment_category', $category->term_id ), true , false ) . ' type="checkbox" id="in-category-' . $category->term_id . '" name="attachments[' . $post->ID . '][hm_categories][]" value="' . $category->term_id . '" /> ' . $category->name . '</label></li>';
$html .= '</ul>
</div>
</div>';
return $html;
}
/**
* Add the attachment category taxonomy to attachments
*
* @access public
* @param mixed $default
* @param mixed $args
* @return string
*/
function hm_add_hm_attachment_category_taxonomy_to_get_categories( $default, $args ) {
if ( $args['type'] == 'hm_attachment_category' )
return 'hm_attachment_category';
return $default;
}
add_filter( 'get_categories_taxonomy', 'hm_add_hm_attachment_category_taxonomy_to_get_categories', 10, 2 );
/**
* Register the hm_attachment_category function
*
* @access public
* @return void
*/
function hm_attachment_category_taxonomy() {
register_taxonomy( 'hm_attachment_category', array( 'attachment' ), array(
'labels' => array(
'name' => 'Attachment Categories',
'singular_name' => 'Attachment Category',
'search_items' => 'Search Categories',
'all_items' => __( 'All Categories' ),
'parent_item' => __( 'Parent Category' ),
'parent_item_colon' => __( 'Parent Category:' ),
'edit_item' => __( 'Edit Category' ),
'update_item' => __( 'Update Category' ),
'add_new_item' => __( 'Add New Category' ),
'new_item_name' => __( 'New Category Name' ),
'menu_name' => __( 'Categories' ),
),
'rewrite' => false,
'show_ui' => true,
'hierarchical' => true,
'label' => 'Attachment Categories',
'singular_label' => 'Attachment Category'
) );
}
add_action( 'init', 'hm_attachment_category_taxonomy' );
/**
* Add the Media Categories menu item
*
* @access public
* @return null
*/
function hm_attachment_category_admin_menu(){
add_submenu_page( 'upload.php', 'Attachment Categories', 'Categories', 'upload_files', 'edit-tags.php?taxonomy=hm_attachment_category' );
}
add_action( 'admin_menu', 'hm_attachment_category_admin_menu' );
/**
* Force the menu parent to upload.php so the correct menu is highlighted
*
* @access public
* @param mixed $parent_file
* @return string
*/
function hm_attachment_category_admin_menu_parent( $parent_file ) {
global $pagenow;
if ( $pagenow == 'edit-tags.php' && isset( $_GET['taxonomy'] ) && $_GET['taxonomy'] == 'hm_attachment_category' )
return 'upload.php';
return $parent_file;
}
add_filter( 'parent_file', 'hm_attachment_category_admin_menu_parent' );
/**
* Add the categories column to the media table
*
* @access public
* @param Array $columns
* @return Array $columns
*/
function hm_attachment_categories_column( $columns ) {
// We want to add the column in the middle so we need to splice the array into 2 parts.
$start = array_slice( $columns, 0, 3 );
$end = array_slice( $columns, 3 );
$start['hm_categories'] = 'Category';
return array_merge( $start, $end );
}
add_filter( 'manage_upload_columns', 'hm_attachment_categories_column' );
/**
* Show the categories in the category column on the media table
*
* @access public
* @param mixed $column
* @return void
*/
function hm_attachment_category_cell( $column ) {
if ( $column == 'hm_categories' ) :
$categories = get_the_terms( 0, 'hm_attachment_category' );
if ( !empty( $categories ) ) :
$out = array();
foreach ( $categories as $c )
$out[] = "<a href='upload.php?taxonomy=hm_attachment_category&term=$c->slug'> " . esc_html( sanitize_term_field( 'name', $c->name, $c->term_id, 'hm_attachment_category', 'display' ) ) . "</a>";
echo join( ', ', $out );
else :
_e( 'None' );
endif;
endif;
}
add_action( 'manage_media_custom_column', 'hm_attachment_category_cell' );
/**
* Allow attachments to be restricted by category
*
* @access public
* @return null
*/
function restrict_attachments_by_category() {
global $pagenow, $wp_the_query;
if ( $pagenow != 'upload.php' )
return;
wp_dropdown_categories( array(
'show_option_all' => 'View all ' . strtolower( get_taxonomy( 'hm_attachment_category' )->labels->menu_name ),
'taxonomy' => 'hm_attachment_category',
'name' => 'hm_attachment_category',
'orderby' => 'name',
'selected' => $wp_the_query->queried_object_id,
'hierarchical' => true,
'depth' => 3,
'show_count' => false,
'hide_empty' => false,
) );
}
add_filter( 'restrict_manage_posts', 'restrict_attachments_by_category' );
/**
* Filter the query vars when restricting attachments by category
*
* @access public
* @param mixed $query
* @return null
*/
function hm_convert_attachment_id_to_taxonomy_term_in_query( $query ) {
global $pagenow, $wpdb;
if ( $pagenow != 'upload.php' || empty( $_GET['hm_attachment_category'] ) )
return $query;
$query->query_vars['taxonomy'] = 'hm_attachment_category';
$query->query_vars['term'] = get_term( $query->query_vars['hm_attachment_category'], 'hm_attachment_category' )->slug;
$query->query['taxonomy'] = 'hm_attachment_category';
$query->query['term'] = get_term( $query->query_vars['hm_attachment_category'], 'hm_attachment_category' )->slug;
unset( $query->query['hm_attachment_category'] );
unset( $query->query_vars['hm_attachment_category'] );
return $query;
}
add_filter( 'parse_query', 'hm_convert_attachment_id_to_taxonomy_term_in_query' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment