post_categories_meta_box

Display post categories form fields.

Description

post_categories_meta_box( (WP_Post) $post, (array) $box ); 

Parameters (2)

0. $post (WP_Post)
Post object.
1. $box (array)
Categories meta box arguments.

Options

  • id (string) => ''

    Meta box id attribute.

  • title (string) => ''

    Meta box title.

  • args (array) => array()

    Extra meta box arguments.

    • taxonomy (string) => 'category'

      Taxonomy.

array(

    /**
     * Meta box 'id' attribute.
     *
     * @type string
     * @default ''
     */
    'id' => '',

    /**
     * Meta box title.
     *
     * @type string
     * @default ''
     */
    'title' => '',

    /**
     * Extra meta box arguments.
     *
     * @type array
     */
    'args' => array(

        /**
         * Taxonomy.
         *
         * @type string
         * @default 'category'
         */
        'taxonomy' => 'category'
    )
);        


Usage

  1. if ( !function_exists( 'post_categories_meta_box' ) ) { 
  2. require_once ABSPATH . '/wp-admin/includes/meta-boxes.php'; 
  3.  
  4. // Post object. 
  5. $post = null; 
  6.  
  7. // Categories meta box arguments. 
  8. $box = array( 
  9. 'id' => '', 
  10. 'title' => '', 
  11. 'args' => array( 
  12. 'taxonomy' => 'category' 
  13. ); 
  14.  
  15. // NOTICE! Understand what this does before running. 
  16. $result = post_categories_meta_box($post, $box); 
  17.  

Defined (1)

The function is defined in the following location(s).

/wp-admin/includes/meta-boxes.php  
  1. function post_categories_meta_box( $post, $box ) { 
  2. $defaults = array( 'taxonomy' => 'category' ); 
  3. if ( ! isset( $box['args'] ) || ! is_array( $box['args'] ) ) { 
  4. $args = array(); 
  5. } else { 
  6. $args = $box['args']; 
  7. $r = wp_parse_args( $args, $defaults ); 
  8. $tax_name = esc_attr( $r['taxonomy'] ); 
  9. $taxonomy = get_taxonomy( $r['taxonomy'] ); 
  10. ?> 
  11. <div id="taxonomy-<?php echo $tax_name; ?>" class="categorydiv"> 
  12. <ul id="<?php echo $tax_name; ?>-tabs" class="category-tabs"> 
  13. <li class="tabs"><a href="#<?php echo $tax_name; ?>-all"><?php echo $taxonomy->labels->all_items; ?></a></li> 
  14. <li class="hide-if-no-js"><a href="#<?php echo $tax_name; ?>-pop"><?php _e( 'Most Used' ); ?></a></li> 
  15. </ul> 
  16.  
  17. <div id="<?php echo $tax_name; ?>-pop" class="tabs-panel" style="display: none;"> 
  18. <ul id="<?php echo $tax_name; ?>checklist-pop" class="categorychecklist form-no-clear" > 
  19. <?php $popular_ids = wp_popular_terms_checklist( $tax_name ); ?> 
  20. </ul> 
  21. </div> 
  22.  
  23. <div id="<?php echo $tax_name; ?>-all" class="tabs-panel"> 
  24. <?php 
  25. $name = ( $tax_name == 'category' ) ? 'post_category' : 'tax_input[' . $tax_name . ']'; 
  26. echo "<input type='hidden' name='{$name}[]' value='0' />"; // Allows for an empty term set to be sent. 0 is an invalid Term ID and will be ignored by empty() checks. 
  27. ?> 
  28. <ul id="<?php echo $tax_name; ?>checklist" data-wp-lists="list:<?php echo $tax_name; ?>" class="categorychecklist form-no-clear"> 
  29. <?php wp_terms_checklist( $post->ID, array( 'taxonomy' => $tax_name, 'popular_cats' => $popular_ids ) ); ?> 
  30. </ul> 
  31. </div> 
  32. <?php if ( current_user_can( $taxonomy->cap->edit_terms ) ) : ?> 
  33. <div id="<?php echo $tax_name; ?>-adder" class="wp-hidden-children"> 
  34. <a id="<?php echo $tax_name; ?>-add-toggle" href="#<?php echo $tax_name; ?>-add" class="hide-if-no-js taxonomy-add-new"> 
  35. <?php 
  36. /** translators: %s: add new taxonomy label */ 
  37. printf( __( '+ %s' ), $taxonomy->labels->add_new_item ); 
  38. ?> 
  39. </a> 
  40. <p id="<?php echo $tax_name; ?>-add" class="category-add wp-hidden-child"> 
  41. <label class="screen-reader-text" for="new<?php echo $tax_name; ?>"><?php echo $taxonomy->labels->add_new_item; ?></label> 
  42. <input type="text" name="new<?php echo $tax_name; ?>" id="new<?php echo $tax_name; ?>" class="form-required form-input-tip" value="<?php echo esc_attr( $taxonomy->labels->new_item_name ); ?>" aria-required="true"/> 
  43. <label class="screen-reader-text" for="new<?php echo $tax_name; ?>_parent"> 
  44. <?php echo $taxonomy->labels->parent_item_colon; ?> 
  45. </label> 
  46. <?php 
  47. $parent_dropdown_args = array( 
  48. 'taxonomy' => $tax_name,  
  49. 'hide_empty' => 0,  
  50. 'name' => 'new' . $tax_name . '_parent',  
  51. 'orderby' => 'name',  
  52. 'hierarchical' => 1,  
  53. 'show_option_none' => '— ' . $taxonomy->labels->parent_item . ' —',  
  54. ); 
  55.  
  56. /** 
  57. * Filters the arguments for the taxonomy parent dropdown on the Post Edit page. 
  58. * @since 4.4.0 
  59. * @param array $parent_dropdown_args { 
  60. * Optional. Array of arguments to generate parent dropdown. 
  61. * @type string $taxonomy Name of the taxonomy to retrieve. 
  62. * @type bool $hide_if_empty True to skip generating markup if no 
  63. * categories are found. Default 0. 
  64. * @type string $name Value for the 'name' attribute 
  65. * of the select element. 
  66. * Default "new{$tax_name}_parent". 
  67. * @type string $orderby Which column to use for ordering 
  68. * terms. Default 'name'. 
  69. * @type bool|int $hierarchical Whether to traverse the taxonomy 
  70. * hierarchy. Default 1. 
  71. * @type string $show_option_none Text to display for the "none" option. 
  72. * Default "— {$parent} —",  
  73. * where `$parent` is 'parent_item' 
  74. * taxonomy label. 
  75. * } 
  76. */ 
  77. $parent_dropdown_args = apply_filters( 'post_edit_category_parent_dropdown_args', $parent_dropdown_args ); 
  78.  
  79. wp_dropdown_categories( $parent_dropdown_args ); 
  80. ?> 
  81. <input type="button" id="<?php echo $tax_name; ?>-add-submit" data-wp-lists="add:<?php echo $tax_name; ?>checklist:<?php echo $tax_name; ?>-add" class="button category-add-submit" value="<?php echo esc_attr( $taxonomy->labels->add_new_item ); ?>" /> 
  82. <?php wp_nonce_field( 'add-' . $tax_name, '_ajax_nonce-add-' . $tax_name, false ); ?> 
  83. <span id="<?php echo $tax_name; ?>-ajax-response"></span> 
  84. </p> 
  85. </div> 
  86. <?php endif; ?> 
  87. </div> 
  88. <?php