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
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 arrayAn 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: emptyA short descriptive summary of what the taxonomy is for.$publicboolWhether 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_menusare inherited from$public.$publicly_queryablebooldefault: falseWhether the taxonomy is publicly queryable. If not set, the default is inherited from$public@type bool $hierarchical Whether the taxonomy is hierarchical.$show_uiboolWhether 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_menuboolWhether 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_uimust be true. If not set, default is inherited from$show_ui(default true).$show_in_nav_menusboolMakes this taxonomy available for selection in navigation menus. If not set, the default is inherited from$public(default true).$show_in_restboolWhether 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 $taxonomyTo change the base url of REST API route.$rest_namespacestringdefault: is wp/v2To change the namespace URL of REST API route.$rest_controller_classstringdefault: is 'WP_REST_Terms_Controller'REST API Controller class name.$show_tagcloudboolWhether 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_editboolWhether 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: falseWhether to display a column for the taxonomy on its post type listing screens.$meta_box_cbbool|callableProvide 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_cbcallableCallback 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 keyDefault '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: trueShould the permastruct be prepended with WP_Rewrite::$front.$hierarchicalbooldefault: falseEither hierarchical rewrite tag or not.$ep_maskintAssign an endpoint mask. DefaultEP_NONE. } @type string|bool $query_var Sets the query var key for this taxonomy. Default$taxonomykey. 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_callbackcallableWorks 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{$namestringName of default term.$slugstringdefault: emptySlug for default term.$descriptionstringdefault: null which equates to falseDescription for default term. Default empty. } @type bool $sort Whether terms in this taxonomy should be sorted in the order they are provided towp_set_object_terms().$argsarrayArray of arguments to automatically use insidewp_get_object_terms()for this taxonomy.$_builtinbooldefault: falseThis 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
- Scaling
- Constant
- Instructions
- 28–86
- Plugin surface
- 2 hooks
- Called by
- 1
Reaches the database via get_terms().
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 111.
Third-party callbacks on 'registered_taxonomy', 'registered_taxonomy_{$taxonomy}' run inside this call, and their cost is not bounded by anything here.
1 place in core call this, so the cost is paid more often than your own code shows.
What it touches
- optionoption read or write
update_option()called directly - hookthird-party callbacks
do_action()called directly - querycontent query
get_terms()one call below register_taxonomy() - serializeserialisation
maybe_serialize()one call below register_taxonomy() - cacheobject cache
wp_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.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 28–32 | wp_parse_args(), __(), _doing_it_wrong(), __() |
!empty($taxonomy) && empty($taxonomy_object) | 45–46 | wp_parse_args(), ->add_rewrite_rules(), ->add_hooks(), do_action(), do_action() |
!empty($taxonomy) && !empty($taxonomy_object) | 63–64 | wp_parse_args(), ->add_rewrite_rules(), ->add_hooks(), term_exists(), update_option(), do_action(), do_action() |
!empty($taxonomy) && !empty($taxonomy_object) && is_wp_error() | 77–78 | wp_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–86 | wp_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:
- do_action( registered_taxonomy )actionline 570 (+52 into the body)
Fires after a taxonomy is registered.
- do_action( registered_taxonomy_{$taxonomy} )actionline 588 (+70 into the body)
Fires after a specific taxonomy is registered.
Uses · 11
- wp_parse_args()Merges user defined arguments into defaults array.
- _doing_it_wrong()Marks something as being incorrectly called.
- __()Retrieves the translation of $text.
- term_exists()Determines whether a taxonomy term exists.
- update_option()Updates the value of an option that was already added.
- wp_insert_term()Adds a new term to the database.
- sanitize_title()Sanitizes a string into a slug, which can be used in URLs or HTML attributes.
- is_wp_error()Checks whether the given variable is a WordPress Error.
- do_action()Calls the callback functions that have been added to an action hook.
- WP_Error::__construct()Initializes the error.
- WP_Taxonomy::__construct()Constructor.
Used by · 1
- create_initial_taxonomies()Creates the initial taxonomies.
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.
Signature, return type and hooks compared across 5 parsed releases.
rest_namespace argument.from the docblockdefault_term argument.from the docblockmeta_box_sanitize_cb argument.from the docblockshow_in_rest, 'rest_base' and 'rest_controller_class' arguments to register the taxonomy in REST API.from the docblockpublicly_queryable argument.from the docblockpublic argument now controls whether the taxonomy can be queried on the front end.from the docblockshow_ui argument is now enforced on the term editing screen.from the docblockshow_in_quick_edit argument.from the docblockAbout 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.