I was sick of repeating myself when making CPTs. This is a pretty simple function that makes it easier.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
add_action('init', 'register_cpts'); function register_cpts() { $heros = array('name'=>'Hero', 'icon' => 'dashicons-awards','position' => 4); jb_register_cpt($heros); } // register a custom post type: ex: jb_register_cpt(array('name' => 'FAQ', 'icon' => 'dashicons-format-status', 'position' => 5, 'is_singular' => true)); function jb_register_cpt($cpt_array = array()){ $default_cpt = array( 'name' => '', 'icon' => 'dashicons-admin-post', 'position' => 4, 'description' => '', 'is_singular' => false, 'exclude_from_search' => false, 'supports' => array('title','editor','thumbnail','page-attributes'), 'taxonomies' => array(), 'has_archive' => false, 'rewrite' => array(), 'public' => true, ); if(! empty($cpt_array)){ $cpt_array = wp_parse_args( $cpt_array, $default_cpt ); $slug = strtolower($cpt_array['name']); $plural = (substr($slug, -1) == 's') ? 'es' : 's'; if(substr($slug, -1) == 'y'){ $slug = rtrim($slug, 'y'); $plural = 'ies'; } $plural_slug = ($cpt_array['is_singular']) ? $slug : $slug.$plural; $plural_slug = str_replace(" ", "-", $plural_slug); $label = ucwords(str_replace("-", " ", $cpt_array['name'])); $is_y = false; if(substr($label, -1) == 'y' && ! $cpt_array['is_singular']){ $label = rtrim($label, 'y'); $is_y = true; } $plural_label = ($cpt_array['is_singular']) ? $label : $label.$plural; //we removed the y from the label to put on an ies...now let's add the Y back. if($is_y){ $label .= 'y'; } register_post_type( $plural_slug, array( 'label' => $plural_label, 'description' => $description, 'public' => $cpt_array['public'], 'show_ui' => true, 'show_in_menu' => true, 'exclude_from_search' => $cpt_array['exclude_from_search'], 'capability_type' => 'post', 'map_meta_cap' => true, 'hierarchical' => true, 'rewrite' => $cpt_array['rewrite'], 'query_var' => true, 'taxonomies' => $cpt_array['taxonomies'], 'menu_position' => $cpt_array['position'], 'menu_icon' => $cpt_array['icon'], 'supports' => $cpt_array['supports'], 'labels' => array ( 'name' => $plural_label, 'singular_name' => $label, 'menu_name' => $plural_label, 'add_new' => 'Add '.$label, 'add_new_item' => 'Add New '.$label, 'edit' => 'Edit', 'edit_item' => 'Edit '.$label, 'new_item' => 'New '.$label, 'view' => 'View '.$plural_label, 'view_item' => 'View '.$label, 'search_items' => 'Search '.$plural_label, 'not_found' => 'No '.$plural_label.' Found', 'not_found_in_trash' => 'No '.$plural_label.' Found in Trash', 'parent' => 'Parent '.$label ) )); } } |
Now this is obviously not a “One Size Fits All” Solution but I find it helpful as a starting point.