Created
March 4, 2020 10:27
-
-
Save csaborio001/24a64c241503eeb1bd6c1fc41e230abd to your computer and use it in GitHub Desktop.
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 | |
function compeer_content_types_init() { | |
/** Other functions hidden for brevity */ | |
register_post_type( 'announcement', generate_compeer_cpt_options( 'announcement', 'announcements', 'announcements', 'dashicons-rss', true, true ) ); | |
} | |
add_action( 'init', 'compeer_content_types_init' ); | |
/** | |
* Generates the necessary CPT labels to be used when creating a custom post type. The names | |
* are automatically converted to the right case so no need to worry about passing the uppercase or | |
* lowercase. | |
* | |
* @param string $singular_name - the singular name of the CPT. | |
* @param string $plural_name - the plural name of the CPT. | |
* | |
* @return array $labels - the generated labels based on the passed parameters. | |
*/ | |
function generate_labels( $singular_name, $plural_name ) { | |
$uc_singular_name = ucwords( $singular_name ); | |
$lc_singular_name = strtolower( $singular_name ); | |
$uc_plural_name = ucwords( $plural_name ); | |
$lc_plural_name = strtolower( $plural_name ); | |
$labels = array( | |
'name' => $uc_plural_name, | |
'singular_name' => $uc_singular_name, | |
'menu_name' => $uc_plural_name, | |
'name_admin_bar' => $uc_singular_name, | |
'add_new' => __( 'Add New ' ) . $uc_singular_name, | |
'add_new_item' => __( 'Add New ' ) . $uc_singular_name, | |
'new_item' => __( 'New ' ) . $uc_singular_name, | |
'edit_item' => __( 'Edit ' ) . $uc_singular_name, | |
'view_item' => __( 'View ' ) . $uc_singular_name, | |
'all_items' => __( 'All ' ) . $uc_plural_name, | |
'search_items' => __( 'Search ' ) . $uc_plural_name, | |
'parent_item_colon' => __( 'Parent ' ) . $uc_plural_name . ':', | |
'not_found' => __( 'No ' ) . $lc_plural_name . __( ' found.' ), | |
'not_found_in_trash' => __( 'No ' ) . $lc_plural_name . __( ' found in Trash.' ), | |
); | |
return $labels; | |
} | |
/** | |
* Generates the CPT option array to be used for CPT creation. These are the common options that | |
* all Compeer CPT share, therefore it was all encapsulated into one function. | |
* | |
* @param string $singular_name - the singular name of the CPT. | |
* @param string $plural_name - the plural name of the CPT. | |
* @param string $slug - the slug to be used. | |
* @param string $menu_icon - the dashicons name of the menu icon to use. | |
* | |
* @return array $args - the generated options array based in the parameter input. | |
*/ | |
function generate_compeer_cpt_options( $singular_name, $plural_name, $slug, $menu_icon, $public = false, $has_archive = false ) { | |
$args = array( | |
'labels' => generate_labels( $singular_name, $plural_name ), | |
'description' => __( 'Description.', 'compeer' ), | |
'public' => $public, | |
'publicly_queryable' => true, | |
'show_ui' => true, | |
'show_in_menu' => true, | |
'query_var' => true, | |
'rewrite' => array( 'slug' => $slug ), | |
'map_meta_cap' => true, | |
'capability_type' => $slug, | |
'has_archive' => $has_archive, | |
'hierarchical' => false, | |
'menu_position' => null, | |
'menu_icon' => $menu_icon, | |
'supports' => array( 'title' ), | |
); | |
return $args; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment