Advertisement
manchumahara

Wordpress Custom Taxonomy checklist for any custom post type

Aug 30th, 2011
460
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.80 KB | None | 0 0
  1. //
  2. // Category Checklists
  3. //
  4.  
  5. /**
  6.  * {@internal Missing Short Description}}
  7.  *
  8.  * @since 2.5.1
  9.  */
  10. class Walker_Category_Checklist extends Walker {
  11.     var $tree_type = 'category';
  12.     var $db_fields = array ('parent' => 'parent', 'id' => 'term_id'); //TODO: decouple this
  13.  
  14.     function start_lvl(&$output, $depth, $args) {
  15.         $indent = str_repeat("\t", $depth);
  16.         //$output .= "$indent<ul class='children'>\n";
  17.                 $output .= "$indent\n";
  18.     }
  19.  
  20.     function end_lvl(&$output, $depth, $args) {
  21.         $indent = str_repeat("\t", $depth);
  22.         //$output .= "$indent</ul>\n";
  23.                 $output .= "$indent\n";
  24.     }
  25.  
  26.     function start_el(&$output, $category, $depth, $args) {
  27.         extract($args);
  28.         if ( empty($taxonomy) )
  29.             $taxonomy = 'category';
  30.  
  31.         if ( $taxonomy == 'category' )
  32.             $name = 'post_category';
  33.         else
  34.                         $name = $taxonomy;
  35.             //$name = 'tax_input['.$taxonomy.']';
  36.  
  37.         //$class = in_array( $category->term_id, $popular_cats ) ? ' class="popular-category"' : '';
  38.         //$output .= "\n<li id='{$taxonomy}-{$category->term_id}'$class>" . '<label class="selectit"><input value="' . $category->term_id . '" type="checkbox" name="'.$name.'[]" id="in-'.$taxonomy.'-' . $category->term_id . '"' . checked( in_array( $category->term_id, $selected_cats ), true, false ) . disabled( empty( $args['disabled'] ), false, false ) . ' /> ' . esc_html( apply_filters('the_category', $category->name )) . '</label>';
  39.                 //$output .= "\n<li id='{$taxonomy}-{$category->term_id}'$class>" . '<label class="selectit"><input value="' . $category->term_id . '" type="checkbox" name="'.$name.'[]" id="in-'.$taxonomy.'-' . $category->term_id . '"' . checked( in_array( $category->term_id, $selected_cats ), true, false ) . disabled( empty( $args['disabled'] ), false, false ) . ' /> ' . esc_html( apply_filters('the_category', $category->name )) . '</label>';
  40.                 $output .= '<br/><label class="fullsize customcheckboxl" for="'.esc_html( apply_filters('the_category', $category->name )).$category->term_id.'"><input type="checkbox" id="'.esc_html( apply_filters('the_category', $category->name )).$category->term_id.'" class="customcheckbox" name="'.$name.'[]" '.checked( in_array( $category->term_id, $selected_cats ), true, false ).' value="'.$category->term_id.'" /> '.esc_html( apply_filters('the_category', $category->name )).'</label>';
  41.                 //'<label class="fullsize customcheckboxl" for="'.$language_list->cat_name.$language_list->cat_ID.'"><input type="checkbox" id="'.$language_list->cat_name.$language_list->cat_ID.'" class="customcheckbox" name="language[]" '.$check.' value="'.$language_list->cat_ID.'" /> '.$language_list->cat_name.'</label>';
  42.     }
  43.  
  44.     function end_el(&$output, $category, $depth, $args) {
  45.         //$output .= "</li>\n";
  46.                 $output .= "\n";
  47.     }
  48. }
  49.  
  50. /**
  51.  * {@internal Missing Short Description}}
  52.  *
  53.  * @since 2.5.1
  54.  *
  55.  * @param unknown_type $post_id
  56.  * @param unknown_type $descendants_and_self
  57.  * @param unknown_type $selected_cats
  58.  * @param unknown_type $popular_cats
  59.  */
  60. function wp_category_checklist( $post_id = 0, $descendants_and_self = 0, $selected_cats = false, $popular_cats = false, $walker = null, $checked_ontop = true ) {
  61.     wp_terms_checklist($post_id,
  62.         array(
  63.             'taxonomy' => 'category',
  64.             'descendants_and_self' => $descendants_and_self,
  65.             'selected_cats' => $selected_cats,
  66.             'popular_cats' => $popular_cats,
  67.             'walker' => $walker,
  68.             'checked_ontop' => $checked_ontop
  69.   ));
  70. }
  71.  
  72. /**
  73.  * Taxonomy independent version of wp_category_checklist
  74.  *
  75.  * @since 3.0.0
  76.  *
  77.  * @param int $post_id
  78.  * @param array $args
  79.  */
  80. function wp_terms_checklist($post_id = 0, $args = array()) {
  81.     $defaults = array(
  82.         'descendants_and_self' => 0,
  83.         'selected_cats' => false,
  84.         'popular_cats' => false,
  85.         'walker' => null,
  86.         'taxonomy' => 'category',
  87.         'checked_ontop' => false
  88.     );
  89.     extract( wp_parse_args($args, $defaults), EXTR_SKIP );
  90.  
  91.     if ( empty($walker) || !is_a($walker, 'Walker') )
  92.         $walker = new Walker_Category_Checklist;
  93.  
  94.     $descendants_and_self = (int) $descendants_and_self;
  95.  
  96.     $args = array('taxonomy' => $taxonomy);
  97.  
  98.     $tax = get_taxonomy($taxonomy);
  99.     $args['disabled'] = !current_user_can($tax->cap->assign_terms);
  100.  
  101.     if ( is_array( $selected_cats ) )
  102.         $args['selected_cats'] = $selected_cats;
  103.     elseif ( $post_id )
  104.         $args['selected_cats'] = wp_get_object_terms($post_id, $taxonomy, array_merge($args, array('fields' => 'ids')));
  105.     else
  106.         $args['selected_cats'] = array();
  107.  
  108.     if ( is_array( $popular_cats ) )
  109.         $args['popular_cats'] = $popular_cats;
  110.     else
  111.         $args['popular_cats'] = get_terms( $taxonomy, array( 'fields' => 'ids', 'orderby' => 'count', 'order' => 'DESC', 'number' => 10, 'hierarchical' => false ) );
  112.  
  113.     if ( $descendants_and_self ) {
  114.         $categories = (array) get_terms($taxonomy, array( 'child_of' => $descendants_and_self, 'hierarchical' => 0, 'hide_empty' => 0 ) );
  115.         $self = get_term( $descendants_and_self, $taxonomy );
  116.         array_unshift( $categories, $self );
  117.     } else {
  118.         $categories = (array) get_terms($taxonomy, array('get' => 'all'));
  119.     }
  120.  
  121.     if ( $checked_ontop ) {
  122.         // Post process $categories rather than adding an exclude to the get_terms() query to keep the query the same across all posts (for any query cache)
  123.         $checked_categories = array();
  124.         $keys = array_keys( $categories );
  125.  
  126.         foreach( $keys as $k ) {
  127.             if ( in_array( $categories[$k]->term_id, $args['selected_cats'] ) ) {
  128.                 $checked_categories[] = $categories[$k];
  129.                 unset( $categories[$k] );
  130.             }
  131.         }
  132.  
  133.         // Put checked cats on top
  134.         echo call_user_func_array(array(&$walker, 'walk'), array($checked_categories, 0, $args));
  135.     }
  136.     // Then the rest of them
  137.     echo call_user_func_array(array(&$walker, 'walk'), array($categories, 0, $args));
  138. }
  139.  
  140. //usage
  141. //here $jobid is post id
  142. wp_terms_checklist($jobid, array( 'taxonomy' => 'childrenage') );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement