Created
March 9, 2012 17:29
-
-
Save ryelle/2007661 to your computer and use it in GitHub Desktop.
Chosen.js for CMB Taxonomy field
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 | |
| /** | |
| * Plugin Name: KD Example! | |
| * Version: v0.1 | |
| * Author: Kelly Dwan | |
| * Author URI: http://redradar.net/ | |
| * Description: Using Chosen + Custom Meta Boxes | |
| */ | |
| class KD_Example_Class { | |
| protected $prefix = 'kd_example_'; | |
| protected $post_type = 'post'; | |
| protected $taxonomy = 'category'; | |
| public function __construct() { | |
| add_action( 'admin_enqueue_scripts', array( $this, 'includes'), 10, 1 ); | |
| add_action( 'init', array( $this, 'initialize_cmb_meta_boxes' ), 9999 ); | |
| add_filter( 'cmb_render_chosen_taxonomy', array( $this, 'render_chosen_taxonomy' ), 10, 2 ); | |
| add_filter( 'cmb_validate_chosen_taxonomy', array( $this, 'validate_chosen_taxonomy' ), 10, 3 ); | |
| add_filter( 'cmb_meta_boxes', array( $this, 'add_meta_boxes' ) ); | |
| add_action( 'do_meta_boxes', array($this, 'remove_meta_boxes'), 10, 3 ); | |
| } | |
| /** | |
| * Add chosen to the edit screen for our new Item CPT | |
| */ | |
| public function includes($hook){ | |
| global $post; | |
| if ( $hook == 'post-new.php' || $hook == 'post.php' ) { | |
| if ( $this->post_type === $post->post_type ) { | |
| wp_enqueue_script( 'chosen', plugins_url('libs/chosen/chosen.jquery.min.js',__FILE__), array( 'jquery' ), '1.0' ); | |
| wp_enqueue_style( 'chosen', plugins_url('libs/chosen/chosen.css',__FILE__) ); | |
| } | |
| } | |
| } | |
| // Initialize the metabox class | |
| function initialize_cmb_meta_boxes() { | |
| if ( !class_exists( 'cmb_Meta_Box' ) ) { | |
| require_once( 'libs/cmb-library/init.php' ); | |
| } | |
| } | |
| function render_chosen_taxonomy( $field, $meta ) { | |
| global $post;?> | |
| <script type="text/javascript"> | |
| jQuery(document).ready(function($){ | |
| $( '.chzn-select' ).chosen(); | |
| }); | |
| </script><?php | |
| echo '<select name="', $field['id'], '[]" id="', $field['id'], '" class="chzn-select widefat" multiple>'; | |
| $current_terms = wp_get_post_terms ( $post->ID, $field['taxonomy'], array('fields' => 'ids') ); | |
| $terms = get_terms( $field['taxonomy'], 'hide_empty=0' ); | |
| foreach ( $terms as $term ) { | |
| ?> | |
| <option value="<?php echo $term->slug; ?>"<?php selected( in_array( $term->term_id, $current_terms ) ); ?>><?php echo $term->name; ?></option> | |
| <?php | |
| } | |
| echo '</select>'; | |
| echo '<p class="cmb_metabox_description">', $field['desc'], '</p>'; | |
| } | |
| function validate_chosen_taxonomy( $new, $post_id, $field ) { | |
| return wp_set_object_terms( $post_id, $new, $field['taxonomy'] ); | |
| } | |
| function add_meta_boxes( $meta_boxes ) { | |
| $meta_boxes[] = array( | |
| 'id' => $this->prefix.'item_metabox', | |
| 'title' => 'Information', | |
| 'pages' => array($this->post_type), | |
| 'context' => 'normal', | |
| 'priority' => 'high', | |
| 'show_names' => true, // Show field names on the left | |
| 'fields' => array( | |
| array( | |
| 'name' => 'Categories', | |
| 'id' => 'chosen_'.$this->taxonomy, | |
| 'type' => 'chosen_taxonomy', | |
| 'taxonomy' => $this->taxonomy | |
| ), | |
| ) | |
| ); | |
| return $meta_boxes; | |
| } | |
| /** | |
| * Since we're adding the Menu taxonomy in the CMB 'Info' box, we should hide the default tax box | |
| * I also generally hide the custom fields box when using CMB, because I don't want extra fields created. | |
| */ | |
| function remove_meta_boxes( $post_type, $priority, $post ) { | |
| remove_meta_box( $this->taxonomy.'div', $this->post_type, 'side' ); // remove tax box | |
| remove_meta_box( 'postcustom', $this->post_type, 'normal'); // remove custom fields | |
| } | |
| } | |
| new KD_Example_Class; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment