Skip to content

Instantly share code, notes, and snippets.

@davatron5000
Created February 28, 2011 23:06
Show Gist options
  • Save davatron5000/848232 to your computer and use it in GitHub Desktop.
Save davatron5000/848232 to your computer and use it in GitHub Desktop.
WordPress Custom Post Type Boilerplate (e.g. Videos)
<?php
/*
Plugin Name: Videos
Plugin URI:
Author: Dave Rupert
Author URI: http://www.daverupert.com
Description: A custom post type that adds videos and custom taxonomies.
Version: 1.0
*/
# This is a Custom Post Type Boilerplate.
# In order to change the post type, change the initial call, the Class name, the variables, and the first function name.
# Also change the Taxonomies and the Custom Metabox as necessary.
new VideoPostType; // Initial call
class VideoPostType {
var $single = "Video"; // this represents the singular name of the post type
var $plural = "Videos"; // this represents the plural name of the post type
var $type = "video"; // this is the actual type
# credit: http://w3prodigy.com/behind-wordpress/php-classes-wordpress-plugin/
function VideoPostType()
{
$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,
'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() {
register_taxonomy(
'topic',
array($this->type),
array(
'hierarchical' => true,
'labels' => array(
'name' => __( 'Topic' ),
'singular_name' => __( 'Topics' ),
'all_items' => __( 'All Topics' ),
'add_new_item' => __( 'Add Topic' )
),
'public' => true,
'query_var' => true,
'rewrite' => array(
'slug' => 'topic'
),
)
);
}
# @credit: http://wptheming.com/2010/08/custom-metabox-for-post-type/
function add_custom_metaboxes() {
add_meta_box( 'metabox1', 'Details', array( &$this, 'metabox1'), $this->type, 'normal', 'high' );
}
# @credit: http://wptheming.com/2010/08/custom-metabox-for-post-type/
function metabox1() {
global $post;
extract(get_post_custom($post->ID));
wp_nonce_field( plugin_basename(__FILE__), 'noncename' ); // Use nonce for verification
?>
<p>
<label for="data[short_title]">Short Title</label>
<input type="text" id= "data[short_title]" name="data[short_title]" value="<?php echo $short_title[0] ?>" placeholder="5-6 Word Title" size="100" />
</p>
<p>
<label for="data[video_url]">Video URL</label>
<input type="url" id= "data[video_url]" name="data[video_url]" value="<?php echo $video_url[0] ?>" placeholder="http://example.com/path/to/my/video.mov" size="100" />
</p>
<style type="text/css">
#metabox1 label {
width: 120px;
display: -moz-inline-stack;
display: inline-block;
zoom: 1;
*display: inline;
}
div.tabs-panel {
height: 80px!important;
}
</style>
<?php
}
function save_postdata(){
if ( empty($_POST) || $_POST['post_type'] !== $this->type || !wp_verify_nonce( $_POST['noncename'], plugin_basename(__FILE__) )) {
return $post_id;
}
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
return $post_id;
// Check permissions
if ( 'page' == $_POST['post_type'] ) {
if ( !current_user_can( 'edit_page', $post_id ) )
return $post_id;
} else {
if ( !current_user_can( 'edit_post', $post_id ) )
return $post_id;
}
if($_POST['post_type'] == $this->type) {
global $post;
foreach($_POST['data'] as $key => $val) {
update_post_meta($post->ID, $key, $val);
}
}
}
}
@reinnovating
Copy link

reinnovating commented Nov 12, 2017

Hey Dave,
I hope you can help. I know you created this long ago, but I LOVE this boilerplate video plugin. I've created a relative url path so I can grab videos we host on Amazon s3 with a simple file title.

I added an array to the meta box function to include posts (below), so the meta boxes show up on the post edit page, however, they are not saving. I assumed it was nonce issue but could be wrong.

' add_meta_box( 'metabox1', 'Details', array( &$this, 'metabox1'), array($this->type, 'post'), 'normal', 'high' );
}
'

Here's the whole plugin. Pretty please lend your thoughts or guidance.
https://gist.github.com/reinnovating/dfbc9f4767b22edb996b3657aeb156e6

@BaldwinJackson
Copy link

Hey Mate,

You have done a great job for creating this custom plugin for videos and custom taxonomies. I am looking for a way to create a custom post type with restriction on user accounts like:
Allowing students to take quiz and video lectures but doesn't allow to overwrite anything on dashboard.

Whereas
Teachers can upload MCQS base quiz and video lectures,

I have created the WordPress custom post type but I am not sure how we can restrict specify user from doing any action like overriding the content.

I hope you can suggest me any valid way to acheieve this task. Thanks in advance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment