Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 24th, 2012  |  syntax: None  |  size: 3.30 KB  |  hits: 9  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php
  2. /**
  3.  * WordPress Chosen Taxonomy Metabox
  4.  * Author: Helen Hou-Sandi
  5.  *
  6.  * Use Chosen for a replacement taxonomy metabox in WordPress
  7.  * Useful for taxonomies that aren't changed much on the fly,
  8.  * as Chosen is for selection only.
  9.  * You can always use the taxonomy admin screen to add/edit taxonomy terms.
  10.  *
  11.  * Example screenshot: http://cl.ly/2T2D232x172G353i2V2C
  12.  *
  13.  * Chosen: http://harvesthq.github.com/chosen/
  14.  */
  15.  
  16. add_action( 'admin_init', 'hhs_add_meta_boxes', 1 );
  17. function hhs_add_meta_boxes() {
  18.         add_meta_box( 'chosen-tax', 'Choose Terms', 'hhs_chosen_tax_meta_box_display', 'post', 'side', 'default' );
  19. }
  20.  
  21. function hhs_chosen_tax_meta_box_display() {
  22.         global $post;
  23.        
  24.         wp_nonce_field( 'hhs_chosen_tax_meta_box_nonce', 'hhs_chosen_tax_meta_box_nonce' );
  25.         ?>
  26.         <script type="text/javascript">
  27.         jQuery(document).ready(function($){
  28.                 $( '.chzn-select' ).chosen();
  29.         });
  30.         </script>
  31.         <?php
  32.         // which taxonomies should be used - can be multiple
  33.         $taxes = array(
  34.                 'post_tag' => 'Tags',
  35.         );
  36.        
  37.         foreach ( $taxes as $tax => $label ) {
  38.                 // add more args if you want (e.g. orderby)
  39.                 $terms = get_terms( $tax, array( 'hide_empty' => 0 ) );
  40.                 $current_terms = wp_get_post_terms ( $post->ID, $tax, array('fields' => 'ids') );
  41.                 ?>
  42.                 <p><label for="<?php echo $tax; ?>"><?php echo $label; ?></label>:</p>
  43.                 <p><select name="<?php echo $tax; ?>[]" class="chzn-select widefat" data-placeholder="Select one or more" multiple="multiple">
  44.                 <?php foreach ( $terms as $term ) { ?>
  45.                         <option value="<?php echo $term->slug; ?>"<?php selected( in_array( $term->term_id, $current_terms ) ); ?>><?php echo $term->name; ?></option>
  46.                 <?php } ?>
  47.                 </select>
  48.                 </p>
  49.                 <?php
  50.         }
  51. }
  52. add_action( 'save_post', 'hhs_chosen_tax_meta_box_display_save' );
  53. function hhs_chosen_tax_meta_box_display_save( $post_id ) {
  54.  
  55.         // verify nonce
  56.         if ( ! isset( $_POST['hhs_chosen_tax_meta_box_nonce'] ) ||
  57.              ! wp_verify_nonce( $_POST['hhs_chosen_tax_meta_box_nonce'], 'hhs_chosen_tax_meta_box_nonce' ) )
  58.                 return;
  59.        
  60.         // check autosave - maybe overkill?
  61.         if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
  62.                 return;
  63.        
  64.         // check permissions
  65.         if ( ! current_user_can( 'edit_post', $post_id ) )
  66.                 return;
  67.        
  68.         // change to match above, or be a good coder and don't dupe this
  69.         $taxes = array (
  70.                 'post_tag',
  71.         );
  72.        
  73.         foreach ( $taxes as $tax ) {
  74.                 if ( isset( $_POST[$tax] ) && is_array( $_POST[$tax] ) )
  75.                         wp_set_post_terms( $post_id, $_POST[$tax], $tax, false );
  76.         }
  77. }
  78.  
  79. // Chosen JS and CSS enqueue - assumes you are doing this in a theme
  80. // with the JS, CSS, and sprite files in themefolder/js/chosen/
  81. // You'd want to use plugins_url() instead if using this in a plugin
  82. add_action( 'admin_enqueue_scripts', 'hhs_add_admin_scripts', 10, 1 );
  83. function hhs_add_admin_scripts( $hook ) {
  84.         global $post;
  85.        
  86.         // There's probably a better way to check the screen...
  87.         if ( $hook == 'post-new.php' || $hook == 'post.php' ) {
  88.                 if ( 'post' === $post->post_type ) {    
  89.                         wp_enqueue_script(  'chosen', get_template_directory_uri().'/js/chosen/chosen.jquery.min.js', array( 'jquery' ), '1.0' );
  90.                         wp_enqueue_style( 'chosen', get_template_directory_uri().'/js/chosen/chosen.css' );
  91.                 }
  92.         }
  93. }
  94.  
  95. // Hide unused taxonomy metabox(es)
  96. add_action( 'do_meta_boxes', 'hhs_remove_tax_meta_boxes', 10, 3 );
  97. function hhs_remove_tax_meta_boxes( $post_type, $priority, $post ) {
  98.         remove_meta_box( 'tagsdiv-post_tag', 'post', 'side' );
  99. }
  100. ?>