Last active
January 29, 2021 06:26
-
-
Save developer-anuragsingh/9d2de143c783f343c080e798325833c0 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
function Register_New_Cpt() | |
{ | |
$cpt_name = 'YOUR_NEW_POST_TYPE'; | |
$all_taxonomy = array('YOUR_NEW_TAXONOMY'); | |
$namespace = 'YOUR_NAMESPACE'; | |
$cpt_name_array = cpt_taxo_naming_convention($cpt_name); | |
foreach ($all_taxonomy as $single) { | |
$taxo_name_array = cpt_taxo_naming_convention($single); | |
$opts['public'] = true; | |
$opts['publicly_queryable'] = true; | |
$opts['hierarchical'] = true; | |
$opts['show_ui'] = true; | |
$opts['show_in_menu'] = true; | |
$opts['show_in_nav_menus'] = true; | |
$opts['show_in_rest'] = true; | |
$opts['show_tagcloud'] = true; | |
$opts['show_in_quick_edit'] = true; | |
$opts['show_admin_column'] = true; | |
$opts['sort'] = 'true'; | |
$opts['capabilities']['manage_terms'] = 'manage_categories'; | |
$opts['capabilities']['edit_terms'] = 'manage_categories'; | |
$opts['capabilities']['delete_terms'] = 'manage_categories'; | |
$opts['capabilities']['assign_terms'] = 'edit_posts'; | |
$opts['rewrite']['slug'] = __($taxo_name_array['slug'], $namespace); | |
$opts['rewrite']['with_front'] = false; | |
$opts['rewrite']['hierarchical'] = false; | |
$opts['rewrite']['ep_mask'] = EP_NONE; | |
$opts['labels']['name'] = __($taxo_name_array['plural'], $namespace); | |
$opts['labels']['singular_name'] = __($taxo_name_array['singular'], $namespace); | |
$opts['labels']['menu_name'] = __($taxo_name_array['plural'], $namespace); | |
$opts['labels']['add_new_item'] = __("Add New {$taxo_name_array['singular']}", $namespace); | |
$opts['labels']['add_or_remove_items'] = __("Add or remove {$taxo_name_array['plural']}", $namespace); | |
$opts['labels']['all_items'] = __($taxo_name_array['plural'], $namespace); | |
$opts['labels']['choose_from_most_used'] = __("Choose from most used {$taxo_name_array['plural']}", $namespace); | |
$opts['labels']['edit_item'] = __("Edit {$taxo_name_array['singular']}", $namespace); | |
$opts['labels']['new_item_name'] = __("New {$taxo_name_array['singular']} Name", $namespace); | |
$opts['labels']['not_found'] = __("No {$taxo_name_array['plural']} Found", $namespace); | |
$opts['labels']['parent_item'] = __("Parent {$taxo_name_array['singular']}", $namespace); | |
$opts['labels']['parent_item_colon'] = __("Parent {$taxo_name_array['singular']}:", $namespace); | |
$opts['labels']['popular_items'] = __("Popular {$taxo_name_array['plural']}", $namespace); | |
$opts['labels']['search_items'] = __("Search {$taxo_name_array['plural']}", $namespace); | |
$opts['labels']['separate_items_with_commas'] = __("Separate {$taxo_name_array['plural']} with commas", $namespace); | |
$opts['labels']['update_item'] = __("Update {$taxo_name_array['singular']}", $namespace); | |
$opts['labels']['view_item'] = __("View {$taxo_name_array['singular']}", $namespace); | |
register_taxonomy($taxo_name_array['slug'], sanitize_title($cpt_name_array['plural']), $opts); | |
} | |
/** | |
* Create new custom post type | |
*/ | |
$opts = array(); | |
$opts['can_export'] = true; | |
$opts['capability_type'] = 'post'; | |
$opts['description'] = ''; | |
$opts['exclude_from_search'] = false; | |
$opts['has_archive'] = sanitize_title($cpt_name_array['plural']); // bool|string | |
$opts['hierarchical'] = true; | |
$opts['map_meta_cap'] = true; | |
$opts['menu_icon'] = 'dashicons-bank'; | |
$opts['menu_position'] = 25; | |
$opts['public'] = true; | |
$opts['publicly_querable'] = true; | |
$opts['query_var'] = true; | |
$opts['register_meta_box_cb'] = ''; | |
$opts['rewrite'] = false; | |
$opts['show_in_admin_bar'] = true; // Define For 'Top Menu' bar | |
$opts['show_in_menu'] = true; | |
$opts['show_in_nav_menu'] = true; | |
$opts['show_ui'] = true; | |
$opts['show_in_rest'] = true; | |
$opts['supports'] = array('title', 'editor', 'thumbnail'); | |
$opts['taxonomies'] = array(); | |
$opts['capabilities']['delete_others_posts'] = "delete_others_posts"; | |
$opts['capabilities']['delete_post'] = "delete_post"; | |
$opts['capabilities']['delete_posts'] = "delete_posts"; | |
$opts['capabilities']['delete_private_posts'] = "delete_private_posts"; | |
$opts['capabilities']['delete_published_posts'] = "delete_published_posts"; | |
$opts['capabilities']['edit_others_posts'] = "edit_others_posts"; | |
$opts['capabilities']['edit_post'] = "edit_post"; | |
$opts['capabilities']['edit_posts'] = "edit_posts"; | |
$opts['capabilities']['edit_private_posts'] = "edit_private_posts"; | |
$opts['capabilities']['edit_published_posts'] = "edit_published_posts"; | |
$opts['capabilities']['publish_posts'] = "publish_posts"; | |
$opts['capabilities']['read_post'] = "read_post"; | |
$opts['capabilities']['read_private_posts'] = "read_private_posts"; | |
$opts['labels']['name'] = __($cpt_name_array['plural'], $namespace); | |
$opts['labels']['singular_name'] = __($cpt_name_array['singular'], $namespace); | |
$opts['labels']['add_new'] = __("Add New {$cpt_name_array['singular']}", $namespace); | |
$opts['labels']['add_new_item'] = __("Add New {$cpt_name_array['singular']}", $namespace); | |
$opts['labels']['edit_item'] = __("Edit {$cpt_name_array['singular']}", $namespace); | |
$opts['labels']['new_item'] = __("New {$cpt_name_array['singular']}", $namespace); | |
$opts['labels']['view_item'] = __("View {$cpt_name_array['singular']}", $namespace); | |
$opts['labels']['view_items'] = __("View {$cpt_name_array['plural']}", $namespace); | |
$opts['labels']['search_items'] = __("Search {$cpt_name_array['plural']}", $namespace); | |
$opts['labels']['not_found'] = __("No {$cpt_name_array['plural']} Found", $namespace); | |
$opts['labels']['not_found_in_trash'] = __("No {$cpt_name_array['plural']} Found in Trash", $namespace); | |
$opts['labels']['parent_item_colon'] = __("Parent {$cpt_name_array['plural']} :", $namespace); | |
$opts['labels']['all_items'] = __($cpt_name_array['plural'], $namespace); | |
$opts['labels']['archives'] = __("{$cpt_name_array['plural']} archives", $namespace); | |
$opts['labels']['attributes'] = __("{$cpt_name_array['singular']} attributes", $namespace); | |
$opts['labels']['insert_into_item'] = __("Insert into {$cpt_name_array['singular']}", $namespace); | |
$opts['labels']['uploaded_to_this_item'] = __("Uploaded to this {$cpt_name_array['singular']}", $namespace); | |
$opts['labels']['featured_image'] = __("{$cpt_name_array['singular']} Image", $namespace); | |
$opts['labels']['set_featured_image'] = __("Set {$cpt_name_array['singular']} Image", $namespace); | |
$opts['labels']['remove_featured_image'] = __("Remove {$cpt_name_array['singular']} Image", $namespace); | |
$opts['labels']['use_featured_image'] = __("Use as {$cpt_name_array['singular']} image", $namespace); | |
$opts['labels']['menu_name'] = __($cpt_name_array['plural'], $namespace); | |
$opts['labels']['filter_items_list'] = __("Filter {$cpt_name_array['plural']} list", $namespace); | |
$opts['labels']['items_list_navigation'] = __("{$cpt_name_array['singular']} list navigation", $namespace); | |
$opts['labels']['items_list'] = __("{$cpt_name_array['singular']} list", $namespace); | |
$opts['labels']['item_published'] = __("{$cpt_name_array['singular']} published", $namespace); | |
$opts['labels']['item_published_privately'] = __("{$cpt_name_array['singular']} published privately", $namespace); | |
$opts['labels']['item_reverted_to_draft'] = __("{$cpt_name_array['singular']} reverted to draft", $namespace); | |
$opts['labels']['item_scheduled'] = __("{$cpt_name_array['singular']} scheduled", $namespace); | |
$opts['labels']['item_updated'] = __("{$cpt_name_array['singular']} updated", $namespace); | |
$opts['rewrite']['ep_mask'] = EP_PERMALINK; | |
$opts['rewrite']['feeds'] = false; | |
$opts['rewrite']['pages'] = true; | |
$opts['rewrite']['slug'] = __($cpt_name_array['slug'], $namespace); | |
$opts['rewrite']['with_front'] = false; | |
register_post_type($cpt_name_array['plural'], $opts); | |
} | |
add_action('init', 'Register_New_cpt'); | |
function cpt_taxo_naming_convention($word) | |
{ | |
$name = []; | |
$name['singular'] = ucwords(strtolower(preg_replace('/\s+/', ' ', $word))); | |
$name['slug'] = sanitize_title(strtolower($name['singular'])); | |
$last_character = substr($name['singular'], -1); | |
if ($last_character === 'y') { | |
$name['plural'] = substr_replace($name['singular'], 'ies', -1); | |
} else { | |
$name['plural'] = $name['singular'] . 's'; // add 's' to convert singular name to plural | |
} | |
return $name; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment