wppaste
WordPress

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

Since
2.3.0, 4.2.0, 4.4.0, 4.4.0, 4.5.0, 4.7.0, 5.1.0, 5.4.0, 5.5.0, 5.9.0
Source
wp-includes/taxonomy.php:518
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.

Compatibility

WordPress
since 5.9.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$taxonomystring
Taxonomy key. Must not exceed 32 characters and may only contain lowercase alphanumeric characters, dashes, and underscores. See sanitize_key().
$object_typearray|string
Object type or array of object types with which the taxonomy should be associated.
$argsarray|stringoptional
Array or query string of arguments for registering a taxonomy.Default: array()
  • $labelsstring[]default: empty array

    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().
  • $descriptionstringdefault: empty

    A short descriptive summary of what the taxonomy is for.
  • $publicbool

    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.
  • $publicly_queryablebooldefault: false

    Whether the taxonomy is publicly queryable. If not set, the default is inherited from $public @type bool $hierarchical Whether the taxonomy is hierarchical.
  • $show_uibool

    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).
  • $show_in_menubool

    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. $show_ui must be true. If not set, default is inherited from $show_ui (default true).
  • $show_in_nav_menusbool

    Makes this taxonomy available for selection in navigation menus. If not set, the default is inherited from $public (default true).
  • $show_in_restbool

    Whether to include the taxonomy in the REST API. Set this to true for the taxonomy to be available in the block editor.
  • $rest_basestringdefault: is $taxonomy

    To change the base url of REST API route.
  • $rest_namespacestringdefault: is wp/v2

    To change the namespace URL of REST API route.
  • $rest_controller_classstringdefault: is 'WP_REST_Terms_Controller'

    REST API Controller class name.
  • $show_tagcloudbool

    Whether to list the taxonomy in the Tag Cloud Widget controls. If not set, the default is inherited from $show_ui (default true).
  • $show_in_quick_editbool

    Whether to show the taxonomy in the quick/bulk edit panel. It not set, the default is inherited from $show_ui (default true).
  • $show_admin_columnbooldefault: false

    Whether to display a column for the taxonomy on its post type listing screens.
  • $meta_box_cbbool|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.
  • $meta_box_sanitize_cbcallable

    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.
  • $capabilitiesstring[]

    { Array of capabilities for this taxonomy.
  • $manage_termsstringdefault: 'manage_categories'

  • $edit_termsstringdefault: 'manage_categories'

  • $delete_termsstringdefault: 'manage_categories'

  • $assign_termsstringdefault: $taxonomy key

    Default 'edit_posts'. } @type bool|array $rewrite { 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: @type string $slug Customize the permastruct slug.
  • $with_frontbooldefault: true

    Should the permastruct be prepended with WP_Rewrite::$front.
  • $hierarchicalbooldefault: false

    Either hierarchical rewrite tag or not.
  • $ep_maskint

    Assign an endpoint mask. Default EP_NONE. } @type string|bool $query_var 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.
  • $update_count_callbackcallable

    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.
  • $default_termstring|arraydefault: term to be used for the taxonomy

    {
  • $namestring

    Name of default term.
  • $slugstringdefault: empty

    Slug for default term.
  • $descriptionstringdefault: null which equates to false

    Description for default term. Default empty. } @type bool $sort Whether terms in this taxonomy should be sorted in the order they are provided to wp_set_object_terms().
  • $argsarray

    Array of arguments to automatically use inside wp_get_object_terms() for this taxonomy.
  • $_builtinbooldefault: false

    This taxonomy is a "built-in" taxonomy. INTERNAL USE ONLY!

Return value

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

Performance profile

How much work a call to register_taxonomy() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Heavy

Reaches the database via get_terms().

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
28–86

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 111.

Plugin surface
2 hooks

Third-party callbacks on 'registered_taxonomy', 'registered_taxonomy_{$taxonomy}' run inside this call, and their cost is not bounded by anything here.

Called by
1

1 place in core call this, so the cost is paid more often than your own code shows.

What it touches

  • optionoption read or writeupdate_option()called directly
  • hookthird-party callbacksdo_action()called directly
  • querycontent queryget_terms()one call below register_taxonomy()
  • serializeserialisationmaybe_serialize()one call below register_taxonomy()
  • cacheobject cachewp_cache_get()one call below register_taxonomy()

Further down the call graph this can also reach transient. That is the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 5 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost register_taxonomy() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always28–32wp_parse_args(), __(), _doing_it_wrong(), __()
!empty($taxonomy) && empty($taxonomy_object)45–46wp_parse_args(), ->add_rewrite_rules(), ->add_hooks(), do_action(), do_action()
!empty($taxonomy) && !empty($taxonomy_object)63–64wp_parse_args(), ->add_rewrite_rules(), ->add_hooks(), term_exists(), update_option(), do_action(), do_action()
!empty($taxonomy) && !empty($taxonomy_object) && is_wp_error()77–78wp_parse_args(), ->add_rewrite_rules(), ->add_hooks(), term_exists(), sanitize_title(), slug(), is_wp_error(), do_action(), do_action()
!empty($taxonomy) && !empty($taxonomy_object) && !is_wp_error()85–86wp_parse_args(), ->add_rewrite_rules(), ->add_hooks(), term_exists(), sanitize_title(), slug(), is_wp_error(), update_option(), do_action(), do_action()

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 111 instructions, 28–86 executed per call, 6 branches. The work does not change between versions.

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 2

2 hooks fire while register_taxonomy() runs, in this order:

  1. do_action( registered_taxonomy )actionline 570 (+52 into the body)

    Fires after a taxonomy is registered.

  2. do_action( registered_taxonomy_{$taxonomy} )actionline 588 (+70 into the body)

    Fires after a specific taxonomy is registered.

Uses · 11

Used by · 1

Source code

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.' ), '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(); 	// Add default term.	if ( ! empty( $taxonomy_object->default_term ) ) {		$term = term_exists( $taxonomy_object->default_term['name'], $taxonomy );		if ( $term ) {			update_option( 'default_term_' . $taxonomy_object->name, $term['term_id'] );		} else {			$term = wp_insert_term(				$taxonomy_object->default_term['name'],				$taxonomy,				array(					'slug'        => sanitize_title( $taxonomy_object->default_term['slug'] ),					'description' => $taxonomy_object->default_term['description'],				)			); 			// Update `term_id` in options.			if ( ! is_wp_error( $term ) ) {				update_option( 'default_term_' . $taxonomy_object->name, $term['term_id'] );			}		}	} 	/**	 * Fires after a taxonomy is registered.	 *	 * @since 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 ); 	/**	 * Fires after a specific taxonomy is registered.	 *	 * The dynamic portion of the filter name, `$taxonomy`, refers to the taxonomy key.	 *	 * Possible hook names include:	 *	 *  - `registered_taxonomy_category`	 *  - `registered_taxonomy_post_tag`	 *	 * @since 6.0.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}", $taxonomy, $object_type, (array) $taxonomy_object ); 	return $taxonomy_object;}

Changelog

Introduced in 5.9.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

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

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/taxonomy.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.