-
-
Save craighooghiem/9db1887962eaaa89977ff2f236d06079 to your computer and use it in GitHub Desktop.
WordPress Custom Post Type Plugin Template
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: CustomType | |
Plugin URI: | |
Author: Craig Hooghiem | |
Author URI: http://www.craighooghiem.com | |
Description: A custom post type that adds CustomType and custom taxonomies. | |
Version: 1.0 | |
*/ | |
new CustomType; // Initial call | |
class CustomType { | |
var $single = "CustomType"; // this represents the singular name of the post type | |
var $plural = "CustomTypes"; // this represents the plural name of the post type | |
var $type = "customtype"; // this is the actual type | |
# credit: http://w3prodigy.com/behind-wordpress/php-classes-wordpress-plugin/ | |
function CustomType() | |
{ | |
$this->__construct(); | |
} | |
function __construct() | |
{ | |
# Place your add_actions and add_filters here | |
add_action( 'init', array( &$this, 'init' ) ); | |
add_action('init', array(&$this, 'add_post_type')); | |
# Add image support | |
add_theme_support('post-thumbnails', array( $this->type ) ); | |
add_image_size(strtolower($this->plural).'-thumb-s', 220, 160, true); | |
add_image_size(strtolower($this->plural).'-thumb-m', 300, 180, true); | |
# Add Post Type to Search | |
add_filter('pre_get_posts', array( &$this, 'query_post_type') ); | |
# Add Custom Taxonomies | |
add_action( 'init', array( &$this, 'add_taxonomies'), 0 ); | |
# Add meta box | |
add_action('add_meta_boxes', array( &$this, 'add_custom_metaboxes') ); | |
# Save entered data | |
add_action('save_post', array( &$this, 'save_postdata') ); | |
} | |
# @credit: http://www.wpinsideout.com/advanced-custom-post-types-php-class-integration | |
function init($options = null){ | |
if($options) { | |
foreach($options as $key => $value){ | |
$this->$key = $value; | |
} | |
} | |
} | |
# @credit: http://www.wpinsideout.com/advanced-custom-post-types-php-class-integration | |
function add_post_type(){ | |
$labels = array( | |
'name' => _x($this->plural, 'post type general name'), | |
'singular_name' => _x($this->single, 'post type singular name'), | |
'add_new' => _x('Add ' . $this->single, $this->single), | |
'add_new_item' => __('Add New ' . $this->single), | |
'edit_item' => __('Edit ' . $this->single), | |
'new_item' => __('New ' . $this->single), | |
'view_item' => __('View ' . $this->single), | |
'search_items' => __('Search ' . $this->plural), | |
'not_found' => __('No ' . $this->plural . ' Found'), | |
'not_found_in_trash' => __('No ' . $this->plural . ' found in Trash'), | |
'parent_item_colon' => '' | |
); | |
$options = array( | |
'labels' => $labels, | |
'public' => true, | |
'publicly_queryable' => true, | |
'show_ui' => true, | |
'query_var' => true, | |
'rewrite' => array('slug' => strtolower($this->plural)), | |
'capability_type' => 'post', | |
'hierarchical' => false, | |
'has_archive' => true, | |
'menu_position' => null, | |
'menu_icon' => 'dashicons-products', | |
'supports' => array( | |
'title', | |
'editor', | |
# 'author', | |
'thumbnail', | |
# 'excerpt', | |
'comments' | |
), | |
); | |
register_post_type($this->type, $options); | |
} | |
function query_post_type($query) { | |
if(is_category() || is_tag()) { | |
$post_type = get_query_var('post_type'); | |
if($post_type) { | |
$post_type = $post_type; | |
} else { | |
$post_type = array($this->type); // replace cpt to your custom post type | |
} | |
$query->set('post_type',$post_type); | |
return $query; | |
} | |
} | |
function add_taxonomies() { | |
$labels = array( | |
'name' => __( 'Include on Page', 'hmc' ), | |
'singular_name' => __( 'Include on Page', 'hmc' ), | |
'search_items' => __( 'Search Include on Page', 'hmc' ), | |
'popular_items' => __( 'Popular Include on Page', 'hmc' ), | |
'all_items' => __( 'All Include on Page', 'hmc' ), | |
'parent_item' => __( 'Parent Include on Page', 'hmc' ), | |
'parent_item_colon' => __( 'Parent Include on Page:', 'hmc' ), | |
'edit_item' => __( 'Edit Include on Page', 'hmc' ), | |
'update_item' => __( 'Update Include on Page', 'hmc' ), | |
'add_new_item' => __( 'Add New Include on Page', 'hmc' ), | |
'new_item_name' => __( 'New Include on Page', 'hmc' ), | |
'separate_items_with_commas' => __( 'Separate Include on Page with commas', 'hmc' ), | |
'add_or_remove_items' => __( 'Add or remove Include on Page', 'hmc' ), | |
'choose_from_most_used' => __( 'Choose from the most used Include on Page', 'hmc' ), | |
'menu_name' => __( 'Include on Page', 'hmc' ), | |
); | |
$args = array( | |
'labels' => $labels, | |
'public' => false, // set to false so your tax terms aren't shown on the front end of your site | |
'show_in_nav_menus' => false, | |
'show_ui' => true, // set to true so these taxonomy terms appear on post edit screen | |
'show_tagcloud' => false, | |
'show_admin_column' => false, | |
'hierarchical' => false, | |
'rewrite' => false, | |
'query_var' => true | |
); | |
register_taxonomy( 'include_staff_on_page', array( 'release' ), $args ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment