Created
December 20, 2012 14:20
-
-
Save lgedeon/4345543 to your computer and use it in GitHub Desktop.
Class to simplify CPT and taxonomy creation including pluralizer algorithm
This file contains hidden or 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 | |
/** | |
* A small, quick class to make Custom Post Types and Taxonomies easier | |
*/ | |
class CPT_Builder { | |
static $plural = array( '/(quiz)$/i' => "$1zes", | |
'/^(ox)$/i' => "$1en", '/([m|l])ouse$/i' => "$1ice", | |
'/(matr|vert|ind)ix|ex$/i' => "$1ices", '/(x|ch|ss|sh)$/i' => "$1es", | |
'/([^aeiouy]|qu)y$/i' => "$1ies", '/(hive)$/i' => "$1s", | |
'/(?:([^f])fe|([lr])f)$/i' => "$1$2ves", '/(shea|lea|loa|thie)f$/i' => "$1ves", | |
'/sis$/i' => "ses", '/([ti])um$/i' => "$1a", | |
'/(tomat|potat|ech|her|vet)o$/i' => "$1oes", '/(bu)s$/i' => "$1ses", | |
'/(alias)$/i' => "$1es", '/(octop)us$/i' => "$1i", | |
'/(ax|test)is$/i' => "$1es", '/(us)$/i' => "$1es", | |
'/s$/i' => "s", '/$/' => "s", ); | |
static $irregular = array( 'move' => 'moves', 'foot' => 'feet', 'goose' => 'geese', 'sex' => 'sexes', 'child' => 'children', 'man' => 'men', 'tooth' => 'teeth', 'person' => 'people' ); | |
static $uncountable = array( 'sheep', 'fish', 'deer', 'series', 'species', 'money', 'rice', 'information', 'equipment' ); | |
function pluralize( $string ) { | |
// save some time in the case that singular and plural are the same | |
if ( in_array( strtolower( $string ), self::$uncountable ) ) | |
return $string; | |
// check for irregular singular forms | |
foreach ( self::$irregular as $pattern => $result ) { | |
$pattern = '/' . $pattern . '$/i'; | |
if ( preg_match( $pattern, $string ) ) | |
return preg_replace( $pattern, $result, $string ); | |
} | |
// check for matches using regular expressions | |
foreach ( self::$plural as $pattern => $result ) { | |
if ( preg_match( $pattern, $string ) ) | |
return preg_replace( $pattern, $result, $string ); | |
} | |
return $string; | |
} | |
/** | |
* Provide smarter defaults for the core register_post_type function. | |
* | |
* @param string $post_type_name | |
* @param string $name_space | |
* @param array $args | |
* @return string name of new post_type | |
*/ | |
function register_post_type( $post_type_name, $name_space, $args = array( ) ) { | |
$name_space = sanitize_key( $name_space ); | |
$post_type_key = sanitize_key( $post_type_name ); | |
$post_type_plural = self::pluralize( $post_type_name ); | |
$defaults = array( | |
'label' => $post_type_plural, | |
'description' => $post_type_plural, | |
'public' => true, | |
'menu_position' => 21, | |
'menu_icon' => get_bloginfo( 'template_url' ) . '/images/admin_' . $post_type_key . '.png', | |
'supports' => array( 'title', 'editor', 'revisions', 'thumbnail' ), | |
'register_meta_box_cb' => $post_type_key . '_meta_box_cb', | |
'labels' => array( | |
'name' => $post_type_plural, | |
'singular_name' => $post_type_name, | |
'add_new' => 'Add ' . $post_type_name, | |
'add_new_item' => 'Add New ' . $post_type_name, | |
'edit_item' => 'Edit ' . $post_type_name, | |
'new_item' => 'New ' . $post_type_name, | |
'view_item' => 'View ' . $post_type_name, | |
'search_items' => 'Search ' . $post_type_plural, | |
'not_found' => 'No ' . $post_type_plural . ' found', | |
'not_found_in_trash' => 'No ' . $post_type_plural . ' found in Trash' | |
), | |
'show_in_nav_menus' => false, | |
'hierarchical' => true, | |
/* The rest of these can be set if needed by passing them in the $args variable - | |
* listed here for reference: 'publicly_queryable', 'exclude_from_search', 'taxonomies', | |
* 'show_ui', 'show_in_nav_menus', 'show_in_menu', 'show_in_admin_bar', | |
*/ | |
); | |
$args = wp_parse_args( $args, $defaults ); | |
register_post_type( $name_space . '_' . $post_type_key, $args ); | |
return $name_space . '_' . $post_type_key; | |
} | |
function register_taxonomy( $taxonomy_name, $post_type, $name_space ) { | |
$name_space = sanitize_key( $name_space ); | |
$taxonomy_key = sanitize_key( $taxonomy_name ); | |
$taxonomy_plural = self::pluralize( $taxonomy_name ); | |
register_taxonomy( $name_space . '_' . $taxonomy_key, $post_type, array( | |
'label' => $taxonomy_plural, | |
'hierarchical' => true, | |
'rewrite' => false, | |
'labels' => array( | |
'name' => $taxonomy_plural, | |
'singular_name' => $taxonomy_name, | |
'search_items' => 'Search ' . $taxonomy_plural, | |
'all_items' => 'All ' . $taxonomy_plural, | |
'parent_item' => 'Parent ' . $taxonomy_name, | |
'parent_item_colon' => 'Parent ' . $taxonomy_name . ':', | |
'edit_item' => 'Edit ' . $taxonomy_name, | |
'update_item' => 'Update ' . $taxonomy_name, | |
'add_new_item' => 'Add New ' . $taxonomy_name, | |
'new_item_name' => 'New ' . $taxonomy_name | |
) | |
) ); | |
} | |
} | |
// *** Usage *** | |
function plugin_init() { | |
$testimonials = CPT_Builder::register_post_type( "Testimonial", 'plugin', array( | |
'supports' => array( 'editor' ), | |
) ); | |
} | |
add_action( 'init', 'plugin_init' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment