burlington-b
7/20/2018 - 10:46 AM

Populate Taxonomy Labels

Significantly simplifies registering custom taxonomies. To populate all taxonomy labels, simply provide the singular and plural label to the populate_taxonomy_labels( $singular, $plural ) function. If there are any situations where the default label structure needs to be overwritten, simply provide an array of the necessary overrides as the third parameter.

<?php

register_taxonomy( 'genre', array( 'book' ), array(
	'hierarchical'      => true,
	'labels'            => populate_taxonomy_labels( 'Genre', 'Genres' ),
	'show_ui'           => true,
	'show_admin_column' => true,
	'query_var'         => true,
	'rewrite'           => array( 'slug' => 'genre' ),
); );
<?php

function populate_taxonomy_labels( $singular, $plural, $overrides = array(), $text_domain = "" ) {
	return shortcode_atts( array(
		'name'                       => _x( $plural, 'Taxonomy General Name', $text_domain ),
		'singular_name'              => _x( $singular, 'Taxonomy Singular Name', $text_domain ),
		'menu_name'                  => __( $plural, $text_domain ),
		'all_items'                  => __( 'All '. $plural, $text_domain ),
		'parent_item'                => __( 'Parent ' . $singular, $text_domain ),
		'parent_item_colon'          => __( 'Parent ' . $singular . ':', $text_domain ),
		'new_item_name'              => __( 'New ' . $singular . ' Name', $text_domain ),
		'add_new_item'               => __( 'Add New ' . $singular, $text_domain ),
		'edit_item'                  => __( 'Edit ' . $singular, $text_domain ),
		'update_item'                => __( 'Update ' . $singular, $text_domain ),
		'view_item'                  => __( 'View ' . $singular, $text_domain ),
		'separate_items_with_commas' => __( 'Separate ' . $plural . ' with commas', $text_domain ),
		'add_or_remove_items'        => __( 'Add or remove ' . $plural, $text_domain ),
		'choose_from_most_used'      => __( 'Choose from the most used', $text_domain ),
		'popular_items'              => __( 'Popular ' . $plural, $text_domain ),
		'search_items'               => __( 'Search ' . $plural, $text_domain ),
		'not_found'                  => __( 'Not Found', $text_domain ),
		'no_terms'                   => __( 'No ' . $plural, $text_domain ),
		'items_list'                 => __( $plural . ' list', $text_domain ),
		'items_list_navigation'      => __( $plural . ' list navigation', $text_domain ),
	), $overrides );
}