WP_Taxonomy

Core class used for interacting with taxonomies.

Defined (1)

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

/wp-includes/class-wp-taxonomy.php  
  1. final class WP_Taxonomy { 
  2. /** 
  3. * Taxonomy key. 
  4. * @since 4.7.0 
  5. * @access public 
  6. * @var string 
  7. */ 
  8. public $name; 
  9.  
  10. /** 
  11. * Name of the taxonomy shown in the menu. Usually plural. 
  12. * @since 4.7.0 
  13. * @access public 
  14. * @var string 
  15. */ 
  16. public $label; 
  17.  
  18. /** 
  19. * An array of labels for this taxonomy. 
  20. * @since 4.7.0 
  21. * @access public 
  22. * @var object 
  23. */ 
  24. public $labels = array(); 
  25.  
  26. /** 
  27. * A short descriptive summary of what the taxonomy is for. 
  28. * @since 4.7.0 
  29. * @access public 
  30. * @var string 
  31. */ 
  32. public $description = ''; 
  33.  
  34. /** 
  35. * Whether a taxonomy is intended for use publicly either via the admin interface or by front-end users. 
  36. * @since 4.7.0 
  37. * @access public 
  38. * @var bool 
  39. */ 
  40. public $public = true; 
  41.  
  42. /** 
  43. * Whether the taxonomy is publicly queryable. 
  44. * @since 4.7.0 
  45. * @access public 
  46. * @var bool 
  47. */ 
  48. public $publicly_queryable = true; 
  49.  
  50. /** 
  51. * Whether the taxonomy is hierarchical. 
  52. * @since 4.7.0 
  53. * @access public 
  54. * @var bool 
  55. */ 
  56. public $hierarchical = false; 
  57.  
  58. /** 
  59. * Whether to generate and allow a UI for managing terms in this taxonomy in the admin. 
  60. * @since 4.7.0 
  61. * @access public 
  62. * @var bool 
  63. */ 
  64. public $show_ui = true; 
  65.  
  66. /** 
  67. * Whether to show the taxonomy in the admin menu. 
  68. * If true, the taxonomy is shown as a submenu of the object type menu. If false, no menu is shown. 
  69. * @since 4.7.0 
  70. * @access public 
  71. * @var bool 
  72. */ 
  73. public $show_in_menu = true; 
  74.  
  75. /** 
  76. * Whether the taxonomy is available for selection in navigation menus. 
  77. * @since 4.7.0 
  78. * @access public 
  79. * @var bool 
  80. */ 
  81. public $show_in_nav_menus = true; 
  82.  
  83. /** 
  84. * Whether to list the taxonomy in the tag cloud widget controls. 
  85. * @since 4.7.0 
  86. * @access public 
  87. * @var bool 
  88. */ 
  89. public $show_tagcloud = true; 
  90.  
  91. /** 
  92. * Whether to show the taxonomy in the quick/bulk edit panel. 
  93. * @since 4.7.0 
  94. * @access public 
  95. * @var bool 
  96. */ 
  97. public $show_in_quick_edit = true; 
  98.  
  99. /** 
  100. * Whether to display a column for the taxonomy on its post type listing screens. 
  101. * @since 4.7.0 
  102. * @access public 
  103. * @var bool 
  104. */ 
  105. public $show_admin_column = false; 
  106.  
  107. /** 
  108. * The callback function for the meta box display. 
  109. * @since 4.7.0 
  110. * @access public 
  111. * @var bool|callable 
  112. */ 
  113. public $meta_box_cb = null; 
  114.  
  115. /** 
  116. * An array of object types this taxonomy is registered for. 
  117. * @since 4.7.0 
  118. * @access public 
  119. * @var array 
  120. */ 
  121. public $object_type = null; 
  122.  
  123. /** 
  124. * Capabilities for this taxonomy. 
  125. * @since 4.7.0 
  126. * @access public 
  127. * @var array 
  128. */ 
  129. public $cap; 
  130.  
  131. /** 
  132. * Rewrites information for this taxonomy. 
  133. * @since 4.7.0 
  134. * @access public 
  135. * @var array|false 
  136. */ 
  137. public $rewrite; 
  138.  
  139. /** 
  140. * Query var string for this taxonomy. 
  141. * @since 4.7.0 
  142. * @access public 
  143. * @var string|false 
  144. */ 
  145. public $query_var; 
  146.  
  147. /** 
  148. * Function that will be called when the count is updated. 
  149. * @since 4.7.0 
  150. * @access public 
  151. * @var callable 
  152. */ 
  153. public $update_count_callback; 
  154.  
  155. /** 
  156. * Whether this taxonomy should appear in the REST API. 
  157. * Default false. If true, standard endpoints will be registered with 
  158. * respect to $rest_base and $rest_controller_class. 
  159. * @since 4.7.4 
  160. * @access public 
  161. * @var bool $show_in_rest 
  162. */ 
  163. public $show_in_rest; 
  164.  
  165. /** 
  166. * The base path for this taxonomy's REST API endpoints. 
  167. * @since 4.7.4 
  168. * @access public 
  169. * @var string|bool $rest_base 
  170. */ 
  171. public $rest_base; 
  172.  
  173. /** 
  174. * The controller for this taxonomy's REST API endpoints. 
  175. * Custom controllers must extend WP_REST_Controller. 
  176. * @since 4.7.4 
  177. * @access public 
  178. * @var string|bool $rest_controller_class 
  179. */ 
  180. public $rest_controller_class; 
  181.  
  182. /** 
  183. * Whether it is a built-in taxonomy. 
  184. * @since 4.7.0 
  185. * @access public 
  186. * @var bool 
  187. */ 
  188. public $_builtin; 
  189.  
  190. /** 
  191. * Constructor. 
  192. * @since 4.7.0 
  193. * @access public 
  194. * @global WP $wp WP instance. 
  195. * @param string $taxonomy Taxonomy key, must not exceed 32 characters. 
  196. * @param array|string $object_type Name of the object type for the taxonomy object. 
  197. * @param array|string $args Optional. Array or query string of arguments for registering a taxonomy. 
  198. * Default empty array. 
  199. */ 
  200. public function __construct( $taxonomy, $object_type, $args = array() ) { 
  201. $this->name = $taxonomy; 
  202.  
  203. $this->set_props( $object_type, $args ); 
  204.  
  205. /** 
  206. * Sets taxonomy properties. 
  207. * @since 4.7.0 
  208. * @access public 
  209. * @param array|string $object_type Name of the object type for the taxonomy object. 
  210. * @param array|string $args Array or query string of arguments for registering a taxonomy. 
  211. */ 
  212. public function set_props( $object_type, $args ) { 
  213. $args = wp_parse_args( $args ); 
  214.  
  215. /** 
  216. * Filters the arguments for registering a taxonomy. 
  217. * @since 4.4.0 
  218. * @param array $args Array of arguments for registering a taxonomy. 
  219. * @param string $taxonomy Taxonomy key. 
  220. * @param array $object_type Array of names of object types for the taxonomy. 
  221. */ 
  222. $args = apply_filters( 'register_taxonomy_args', $args, $this->name, (array) $object_type ); 
  223.  
  224. $defaults = array( 
  225. 'labels' => array(),  
  226. 'description' => '',  
  227. 'public' => true,  
  228. 'publicly_queryable' => null,  
  229. 'hierarchical' => false,  
  230. 'show_ui' => null,  
  231. 'show_in_menu' => null,  
  232. 'show_in_nav_menus' => null,  
  233. 'show_tagcloud' => null,  
  234. 'show_in_quick_edit' => null,  
  235. 'show_admin_column' => false,  
  236. 'meta_box_cb' => null,  
  237. 'capabilities' => array(),  
  238. 'rewrite' => true,  
  239. 'query_var' => $this->name,  
  240. 'update_count_callback' => '',  
  241. 'show_in_rest' => false,  
  242. 'rest_base' => false,  
  243. 'rest_controller_class' => false,  
  244. '_builtin' => false,  
  245. ); 
  246.  
  247. $args = array_merge( $defaults, $args ); 
  248.  
  249. // If not set, default to the setting for public. 
  250. if ( null === $args['publicly_queryable'] ) { 
  251. $args['publicly_queryable'] = $args['public']; 
  252.  
  253. if ( false !== $args['query_var'] && ( is_admin() || false !== $args['publicly_queryable'] ) ) { 
  254. if ( true === $args['query_var'] ) { 
  255. $args['query_var'] = $this->name; 
  256. } else { 
  257. $args['query_var'] = sanitize_title_with_dashes( $args['query_var'] ); 
  258. } else { 
  259. // Force query_var to false for non-public taxonomies. 
  260. $args['query_var'] = false; 
  261.  
  262. if ( false !== $args['rewrite'] && ( is_admin() || '' != get_option( 'permalink_structure' ) ) ) { 
  263. $args['rewrite'] = wp_parse_args( $args['rewrite'], array( 
  264. 'with_front' => true,  
  265. 'hierarchical' => false,  
  266. 'ep_mask' => EP_NONE,  
  267. ) ); 
  268.  
  269. if ( empty( $args['rewrite']['slug'] ) ) { 
  270. $args['rewrite']['slug'] = sanitize_title_with_dashes( $this->name ); 
  271.  
  272. // If not set, default to the setting for public. 
  273. if ( null === $args['show_ui'] ) { 
  274. $args['show_ui'] = $args['public']; 
  275.  
  276. // If not set, default to the setting for show_ui. 
  277. if ( null === $args['show_in_menu'] || ! $args['show_ui'] ) { 
  278. $args['show_in_menu'] = $args['show_ui']; 
  279.  
  280. // If not set, default to the setting for public. 
  281. if ( null === $args['show_in_nav_menus'] ) { 
  282. $args['show_in_nav_menus'] = $args['public']; 
  283.  
  284. // If not set, default to the setting for show_ui. 
  285. if ( null === $args['show_tagcloud'] ) { 
  286. $args['show_tagcloud'] = $args['show_ui']; 
  287.  
  288. // If not set, default to the setting for show_ui. 
  289. if ( null === $args['show_in_quick_edit'] ) { 
  290. $args['show_in_quick_edit'] = $args['show_ui']; 
  291.  
  292. $default_caps = array( 
  293. 'manage_terms' => 'manage_categories',  
  294. 'edit_terms' => 'manage_categories',  
  295. 'delete_terms' => 'manage_categories',  
  296. 'assign_terms' => 'edit_posts',  
  297. ); 
  298.  
  299. $args['cap'] = (object) array_merge( $default_caps, $args['capabilities'] ); 
  300. unset( $args['capabilities'] ); 
  301.  
  302. $args['object_type'] = array_unique( (array) $object_type ); 
  303.  
  304. // If not set, use the default meta box 
  305. if ( null === $args['meta_box_cb'] ) { 
  306. if ( $args['hierarchical'] ) { 
  307. $args['meta_box_cb'] = 'post_categories_meta_box'; 
  308. } else { 
  309. $args['meta_box_cb'] = 'post_tags_meta_box'; 
  310.  
  311. $args['name'] = $this->name; 
  312.  
  313. foreach ( $args as $property_name => $property_value ) { 
  314. $this->$property_name = $property_value; 
  315.  
  316. $this->labels = get_taxonomy_labels( $this ); 
  317. $this->label = $this->labels->name; 
  318.  
  319. /** 
  320. * Adds the necessary rewrite rules for the taxonomy. 
  321. * @since 4.7.0 
  322. * @access public 
  323. * @global WP $wp Current WordPress environment instance. 
  324. */ 
  325. public function add_rewrite_rules() { 
  326. /** @var WP $wp */ 
  327. global $wp; 
  328.  
  329. // Non-publicly queryable taxonomies should not register query vars, except in the admin. 
  330. if ( false !== $this->query_var && $wp ) { 
  331. $wp->add_query_var( $this->query_var ); 
  332.  
  333. if ( false !== $this->rewrite && ( is_admin() || '' != get_option( 'permalink_structure' ) ) ) { 
  334. if ( $this->hierarchical && $this->rewrite['hierarchical'] ) { 
  335. $tag = '(.+?)'; 
  336. } else { 
  337. $tag = '([^/]+)'; 
  338.  
  339. add_rewrite_tag( "%$this->name%", $tag, $this->query_var ? "{$this->query_var}=" : "taxonomy=$this->name&term=" ); 
  340. add_permastruct( $this->name, "{$this->rewrite['slug']}/%$this->name%", $this->rewrite ); 
  341.  
  342. /** 
  343. * Removes any rewrite rules, permastructs, and rules for the taxonomy. 
  344. * @since 4.7.0 
  345. * @access public 
  346. * @global WP $wp Current WordPress environment instance. 
  347. */ 
  348. public function remove_rewrite_rules() { 
  349. /** @var WP $wp */ 
  350. global $wp; 
  351.  
  352. // Remove query var. 
  353. if ( false !== $this->query_var ) { 
  354. $wp->remove_query_var( $this->query_var ); 
  355.  
  356. // Remove rewrite tags and permastructs. 
  357. if ( false !== $this->rewrite ) { 
  358. remove_rewrite_tag( "%$this->name%" ); 
  359. remove_permastruct( $this->name ); 
  360.  
  361. /** 
  362. * Registers the ajax callback for the meta box. 
  363. * @since 4.7.0 
  364. * @access public 
  365. */ 
  366. public function add_hooks() { 
  367. add_filter( 'wp_ajax_add-' . $this->name, '_wp_ajax_add_hierarchical_term' ); 
  368.  
  369. /** 
  370. * Removes the ajax callback for the meta box. 
  371. * @since 4.7.0 
  372. * @access public 
  373. */ 
  374. public function remove_hooks() { 
  375. remove_filter( 'wp_ajax_add-' . $this->name, '_wp_ajax_add_hierarchical_term' );