-
-
Save subharanjanm/4246611 to your computer and use it in GitHub Desktop.
Use Chosen for a replacement WordPress taxonomy metabox
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* WordPress Chosen Taxonomy Metabox | |
* Author: Helen Hou-Sandi | |
* | |
* Use Chosen for a replacement taxonomy metabox in WordPress | |
* Useful for taxonomies that aren't changed much on the fly and are | |
* non-hierarchical in nature, as Chosen is for flat selection only. | |
* You can always use the taxonomy admin screen to add/edit taxonomy terms. | |
* Categories need slightly different treatment from the rest in order to | |
* leverage the correct form field name for saving without interference. | |
* | |
* Example screenshot: http://cl.ly/2T2D232x172G353i2V2C | |
* | |
* Chosen: http://harvesthq.github.com/chosen/ | |
*/ | |
add_action( 'add_meta_boxes', 'hhs_add_meta_boxes' ); | |
function hhs_add_meta_boxes() { | |
remove_meta_box( 'tagsdiv-post_tag', 'post', 'side' ); | |
remove_meta_box( 'categorydiv', 'post', 'side' ); | |
remove_meta_box( 'customtaxdiv', 'post', 'side' ); | |
add_meta_box( 'chosen-tax', 'Choose Terms', 'hhs_chosen_tax_meta_box_display', 'post', 'side', 'default' ); | |
} | |
function hhs_chosen_tax_meta_box_display() { | |
global $post; | |
?> | |
<script type="text/javascript"> | |
jQuery(document).ready(function($){ | |
$( '.chzn-select' ).chosen(); | |
}); | |
</script> | |
<?php | |
// which taxonomies should be used - can be multiple | |
$taxes = array( | |
'post_tag' => 'Tags', | |
'category' => 'Categories', | |
'customtax' => 'Custom Taxonomy', | |
); | |
foreach ( $taxes as $tax => $label ) { | |
/** | |
* You may want to check if the current user can assign terms to | |
* the taxonomy in question before going any further. | |
* It will not save their assignments anyway, but it's a bit of a | |
* funny user experience. | |
*/ | |
// add more args if you want (e.g. orderby) | |
$terms = get_terms( $tax, array( 'hide_empty' => 0 ) ); | |
$current_terms = wp_get_post_terms ( $post->ID, $tax, array('fields' => 'ids') ); | |
if ( 'category' == $tax ) | |
$name = 'post_category'; | |
else | |
$name = "tax_input[$tax]"; | |
?> | |
<p><label for="<?php echo $tax; ?>"><?php echo $label; ?></label>:</p> | |
<p><select name="<?php echo $name; ?>[]" class="chzn-select widefat" data-placeholder="Select one or more" multiple="multiple"> | |
<?php foreach ( $terms as $term ) { ?> | |
<option value="<?php echo $term->term_id; ?>"<?php selected( in_array( $term->term_id, $current_terms ) ); ?>><?php echo $term->name; ?></option> | |
<?php } ?> | |
</select> | |
</p> | |
<?php | |
} | |
} | |
// Chosen JS and CSS enqueue - assumes you are doing this in a theme | |
// with the JS, CSS, and sprite files in themefolder/js/chosen/ | |
// You'd want to use plugins_url() instead if using this in a plugin | |
add_action( 'admin_enqueue_scripts', 'hhs_add_admin_scripts', 10, 1 ); | |
function hhs_add_admin_scripts( $hook ) { | |
global $post; | |
if ( $hook == 'post-new.php' || $hook == 'post.php' ) { | |
if ( 'post' === $post->post_type ) { | |
wp_enqueue_script( 'chosen', get_template_directory_uri().'/js/chosen/chosen.jquery.min.js', array( 'jquery' ), '1.0' ); | |
wp_enqueue_style( 'chosen', get_template_directory_uri().'/js/chosen/chosen.css' ); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment