Skip to content

Instantly share code, notes, and snippets.

@reinnovating
Last active November 14, 2017 14:05
Show Gist options
  • Save reinnovating/dfbc9f4767b22edb996b3657aeb156e6 to your computer and use it in GitHub Desktop.
Save reinnovating/dfbc9f4767b22edb996b3657aeb156e6 to your computer and use it in GitHub Desktop.
<?php
/*
Plugin Name: RE:INNOVATING Videos - Custom Post Type
Plugin URI: https://reinnovating.studio
Author: RE:INNOVATING
Author URI: https://reinnovating.studio
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 = "Stream"; // this represents the plural name of the post type
var $type = "video";
var $postTypes = array( 'page' => 'page', 'video', 'post' => 'post' );
# 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', 1920, 1080, true);
add_image_size(strtolower($this->plural).'-thumb-m', 1280, 720, true);
# Add Post Type to Search
add_filter('pre_get_posts', array( &$this, 'query_post_type') );
# Add Custom Taxonomies
add_action( 'init', array( &$this, 'restudio_video_add_taxonomies'), 0 );
# Add meta box
add_action('add_meta_boxes', array( &$this, 'restudio_video_metaboxes') );
# Save entered data
add_action('save_post', array( &$this, 'restudio_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' => true,
'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->video); // replace cpt to your custom post type
}
$query->set('post_type',$post_type);
return $query;
}
}
function restudio_video_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 restudio_video_metaboxes() {
add_meta_box('metabox1', 'Video', array( &$this, 'metabox1'), $postTypes, '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__), 'restudio_video_nonce' ); // 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="50" />
</p>
<p>
<label for="data[video_url_mp4]">Video MP4</label>
<input type="text" id= "data[video_url_mp4]" name="data[video_url_mp4]" value="<?php echo $video_url_mp4[0] ?>" placeholder="just-video-file-name" size="50" />
</p>
<p>
<label for="data[video_url_webm]">Video WebM</label>
<input type="text" id= "data[video_url_webm]" name="data[video_url_webm]" value="<?php echo $video_url_webm[0] ?>" placeholder="just-video-file-name" size="50" />
</p>
<style type="text/css">
#metabox1 label {
width: 100%;
display: -moz-inline-stack;
display: inline-block;
zoom: 1;
*display: inline;
}
div.tabs-panel {
height: 80px!important;
}
</style>
<?php
}
function restudio_save_postdata(){
if ( empty($_POST) || $_POST['post_type'] !== $this->type || !wp_verify_nonce( $_POST['restudio_video_nonce'], 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);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment