-
-
Save whitesided/7bb122d968deb0f24f9414d386ce2d02 to your computer and use it in GitHub Desktop.
Helper functions to make generating WordPress post type labels and taxonomy labels easier
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Register a Book post type | |
$args = array( | |
'labels' => generate_post_type_labels( 'book', 'books' ), | |
'public' => true, | |
'publicly_queryable' => true, | |
'show_ui' => true, | |
'show_in_menu' => true, | |
'query_var' => true, | |
'rewrite' => array( 'slug' => 'book' ), | |
'capability_type' => 'post', | |
'has_archive' => true, | |
'hierarchical' => false, | |
'menu_position' => null, | |
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' ), | |
); | |
register_post_type( 'book', $args ); | |
// Register a Writer taxonomy for Books | |
$args = array( | |
'hierarchical' => false, | |
'labels' => generate_taxonomy_labels( 'writer', 'writers' ), | |
'show_ui' => true, | |
'show_admin_column' => true, | |
'update_count_callback' => '_update_post_term_count', | |
'query_var' => true, | |
'rewrite' => array( 'slug' => 'writer' ), | |
); | |
register_taxonomy( 'writer', 'book', $args ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Simplify generating post type labels by only needing to enter a singular and plural verison | |
* | |
* @param string $singular The singular version of the post type label | |
* @param string $plural The plural version of the post type label | |
* @param array $overrides Specific labels to override that might not fit this pattern | |
* @return array Post type labels | |
*/ | |
function generate_post_type_labels( $singular = '', $plural = '', $overrides = array() ) { | |
$lc_plural = strtolower( $plural ); | |
$uc_plural = ucwords( $lc_plural ); | |
$lc_singular = strtolower( $singular ); | |
$uc_singular = ucwords( $lc_singular ); | |
$labels = array( | |
'name' => $uc_plural, | |
'singular_name' => $uc_singular, | |
'menu_name' => $uc_plural, | |
'name_admin_bar' => $uc_singular, | |
'archives' => $uc_singular . ' Archives', | |
'attributes' => $uc_singular . ' Attributes', | |
'parent_item_colon' => 'Parent ' . $uc_singular . ':', | |
'all_items' => 'All ' . $uc_plural, | |
'add_new_item' => 'Add New ' . $uc_singular, | |
'add_new' => 'Add New', | |
'new_item' => 'New ' . $uc_singular, | |
'edit_item' => 'Edit ' . $uc_singular, | |
'update_item' => 'Update ' . $uc_singular, | |
'view_item' => 'View ' . $uc_singular, | |
'view_items' => 'View ' . $uc_plural, | |
'search_items' => 'Search ' . $uc_singular, | |
'not_found' => 'Not found', | |
'not_found_in_trash' => 'Not found in Trash', | |
'featured_image' => 'Featured Image', | |
'set_featured_image' => 'Set featured image', | |
'remove_featured_image' => 'Remove featured image', | |
'use_featured_image' => 'Use as featured image', | |
'insert_into_item' => 'Insert into ' . $lc_singular, | |
'uploaded_to_this_item' => 'Uploaded to this ' . $lc_singular, | |
'items_list' => ucfirst( $lc_plural ) . ' list', | |
'items_list_navigation' => ucfirst( $lc_plural ) . ' list navigation', | |
'filter_items_list' => 'Filter ' . $lc_plural . ' list', | |
); | |
return wp_parse_args( $overrides, $labels ); | |
} | |
/** | |
* Simplify generating taxonomy labels by only needing to enter a singular and plural verison | |
* | |
* @param string $singular The singular version of the taxonomy label | |
* @param string $plural The plural version of the taxonomy label | |
* @param array $overrides Specific labels to override that might not fit this pattern | |
* @return array Taxonomy labels | |
*/ | |
function generate_taxonomy_labels( $singular = '', $plural = '', $overrides = array() ) { | |
$lc_plural = strtolower( $plural ); | |
$uc_plural = ucwords( $lc_plural ); | |
$lc_singular = strtolower( $singular ); | |
$uc_singular = ucwords( $lc_singular ); | |
$labels = array( | |
'name' => $uc_plural, | |
'singular_name' => $uc_singular, | |
'menu_name' => $uc_plural, | |
'all_items' => 'All ' . $uc_plural, | |
'parent_item' => 'Parent ' . $uc_singular, | |
'parent_item_colon' => 'Parent ' . $uc_singular . ':', | |
'new_item_name' => 'New ' . $uc_singular . ' Name', | |
'add_new_item' => 'Add New ' . $uc_singular, | |
'edit_item' => 'Edit ' . $uc_singular, | |
'update_item' => 'Update ' . $uc_singular, | |
'view_item' => 'View ' . $uc_singular, | |
'separate_items_with_commas' => 'Separate ' . $lc_plural . ' with commas', | |
'add_or_remove_items' => 'Add or remove ' . $lc_plural, | |
'choose_from_most_used' => 'Choose from the most used', | |
'popular_items' => 'Popular ' . $uc_plural, | |
'search_items' => 'Search ' . $uc_plural, | |
'not_found' => 'Not Found', | |
'no_terms' => 'No ' . $lc_plural, | |
'items_list' => ucfirst( $lc_plural ) . ' list', | |
'items_list_navigation' => ucfirst( $lc_plural ) . ' list navigation', | |
); | |
return wp_parse_args( $overrides, $labels ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment