register_taxonomy( string $taxonomy, array|string $object_type, array|string $args = array() )

Creates or modifies a taxonomy object.


Description

Note: Do not use before the ‘init’ hook.

A simple function for creating or modifying a taxonomy object based on the parameters given. If modifying an existing taxonomy object, note that the $object_type value from the original registration will be overwritten.


Parameters

$taxonomy

(Required) Taxonomy key. Must not exceed 32 characters and may only contain lowercase alphanumeric characters, dashes, and underscores. See sanitize_key().

$object_type

(Required) Object type or array of object types with which the taxonomy should be associated.

$args

(Optional) Array or query string of arguments for registering a taxonomy.<br>

  • 'labels'
    (string[]) An array of labels for this taxonomy. By default, Tag labels are used for non-hierarchical taxonomies, and Category labels are used for hierarchical taxonomies. See accepted values in get_taxonomy_labels(). <br>
  • 'description'
    (string) A short descriptive summary of what the taxonomy is for. <br>
  • 'public'
    (bool) Whether a taxonomy is intended for use publicly either via the admin interface or by front-end users. The default settings of $publicly_queryable, $show_ui, and $show_in_nav_menus are inherited from $public.<br>
  • 'publicly_queryable'
    (bool) Whether the taxonomy is publicly queryable.<br> If not set, the default is inherited from $public
  • 'hierarchical'
    (bool) Whether the taxonomy is hierarchical. Default false.<br>
  • 'show_ui'
    (bool) Whether to generate and allow a UI for managing terms in this taxonomy in the admin. If not set, the default is inherited from $public (default true).<br>
  • 'show_in_menu'
    (bool) Whether to show the taxonomy in the admin menu. If true, the taxonomy is shown as a submenu of the object type menu. If false, no menu is shown.<br> $show_ui must be true. If not set, default is inherited from $show_ui (default true).<br>
  • 'show_in_nav_menus'
    (bool) Makes this taxonomy available for selection in navigation menus. If not set, the default is inherited from $public (default true).<br>
  • 'show_in_rest'
    (bool) Whether to include the taxonomy in the REST API. Set this to true for the taxonomy to be available in the block editor.<br>
  • 'rest_base'
    (string) To change the base url of REST API route. Default is $taxonomy.<br>
  • 'rest_namespace'
    (string) To change the namespace URL of REST API route. Default is wp/v2.<br>
  • 'rest_controller_class'
    (string) REST API Controller class name. Default is 'WP_REST_Terms_Controller'.<br>
  • 'show_tagcloud'
    (bool) Whether to list the taxonomy in the Tag Cloud Widget controls. If not set, the default is inherited from $show_ui (default true).<br>
  • 'show_in_quick_edit'
    (bool) Whether to show the taxonomy in the quick/bulk edit panel. It not set, the default is inherited from $show_ui (default true).<br>
  • 'show_admin_column'
    (bool) Whether to display a column for the taxonomy on its post type listing screens. Default false.<br>
  • 'meta_box_cb'
    (bool|callable) Provide a callback function for the meta box display. If not set, post_categories_meta_box() is used for hierarchical taxonomies, and post_tags_meta_box() is used for non-hierarchical. If false, no meta box is shown.<br>
  • 'meta_box_sanitize_cb'
    (callable) Callback function for sanitizing taxonomy data saved from a meta box. If no callback is defined, an appropriate one is determined based on the value of $meta_box_cb.<br>
  • 'capabilities'
    (string[]) Array of capabilities for this taxonomy.<br>
    • 'manage_terms'
      (string) Default 'manage_categories'.<br>
    • 'edit_terms'
      (string) Default 'manage_categories'.<br>
    • 'delete_terms'
      (string) Default 'manage_categories'.<br>
    • 'assign_terms'
      (string) Default 'edit_posts'.<br>
  • 'rewrite'
    (bool|array) Triggers the handling of rewrites for this taxonomy. Default true, using $taxonomy as slug. To prevent rewrite, set to false. To specify rewrite rules, an array can be passed with any of these keys:
    • 'slug'
      (string) Customize the permastruct slug. Default $taxonomy key.<br>
    • 'with_front'
      (bool) Should the permastruct be prepended with WP_Rewrite::$front. Default true.<br>
    • 'hierarchical'
      (bool) Either hierarchical rewrite tag or not. Default false.<br>
    • 'ep_mask'
      (int) Assign an endpoint mask. Default EP_NONE.<br>
  • 'query_var'
    (string|bool) Sets the query var key for this taxonomy. Default $taxonomy key. If false, a taxonomy cannot be loaded at ?{query_var}={term_slug}. If a string, the query ?{query_var}={term_slug} will be valid.<br>
  • 'update_count_callback'
    (callable) Works much like a hook, in that it will be called when the count is updated. Default _update_post_term_count() for taxonomies attached to post types, which confirms that the objects are published before counting them. Default _update_generic_term_count() for taxonomies attached to other object types, such as users.<br>
  • 'default_term'
    (string|array) Default term to be used for the taxonomy.<br>
    • 'name'
      (string) Name of default term.<br>
    • 'slug'
      (string) Slug for default term. <br>
    • 'description'
      (string) Description for default term. <br>
  • 'sort'
    (bool) Whether terms in this taxonomy should be sorted in the order they are provided to wp_set_object_terms(). Default null which equates to false.<br>
  • 'args'
    (array) Array of arguments to automatically use inside wp_get_object_terms() for this taxonomy.<br>
  • '_builtin'
    (bool) This taxonomy is a "built-in" taxonomy. INTERNAL USE ONLY! Default false.<br>

Default value: array()


Return

(WP_Taxonomy|WP_Error) The registered taxonomy object on success, WP_Error object on failure.


Source

File: wp-includes/taxonomy.php

function register_taxonomy( $taxonomy, $object_type, $args = array() ) {
	global $wp_taxonomies;

	if ( ! is_array( $wp_taxonomies ) )
		$wp_taxonomies = array();

	$args = wp_parse_args( $args );

	if ( empty( $taxonomy ) || strlen( $taxonomy ) > 32 ) {
		_doing_it_wrong( __FUNCTION__, __( 'Taxonomy names must be between 1 and 32 characters in length.' ), 'WP-4.2.0' );
		return new WP_Error( 'taxonomy_length_invalid', __( 'Taxonomy names must be between 1 and 32 characters in length.' ) );
	}

	$taxonomy_object = new WP_Taxonomy( $taxonomy, $object_type, $args );
	$taxonomy_object->add_rewrite_rules();

	$wp_taxonomies[ $taxonomy ] = $taxonomy_object;

	$taxonomy_object->add_hooks();


	/**
	 * Fires after a taxonomy is registered.
	 *
	 * @since WP-3.3.0
	 *
	 * @param string       $taxonomy    Taxonomy slug.
	 * @param array|string $object_type Object type or array of object types.
	 * @param array        $args        Array of taxonomy registration arguments.
	 */
	do_action( 'registered_taxonomy', $taxonomy, $object_type, (array) $taxonomy_object );
}


Changelog

Changelog
Version Description
5.9.0 Introduced rest_namespace argument.
5.5.0 Introduced default_term argument.
5.4.0 Added the registered taxonomy object as a return value.
5.1.0 Introduced meta_box_sanitize_cb argument.
4.7.0 Introduced show_in_rest, 'rest_base' and 'rest_controller_class' arguments to register the taxonomy in REST API.
4.5.0 Introduced publicly_queryable argument.
4.4.0 The public argument now controls whether the taxonomy can be queried on the front end.
4.2.0 Introduced show_in_quick_edit argument.
2.3.0 Introduced.