get_taxonomy_labels()WP 3.0.0

Builds an object with all taxonomy labels out of a taxonomy object.

Return

Object. Taxonomy labels object. The first default value is for non-hierarchical taxonomies (like tags) and the second one is for hierarchical taxonomies (like categories).

Usage

get_taxonomy_labels( $tax );
$tax(WP_Taxonomy) (required)
Taxonomy object.

Changelog

Since 3.0.0 Introduced.
Since 4.3.0 Added the no_terms label.
Since 4.4.0 Added the items_list_navigation and items_list labels.
Since 4.9.0 Added the most_used and back_to_items labels.
Since 5.7.0 Added the filter_by_item label.
Since 5.8.0 Added the item_link and item_link_description labels.
Since 5.9.0 Added the name_field_description, slug_field_description, parent_field_description, and desc_field_description labels.

Code of get_taxonomy_labels() WP 5.9.3

function get_taxonomy_labels( $tax ) {
	$tax->labels = (array) $tax->labels;

	if ( isset( $tax->helps ) && empty( $tax->labels['separate_items_with_commas'] ) ) {
		$tax->labels['separate_items_with_commas'] = $tax->helps;
	}

	if ( isset( $tax->no_tagcloud ) && empty( $tax->labels['not_found'] ) ) {
		$tax->labels['not_found'] = $tax->no_tagcloud;
	}

	$name_field_description   = __( 'The name is how it appears on your site.' );
	$slug_field_description   = __( 'The “slug” is the URL-friendly version of the name. It is usually all lowercase and contains only letters, numbers, and hyphens.' );
	$parent_field_description = __( 'Assign a parent term to create a hierarchy. The term Jazz, for example, would be the parent of Bebop and Big Band.' );
	$desc_field_description   = __( 'The description is not prominent by default; however, some themes may show it.' );

	$nohier_vs_hier_defaults = array(
		'name'                       => array( _x( 'Tags', 'taxonomy general name' ), _x( 'Categories', 'taxonomy general name' ) ),
		'singular_name'              => array( _x( 'Tag', 'taxonomy singular name' ), _x( 'Category', 'taxonomy singular name' ) ),
		'search_items'               => array( __( 'Search Tags' ), __( 'Search Categories' ) ),
		'popular_items'              => array( __( 'Popular Tags' ), null ),
		'all_items'                  => array( __( 'All Tags' ), __( 'All Categories' ) ),
		'parent_item'                => array( null, __( 'Parent Category' ) ),
		'parent_item_colon'          => array( null, __( 'Parent Category:' ) ),
		'name_field_description'     => array( $name_field_description, $name_field_description ),
		'slug_field_description'     => array( $slug_field_description, $slug_field_description ),
		'parent_field_description'   => array( null, $parent_field_description ),
		'desc_field_description'     => array( $desc_field_description, $desc_field_description ),
		'edit_item'                  => array( __( 'Edit Tag' ), __( 'Edit Category' ) ),
		'view_item'                  => array( __( 'View Tag' ), __( 'View Category' ) ),
		'update_item'                => array( __( 'Update Tag' ), __( 'Update Category' ) ),
		'add_new_item'               => array( __( 'Add New Tag' ), __( 'Add New Category' ) ),
		'new_item_name'              => array( __( 'New Tag Name' ), __( 'New Category Name' ) ),
		'separate_items_with_commas' => array( __( 'Separate tags with commas' ), null ),
		'add_or_remove_items'        => array( __( 'Add or remove tags' ), null ),
		'choose_from_most_used'      => array( __( 'Choose from the most used tags' ), null ),
		'not_found'                  => array( __( 'No tags found.' ), __( 'No categories found.' ) ),
		'no_terms'                   => array( __( 'No tags' ), __( 'No categories' ) ),
		'filter_by_item'             => array( null, __( 'Filter by category' ) ),
		'items_list_navigation'      => array( __( 'Tags list navigation' ), __( 'Categories list navigation' ) ),
		'items_list'                 => array( __( 'Tags list' ), __( 'Categories list' ) ),
		/* translators: Tab heading when selecting from the most used terms. */
		'most_used'                  => array( _x( 'Most Used', 'tags' ), _x( 'Most Used', 'categories' ) ),
		'back_to_items'              => array( __( '← Go to Tags' ), __( '← Go to Categories' ) ),
		'item_link'                  => array(
			_x( 'Tag Link', 'navigation link block title' ),
			_x( 'Category Link', 'navigation link block description' ),
		),
		'item_link_description'      => array(
			_x( 'A link to a tag.', 'navigation link block description' ),
			_x( 'A link to a category.', 'navigation link block description' ),
		),
	);

	$nohier_vs_hier_defaults['menu_name'] = $nohier_vs_hier_defaults['name'];

	$labels = _get_custom_object_labels( $tax, $nohier_vs_hier_defaults );

	$taxonomy = $tax->name;

	$default_labels = clone $labels;

	/**
	 * Filters the labels of a specific taxonomy.
	 *
	 * The dynamic portion of the hook name, `$taxonomy`, refers to the taxonomy slug.
	 *
	 * Possible hook names include:
	 *
	 *  - `taxonomy_labels_category`
	 *  - `taxonomy_labels_post_tag`
	 *
	 * @since 4.4.0
	 *
	 * @see get_taxonomy_labels() for the full list of taxonomy labels.
	 *
	 * @param object $labels Object with labels for the taxonomy as member variables.
	 */
	$labels = apply_filters( "taxonomy_labels_{$taxonomy}", $labels );

	// Ensure that the filtered labels contain all required default values.
	$labels = (object) array_merge( (array) $default_labels, (array) $labels );

	return $labels;
}