Created
January 25, 2020 12:15
-
-
Save DaveyJake/f59ba722578cfd7b12f3d2b2ceed74a4 to your computer and use it in GitHub Desktop.
[WP] An elongated file showing how to register a custom taxonomy and custom post type.
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 | |
/** | |
* Developer API: Snippets for common WordPress configuration setups. | |
* | |
* IMPORTANT: You MUST defined the `const` variables in this file before | |
* implementing it into your current project. | |
* | |
* @link https://codex.wordpress.org/Function_Reference/register_taxonomy | |
* @link https://codex.wordpress.org/Function_Reference/register_post_type | |
* | |
* @author Davey Jacobson <[email protected]> | |
* @package Davey_Jacobson | |
* @subpackage Snippets | |
* @version 0.0.1 | |
*/ | |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly. | |
class DJ_Snippets { | |
/** | |
* Your project's text domain used for translations. | |
* | |
* @var string | |
*/ | |
const TEXT_DOMAIN = ''; | |
/** | |
* The de facto slug of the custom taxonomy. | |
* | |
* @var string | |
*/ | |
const TAXONOMY_SLUG = ''; | |
/** | |
* The plural name of the taxonomy. | |
* | |
* @var string | |
*/ | |
const TAX_NAME_PLURAL = ''; | |
/** | |
* The singular name of the taxonomy. | |
* | |
* @var string | |
*/ | |
const TAX_NAME_SINGLE = ''; | |
/** | |
* The name of the taxonmy if viewed in menu. | |
* | |
* @var string | |
*/ | |
const TAX_MENU_NAME = ''; | |
/** | |
* The slug of the taxonmy if viewed in a URL. | |
* | |
* @var string | |
*/ | |
const TAX_REWRITE_SLUG = ''; | |
/** | |
* The de facto slug of the custom post type. | |
* | |
* @var string | |
*/ | |
const POST_TYPE_SLUG = ''; | |
/** | |
* The dashicon for the custom post type menu. | |
* | |
* @var string | |
*/ | |
const PT_DASHICON = ''; | |
/** | |
* The description of the custom post type. | |
* | |
* @var string | |
*/ | |
const PT_DESCRIPTION = ''; | |
/** | |
* The plural name of the taxonomy. | |
* | |
* @var string | |
*/ | |
const PT_NAME_PLURAL = ''; | |
/** | |
* The singular name of the taxonomy. | |
* | |
* @var string | |
*/ | |
const PT_NAME_SINGLE = ''; | |
/** | |
* The name of the taxonmy if viewed in menu. | |
* | |
* @var string | |
*/ | |
const PT_MENU_NAME = ''; | |
/** | |
* The slug of the taxonmy if viewed in a URL. | |
* | |
* @var string | |
*/ | |
const PT_REWRITE_SLUG = ''; | |
/** | |
* Primary constructor. | |
* | |
* @return DJ_Snippets | |
*/ | |
public function __construct() { | |
/** | |
* The post types this taxonomy applies to. | |
* | |
* @var array | |
*/ | |
self::$post_types = array( self::TAXONOMY_SLUG ); | |
/** | |
* The taxonomies applied to the post type. | |
* | |
* @var array | |
*/ | |
self::$taxonomies = array( self::POST_TYPE_SLUG ); | |
/** | |
* This is where the magic happens! | |
*/ | |
add_action( 'init', array( __CLASS__, 'register_custom_taxonomy' ), 10 ); | |
add_action( 'init', array( __CLASS__, 'register_custom_post_type' ), 10 ); | |
} | |
/** | |
* Register taxonomy template. | |
* | |
* @since DJ_Snippets 0.0.1 | |
*/ | |
public static function register_custom_taxonomy() { | |
if ( taxonomy_exists( self::TAXONOMY_SLUG ) ) { | |
return; | |
} | |
/** | |
* Filter prefix. | |
* | |
* @var string | |
*/ | |
$func = __FUNCTION__; | |
/** | |
* Default labels. | |
* | |
* @var array | |
*/ | |
$labels = array( | |
'name' => _x( self::TAX_NAME_PLURAL, 'Taxonomy General Name', self::TEXT_DOMAIN ), | |
'singular_name' => _x( self::TAX_NAME_SINGLE, 'Taxonomy Singular Name', self::TEXT_DOMAIN ), | |
'menu_name' => __( self::TAX_MENU_NAME, self::TEXT_DOMAIN ), | |
'all_items' => __( 'All ' . self::TAX_NAME_PLURAL, self::TEXT_DOMAIN ), | |
'edit_item' => __( 'Edit ' . self::TAX_NAME_SINGLE, self::TEXT_DOMAIN ), | |
'view_item' => __( 'View ' . self::TAX_NAME_SINGLE, self::TEXT_DOMAIN ), | |
'update_item' => __( 'Update ' . self::TAX_NAME_SINGLE, self::TEXT_DOMAIN ), | |
'add_new_item' => __( 'Add new ' . self::TAX_NAME_SINGLE, self::TEXT_DOMAIN ), | |
'new_item_name' => __( 'New ' . self::TAX_NAME_SINGLE . ' name', self::TEXT_DOMAIN ), | |
'parent_item' => __( 'Parent ' . self::TAX_NAME_SINGLE, self::TEXT_DOMAIN ), | |
'parent_item_colon' => __( 'Parent ' . self::TAX_NAME_SINGLE . ':', self::TEXT_DOMAIN ), | |
'search_items' => __( 'Search ' . self::TAX_NAME_PLURAL, self::TEXT_DOMAIN ), | |
'popular_items' => __( 'Popular ' . self::TAX_NAME_PLURAL, self::TEXT_DOMAIN ), | |
'separate_items_with_commas' => __( 'Separate ' . strtolower( self::TAX_NAME_PLURAL ) . ' with commas', self::TEXT_DOMAIN ), | |
'add_or_remove_items' => __( 'Add or remove ' . strtolower( self::TAX_NAME_PLURAL ), self::TEXT_DOMAIN ), | |
'choose_from_most_used' => __( 'Choose from most used ' . strtolower( self::TAX_NAME_PLURAL ), self::TEXT_DOMAIN ), | |
'not_found' => __( 'No ' . strtolower( self::TAX_NAME_PLURAL ) . ' found', self::TEXT_DOMAIN ), | |
); | |
/** | |
* Filters the individual labels for this taxonomy. | |
* | |
* @param array $labels See {@link https://codex.wordpress.org/Function_Reference/register_taxonomy#labels} | |
* for a description of each label available. | |
*/ | |
$labels = apply_filters( "{$func}_labels", $labels ); // Filter name: `register_custom_taxonomy_labels` | |
/** | |
* The rewrite settings for this custom taxonomy. | |
* | |
* @var array | |
*/ | |
$rewrite = array( | |
'slug' => self::TAX_REWRITE_SLUG, | |
'with_front' => true, | |
); | |
/** | |
* Filters the rewrite settings for this taxonomy. | |
* | |
* @param array $rewrite | |
*/ | |
$rewrite = apply_filters( "{$func}_rewrite", $rewrite ); // Filter name: `register_custom_taxonomy_rewrite` | |
/** | |
* Default arguments. | |
* | |
* @var array | |
*/ | |
$args = array( | |
'labels' => $labels, | |
'public' => true, | |
'publicly_queryable' => true, | |
'hierarchical' => true, | |
'show_ui' => true, | |
'show_in_menu' => true, | |
'show_in_nav_menus' => true, | |
'query_var' => true, | |
'rewrite' => $rewrite, | |
'show_admin_column' => true, | |
'show_in_rest' => false, | |
'rest_base' => self::TAX_REWRITE_SLUG, | |
'rest_controller_class' => 'WP_REST_Terms_Controller', | |
'show_in_quick_edit' => true, | |
); | |
/** | |
* Filters the individual arguments for this taxonomy. | |
* | |
* @param array $args See {@link https://codex.wordpress.org/Function_Reference/register_taxonomy#Arguments} | |
* for a description of each label available. | |
*/ | |
$args = apply_filters( "{$func}_args", $args ); // Filter name: `register_custom_taxonomy_args` | |
// Finally, register the new taxonomy. | |
register_taxonomy( self::TAXONOMY_SLUG, self::$post_types, $args ); | |
} | |
/** | |
* Register custom post type. | |
* | |
* @since DJ_Snippets 0.0.1 | |
*/ | |
public static function register_custom_post_type() { | |
if ( post_type_exists( self::POST_TYPE_SLUG ) ) { | |
return; | |
} | |
/** | |
* Filter prefix. | |
* | |
* @var string | |
*/ | |
$func = __FUNCTION__; | |
/** | |
* Default label settings for this custom post type. | |
* | |
* @var array | |
*/ | |
$labels = array( | |
'name' => _x( self::PT_NAME_PLURAL, 'Post Type General Name', self::TEXT_DOMAIN ), | |
'singular_name' => _x( self::PT_NAME_SINGLE, 'Post Type Singular Name', self::TEXT_DOMAIN ), | |
'menu_name' => __( self::PT_MENU_NAME, self::TEXT_DOMAIN ), | |
'all_items' => __( 'All ' . self::PT_NAME_PLURAL, self::TEXT_DOMAIN ), | |
'add_new' => __( 'Add new ' . self::PT_NAME_SINGLE, self::TEXT_DOMAIN ), | |
'add_new_item' => __( 'Add new ' . self::PT_NAME_SINGLE, self::TEXT_DOMAIN ), | |
'edit_item' => __( 'Edit ' . self::PT_NAME_SINGLE, self::TEXT_DOMAIN ), | |
'new_item' => __( 'New ' . self::PT_NAME_SINGLE, self::TEXT_DOMAIN ), | |
'view_item' => __( 'View ' . self::PT_NAME_SINGLE, self::TEXT_DOMAIN ), | |
'view_items' => __( 'View ' . self::PT_NAME_PLURAL, self::TEXT_DOMAIN ), | |
'search_items' => __( 'Search ' . self::PT_NAME_PLURAL, self::TEXT_DOMAIN ), | |
'not_found' => __( self::PT_NAME_PLURAL . ' not found', self::TEXT_DOMAIN ), | |
'not_found_in_trash' => __( self::PT_NAME_PLURAL . ' not found in trash', self::TEXT_DOMAIN ), | |
'featured_image' => __( 'Featured image', self::TEXT_DOMAIN ), | |
'set_featured_image' => __( 'Set featured image', self::TEXT_DOMAIN ), | |
'remove_featured_image' => __( 'Remove featured image', self::TEXT_DOMAIN ), | |
'use_featured_image' => __( 'Use featured image', self::TEXT_DOMAIN ), | |
'archives' => __( 'Archives', self::TEXT_DOMAIN ), | |
'insert_into_item' => __( 'Insert into ' . strtolower( self::PT_NAME_SINGLE ), self::TEXT_DOMAIN ), | |
'uploaded_to_this_item' => __( 'Uploaded to this ' . strtolower( self::PT_NAME_SINGLE ), self::TEXT_DOMAIN ), | |
'filter_items_list' => __( 'Filter ' . strtolower( self::PT_NAME_PLURAL ) . ' list', self::TEXT_DOMAIN ), | |
'items_list_navigation' => __( self::PT_NAME_PLURAL . ' list navigation', self::TEXT_DOMAIN ), | |
'items_list' => __( self::PT_NAME_PLURAL . ' list', self::TEXT_DOMAIN ), | |
'attributes' => __( self::PT_NAME_SINGLE . ' attributes', self::TEXT_DOMAIN ), | |
); | |
/** | |
* Filter the label settings for this custom post type. | |
* | |
* @param array $labels See {@link https://codex.wordpress.org/Function_Reference/register_post_type#labels} | |
* for a description of each label available. | |
*/ | |
$labels = apply_filters( "{$func}_labels", $labels ); // Filter name: `register_custom_post_type_labels` | |
/** | |
* See {@link https://codex.wordpress.org/Function_Reference/register_post_type#rewrite} | |
* for a complete description of the rewrite settings available. | |
* | |
* @var array | |
*/ | |
$rewrite = array( | |
'slug' => self::PT_REWRITE_SLUG, | |
'with_front' => true, | |
); | |
/** | |
* Filters the rewrite settings for this post type. See {@link https://codex.wordpress.org/Function_Reference/register_post_type#rewrite} | |
* for a complete description of the rewrite settings available. | |
* | |
* @param array $rewrite | |
*/ | |
$rewrite = apply_filters( "{$func}_rewrite", $rewrite ); // Filter name: `register_custom_post_type_rewrite` | |
/** | |
* The features this post type supports. | |
* | |
* @var array | |
*/ | |
$supports = array( 'title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'page-attributes' ); | |
/** | |
* Filter the features supported for this post type. | |
* | |
* @var array $supports See {@link https://codex.wordpress.org/Function_Reference/register_post_type#supports} | |
* for a description of each label available. | |
*/ | |
$supports = apply_filters( "{$func}_supports", $supports ); // Filter name: `register_custom_post_type_supports` | |
/** | |
* The default arguments for this post type. | |
* | |
* @var array $args See {@link https://codex.wordpress.org/Function_Reference/register_post_type#Arguments} | |
* for a description of each label available. | |
*/ | |
$args = array( | |
'labels' => $labels, | |
'description' => __( self::PT_DESCRIPTION, self::TEXT_DOMAIN ), | |
'public' => true, | |
'publicly_queryable' => true, | |
'show_ui' => true, | |
'delete_with_user' => false, | |
'show_in_rest' => false, | |
'rest_base' => self::PT_REWRITE_SLUG, | |
'rest_controller_class' => 'WP_REST_Posts_Controller', | |
'has_archive' => false, | |
'show_in_menu' => true, | |
'show_in_nav_menus' => true, | |
'exclude_from_search' => false, | |
'capability_type' => 'post', | |
'map_meta_cap' => true, | |
'hierarchical' => false, | |
'rewrite' => $rewrite, | |
'query_var' => true, | |
'menu_icon' => ( empty( self::PT_DASHICON ) ? '' : 'dashicons-' . self::PT_DASHICON ), | |
'supports' => $supports, | |
'taxonomies' => self::$taxonomies, | |
); | |
/** | |
* Filters the individual args for this post type. | |
* | |
* @param array $labels See {@link https://codex.wordpress.org/Function_Reference/register_post_type#Arguments} | |
* for a description of each label available. | |
*/ | |
$args = apply_filters( "{$func}_args", $args ); // Filter name: `register_custom_post_type_args` | |
// Finally, register the new custom post type. | |
register_post_type( self::POST_TYPE_SLUG, $args ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment