Last active
August 29, 2015 14:16
-
-
Save WolfieZero/24f6c54206b19ef23926 to your computer and use it in GitHub Desktop.
Allows you to quickly add multiple custom post types and taxonomies to WordPress via a plugin
This file contains 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 | |
/** | |
* Plugin Name: {NAMESPACE}: Post Types | |
* Description: Add custom post types | |
* Author: {AUTHOR} | |
* Author URI: {AUTHOR_URL} | |
* Version: 1.0.0 | |
*/ | |
add_action( 'init', array('{NAMESPACE}_PostTypes', 'init') ); | |
class {NAMESPACE}_PostTypes { | |
/** | |
* Multiple post_types | |
* | |
* @var array | |
*/ | |
private static $post_types = array( | |
'' => array( | |
'post_type' => '', | |
'name' => '', | |
'name_single' => '', | |
'menu_icon' => '' // https://developer.wordpress.org/resource/dashicons/ | |
) | |
); | |
/** | |
* Multiple taxonomies | |
* | |
* @var array | |
*/ | |
private static $taxonomies = array( | |
'' => array( | |
'taxonomy' => '', | |
'for_post_type' => '', | |
'name' => '', | |
'name_single' => '', | |
'hierarchical' => false, | |
'show_ui' => true, | |
'show_admin_column' => true, | |
'query_var' => true, | |
'rewrite' => array('slug' => ''), | |
) | |
); | |
/** | |
* Initial functions to fire | |
* | |
* @return void | |
*/ | |
function init() | |
{ | |
{NAMESPACE}_PostTypes::create_post_types(); | |
{NAMESPACE}_PostTypes::create_taxonomies(); | |
} | |
/** | |
* Creates the custom post types based on the `$post_types` class variable | |
* | |
* @return void | |
*/ | |
function create_post_types() | |
{ | |
$post_types = {NAMESPACE}_PostTypes::$post_types; | |
foreach ($post_types as $post_type) { | |
$labels = array( | |
'name' => $post_type['name'], | |
'singular_name' => $post_type['name_single'], | |
'menu_name' => $post_type['name'], | |
'name_admin_bar' => $post_type['name_single'], | |
'add_new' => __( 'Add New' ), | |
'add_new_item' => __( 'Add New' ) . ' ' . $post_type['name_single'], | |
'new_item' => __( 'New' ) . ' ' . $post_type['name_single'], | |
'edit_item' => __( 'Edit' ) . ' ' . $post_type['name_single'], | |
'view_item' => __( 'View' ) . ' ' . $post_type['name_single'], | |
'all_items' => __( 'All' ) . ' ' . $post_type['name'], | |
'search_items' => __( 'Search' ) . ' ' . $post_type['name'], | |
'parent_item_colon' => __( 'Parent' ) . ' ' . $post_type['name_single'] . ':', | |
'not_found' => __( 'None found.' ), | |
'not_found_in_trash' => __( 'None in Trash.' ) | |
); | |
register_post_type( $post_type['post_type'], | |
array( | |
'labels' => $labels, | |
'public' => true, | |
'has_archive' => false, | |
'rewrite' => array('slug' => $post_type['post_type']), | |
'menu_icon' => $config['menu_icon'] | |
) | |
); | |
} | |
} | |
/** | |
* Creates taxonomies based on the `$taxonomies` class variable | |
* | |
* @return void | |
*/ | |
public static function create_taxonomies() | |
{ | |
$taxonomies = {NAMESPACE}_PostTypes::$taxonomies; | |
foreach ($taxonomies as $taxonomy) { | |
$labels = array( | |
'name' => $taxonomy['name'], | |
'singular_name' => $taxonomy['name_single'], | |
'menu_name' => $taxonomy['name'], | |
'search_items' => __( 'Search' ) . ' ' . $taxonomy['name'], | |
'all_items' => __( 'All' ) . ' ' . $taxonomy['name'], | |
'edit_item' => __( 'Edit' ) . ' ' . $taxonomy['name_single'], | |
'update_item' => __( 'Update' ) . ' ' . $taxonomy['name_single'], | |
'add_new_item' => __( 'Add New' ), | |
'new_item_name' => __( 'Add New' ) . ' ' . $taxonomy['name_single'], | |
); | |
$options = array( | |
'hierarchical' => $taxonomy['hierarchical'], | |
'show_ui' => $taxonomy['show_ui'], | |
'show_admin_column' => $taxonomy['show_admin_column'], | |
'query_var' => $taxonomy['query_var'], | |
'rewrite' => $taxonomy['rewrite'] | |
); | |
if ($taxonomy['hierarchical']) { | |
$labels['parent_item'] = __( 'Parent' ) . ' ' . $taxonomy['name_single']; | |
$labels['parent_item_colon'] = __( 'Parent' ) . ' ' . $taxonomy['name_single'] . ':'; | |
} else { | |
$labels['popular_items'] = __( 'Popular' ) . ' ' . $taxonomy['name']; | |
$labels['separate_items_with_commas'] = __( 'Separate with commas' ); | |
$labels['add_or_remove_items'] = __( 'Add or remove' ) . ' ' . $taxonomy['name']; | |
$labels['choose_from_most_used'] = __( 'Choose from the most used' ) . ' ' . $taxonomy['name']; | |
$options['update_count_callback'] = '_update_post_term_count'; | |
} | |
$options['labels'] = $labels; | |
register_taxonomy($taxonomy['taxonomy'], $taxonomy['for_post_type'], $options); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment