register_taxonomy() – Creates or modifies a taxonomy object.

You appear to be a bot. Output may be restricted

Description

Creates or modifies a taxonomy object.

Note: Do not use before the initinit 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.

Usage

$WP_Taxonomy|WP_Error = register_taxonomy( $taxonomy, $object_type, $args );

Parameters

$taxonomy
( string ) required – Taxonomy key, must not exceed 32 characters.
$object_type
( array|string ) required – Object type or array of object types with which the taxonomy should be associated.
$args
( array|string ) optional – { Optional. Array or query string of arguments for registering a taxonomy.
$labels
( string[] ) optional – 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(). Default empty array.
$description
( string ) optional – A short descriptive summary of what the taxonomy is for. Default empty.
$public
( bool ) optional – 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_queryable
( bool ) optional – Whether the taxonomy is publicly queryable. If not set, the default is inherited from $public
$hierarchical
( bool ) optional – Whether the taxonomy is hierarchical. Default false.
$show_ui
( bool ) optional – 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_menu
( bool ) optional – 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_menus
( bool ) optional – Makes this taxonomy available for selection in navigation menus. If not set, the default is inherited from $public (default true).
$show_in_rest
( bool ) optional – Whether to include the taxonomy in the REST API. Set this to true for the taxonomy to be available in the block editor.
$rest_base
( string ) optional – To change the base url of REST API route. Default is $taxonomy.
$rest_namespace
( string ) optional – To change the namespace URL of REST API route. Default is wp/v2.
$rest_controller_class
( string ) optional – REST API Controller class name. Default is 'WP_REST_Terms_Controller'.
$show_tagcloud
( bool ) optional – 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_edit
( bool ) optional – 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_column
( bool ) optional – Whether to display a column for the taxonomy on its post type listing screens. Default false.
$meta_box_cb
( bool|callable ) optional – 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_cb
( callable ) optional – 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`.
$capabilities
( string[] ) optional – { Array of capabilities for this taxonomy.
$manage_terms
( string ) optional – Default 'manage_categories'.
$edit_terms
( string ) optional – Default 'manage_categories'.
$delete_terms
( string ) optional – Default 'manage_categories'.
$assign_terms
( string ) optional – Default 'edit_posts'. }
$rewrite
( bool|array ) optional – { 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 ) optional – Customize the permastruct slug. Default $taxonomy key.
$with_front
( bool ) optional – Should the permastruct be prepended with WP_Rewrite::$front. Default true.
$hierarchical
( bool ) optional – Either hierarchical rewrite tag or not. Default false.
$ep_mask
( int ) optional – Assign an endpoint mask. Default `EP_NONE`. }
$query_var
( string|bool ) optional – 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_callback
( callable ) optional – 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_term
( string|array ) optional – { Default term to be used for the taxonomy.
$name
( string ) optional – Name of default term.
$slug
( string ) optional – Slug for default term. Default empty.
$description
( string ) optional – Description for default term. Default empty. }
$sort
( bool ) optional – 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.
$args
( array ) optional – Array of arguments to automatically use inside wp_get_object_terms() for this taxonomy.
$_builtin
( bool ) optional – This taxonomy is a "built-in" taxonomy. INTERNAL USE ONLY! Default false. }

Returns

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

Source

File name: wordpress/wp-includes/taxonomy.php


Lines:

1 to 77 of 77
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( register_taxonomy, __( '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;
}
 

 View on GitHub View on Trac