Skip to content

Instantly share code, notes, and snippets.

@ianpegg
Last active September 25, 2023 16:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ianpegg/8e5039a2d4eb7ce631e25d93f574768a to your computer and use it in GitHub Desktop.
Save ianpegg/8e5039a2d4eb7ce631e25d93f574768a to your computer and use it in GitHub Desktop.
EggMUP that registers a demo Custom Post Type and Custom Taxonomy. Edit the arrays to create your own, then drop into your mu-plugins directory.
<?php
/**
* Plugin Name: EggMUP: Register Custom Data Types
* Plugin URI: https://gist.github.com/ianpegg/
* Description: Defines and registers custom post types and custom taxonomies.
* Version: 1.0.0
* Author: Ian Pegg
* Author URI: https://eggcupwebdesign.com
* php version 8.0.28
*
* @category Must_Use_Plugin
* @package WordPress_Plugin
* @author Ian Pegg <ian@eggcupweb.com>
* @license GNU/GPLv3 https://www.gnu.org/licenses/gpl-3.0.txt
* @link https://eggcupwebdesign.com
*/
namespace EggCup\MUP\CustomDataTypes;
if (!defined('ABSPATH')) {
exit;
}
register_deactivation_hook(__FILE__, '\flush_rewrite_rules');
register_activation_hook(__FILE__, __NAMESPACE__ . '\\Register_Custom_Data_types');
/**
* Register taxonomy first by demoting CPT registration priority:
* @link https://cnpagency.com/blog/the-right-way-to-do-wordpress-custom-taxonomy-rewrites/
*/
add_action('init', __NAMESPACE__ . '\\Register_Custom_taxonomies', 10);
add_action('init', __NAMESPACE__ . '\\Register_Custom_Post_types', 11);
function Register_Custom_Data_types()
{
/**
* Call the custom data type registration functions here.
* These functions should also be hooked into 'init'.
*/
Register_Custom_taxonomies();
Register_Custom_Post_types();
flush_rewrite_rules();
}
function Register_Custom_Post_types()
{
/**
* Initialise a new array to store all of our CPT definitions:
**/
$Arr_cpt_definitions = [];
/**
* Define each CPT in a new array:
**/
$Arr_cpt_definitions[] = [
'Str_cpt_name' => 'projects',
'Arr_args' => [
'label' => esc_html__('Projects', 'eggmup'),
'labels' => [
'name' => esc_html__('Projects', 'eggmup'),
'singular_name' => esc_html__('Projects', 'eggmup'),
'menu_name' => esc_html__('Projects', 'eggmup'),
'all_items' => esc_html__('All Projects', 'eggmup'),
'add_new' => esc_html__('Add new', 'eggmup'),
'add_new_item' => esc_html__('Add new Project', 'eggmup'),
'edit_item' => esc_html__('Edit Project', 'eggmup'),
'new_item' => esc_html__('New Project', 'eggmup'),
'view_item' => esc_html__('View Project', 'eggmup'),
'view_items' => esc_html__('View Projects', 'eggmup'),
'search_items' => esc_html__('Search Projects', 'eggmup'),
'not_found' => esc_html__('No Projects found', 'eggmup'),
'not_found_in_trash' => esc_html__('No Projects found in bin', 'eggmup'),
'parent' => esc_html__('Parent Project:', 'eggmup'),
'featured_image' => esc_html__('Featured image for this Project', 'eggmup'),
'set_featured_image' => esc_html__('Set featured image for this Project', 'eggmup'),
'remove_featured_image' => esc_html__('Remove featured image for this Project', 'eggmup'),
'use_featured_image' => esc_html__('Use as featured image for this Project', 'eggmup'),
'archives' => esc_html__('Project archives', 'eggmup'),
'insert_into_item' => esc_html__('Insert into Project', 'eggmup'),
'uploaded_to_this_item' => esc_html__('Upload to this Project', 'eggmup'),
'filter_items_list' => esc_html__('Filter Project list', 'eggmup'),
'items_list_navigation' => esc_html__('Project list navigation', 'eggmup'),
'items_list' => esc_html__('Projects Project list', 'eggmup'),
'attributes' => esc_html__('Project attributes', 'eggmup'),
'name_admin_bar' => esc_html__('Projects', 'eggmup'),
'item_published' => esc_html__('Project published', 'eggmup'),
'item_published_privately' => esc_html__('Project published privately', 'eggmup'),
'item_reverted_to_draft' => esc_html__('Project reverted to draft', 'eggmup'),
'item_scheduled' => esc_html__('Project scheduled', 'eggmup'),
'item_updated' => esc_html__('Project updated', 'eggmup'),
'parent_item_colon' => esc_html__('Parent Project:', 'eggmup'),
],
'description' => '',
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_rest' => true,
'rest_base' => '',
'rest_controller_class' => 'WP_REST_Posts_Controller',
'has_archive' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'delete_with_user' => false,
'exclude_from_search' => false,
'capability_type' => 'post',
'map_meta_cap' => true,
'hierarchical' => false,
'rewrite' => [
'slug' => 'projects',
'with_front' => false
],
'query_var' => true,
'menu_position' => 7,
'menu_icon' => 'dashicons-portfolio',
'supports' => [
'title',
'editor',
'thumbnail',
'excerpt',
'revisions',
'author'
],
'taxonomies' => [
'services'
],
],
];
/**
* Spin over all of our CPT definitions and register them using WP API:
**/
if (!empty($Arr_cpt_definitions)) {
foreach ($Arr_cpt_definitions as $Arr_cpt) {
register_post_type(
$Arr_cpt[ 'Str_cpt_name' ],
$Arr_cpt[ 'Arr_args' ]
);
}
}
}
function Register_Custom_taxonomies()
{
/**
* Initialise a new array to store all of our Taxonomy definitions:
**/
$Arr_taxonomy_definitions = [];
$Arr_taxonomy_definitions[] = [
'Str_tx_name' => 'services',
'Str_assoc_cpt' => [
'projects'
],
'Arr_args' => [
'label' => esc_html__('Services', 'eggmup'),
'labels' => [
'name' => esc_html__('Services', 'eggmup'),
'singular_name' => esc_html__('Service', 'eggmup'),
'menu_name' => esc_html__('Services', 'eggmup'),
'all_items' => esc_html__('All Services', 'eggmup'),
'edit_item' => esc_html__('Edit Service', 'eggmup'),
'view_item' => esc_html__('View Service', 'eggmup'),
'update_item' => esc_html__('Update Service name', 'eggmup'),
'add_new_item' => esc_html__('Add new Service', 'eggmup'),
'new_item_name' => esc_html__('New Service name', 'eggmup'),
'parent_item' => esc_html__('Parent Service', 'eggmup'),
'parent_item_colon' => esc_html__('Parent Service:', 'eggmup'),
'search_items' => esc_html__('Search Services', 'eggmup'),
'popular_items' => esc_html__('Popular Services', 'eggmup'),
'separate_items_with_commas' => esc_html__('Separate Services with commas', 'eggmup'),
'add_or_remove_items' => esc_html__('Add or remove Services', 'eggmup'),
'choose_from_most_used' => esc_html__('Choose from the most used Services', 'eggmup'),
'not_found' => esc_html__('No Services found', 'eggmup'),
'no_terms' => esc_html__('No Services', 'eggmup'),
'items_list_navigation' => esc_html__('Services list navigation', 'eggmup'),
'items_list' => esc_html__('Services list', 'eggmup'),
],
'public' => true,
'publicly_queryable' => true,
'hierarchical' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'query_var' => true,
'rewrite' => [
'slug' => 'services',
'with_front' => false,
'hierarchical' => true,
],
'show_admin_column' => true,
'show_in_rest' => true,
'rest_base' => 'services',
'rest_controller_class' => 'WP_REST_Terms_Controller',
'show_in_quick_edit' => true,
],
];
/**
* Spin over all of our taxonomy definitions and register them using WP API:
**/
if (!empty($Arr_taxonomy_definitions)) {
foreach ($Arr_taxonomy_definitions as $Arr_taxonomy) {
register_taxonomy(
$Arr_taxonomy[ 'Str_tx_name' ],
$Arr_taxonomy[ 'Str_assoc_cpt' ],
$Arr_taxonomy[ 'Arr_args' ]
);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment