Skip to content

Instantly share code, notes, and snippets.

@vienhoang
Created August 11, 2014 16:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vienhoang/561f37893edaf6c81602 to your computer and use it in GitHub Desktop.
Save vienhoang/561f37893edaf6c81602 to your computer and use it in GitHub Desktop.
WordPress: Custom post type
<?php
/*
Plugin Name: Movies Post Type
Plugin URI: http://vienhoang.com
Description: Offers a method for managing a movie collection.
Version: 1.0
Author: Vien Hoang
Author URI: http://vienhoang.com
License: GPL2
*/
class VH_Movies_Post_Type {
public function __construct() {
$this->register_post_type();
$this->taxonomies();
$this->metaboxes();
}
public function register_post_type() {
$args = array(
'labels' => array(
'name' => 'Movies',
'singular_name' => 'Movie',
'add_new' => 'Add New Movie',
'add_new_item' => 'Add New Movie',
'edit_item' => 'Edit Item',
'new_item' => 'Add New Item',
'view_item' => 'View Movie',
'search_items' => 'Search Movies',
'not_found' => 'No Movies Found',
'not_found_in_trash' => 'No Movies Found in Trash'
),
// How we query
'query_var' => 'movies',
'rewrite' => array(
'slug' => 'movies'
),
// Visual in back end
'public' => true,
'menu_position' => 25,
'menu_icon' => admin_url() . 'images/media-button-video.gif',
'supports' => array(
'title',
'thumbnail',
'excerpt'
)
);
register_post_type( 'vh_movie', $args );
}
public function taxonomies() {
$taxonomies = array();
$taxonomies['genre'] = array(
// Can have a parent
'hierarchical' => true,
'query_var' => 'movie_genre',
'rewrite' => array(
'slug' => 'movies/genre'
),
'labels' => array(
'name' => 'Genre',
'singular_name' => 'Genre',
'edit_item' => 'Edit Genre',
'update_item' => 'Update Genre',
'add_new_item' => 'Add Genre',
'new_item_name' => 'Add New Genre',
'all_items' => 'All Genres',
'search_items' => 'Search Genres',
'popular_items' => 'Popular Genres',
'separate_items_with_comments' => 'Seperate genres with commas',
'add_or_remove_items' => 'Add or remove genres',
'choose_from_most_used' => 'Choose from the most used genres'
)
);
$taxonomies['studio'] = array(
// Can have a parent
'hierarchical' => true,
'query_var' => 'movie_studio',
'rewrite' => array(
'slug' => 'movies/studios'
),
'labels' => array(
'name' => 'Studio',
'singular_name' => 'Studio',
'edit_item' => 'Edit Studio',
'update_item' => 'Update Studio',
'add_new_item' => 'Add Studio',
'new_item_name' => 'Add New Studio',
'all_items' => 'All Studios',
'search_items' => 'Search Studios',
'popular_items' => 'Popular Studios',
'separate_items_with_comments' => 'Seperate studios with commas',
'add_or_remove_items' => 'Add or remove studios',
'choose_from_most_used' => 'Choose from the most used studios'
)
);
$this->register_all_taxonomies( $taxonomies );
}
public function register_all_taxonomies( $taxonomies ) {
// $name = genre, $arr = array
foreach ($taxonomies as $name => $arr) {
register_taxonomy( $name, array('vh_movie'), $arr );
}
}
public function metaboxes() {
add_action( 'add_meta_boxes', function() {
// css id, title, cb func, page, priority, cb func arguments
add_meta_box( 'vh_movie_length', 'Movie length', 'movie_length', 'vh_movie' );
} );
function movie_length( $post ) {
// Get the movie length stored in post meta, 3rd param true to return a single value
$length = get_post_meta( $post->ID, 'vh_movie_length', true);
?>
<p>
<label for="vh_movie_length">Length: </label>
<input type="text" class="widefat" name="vh_movie_length" id="vh_movie_length" value="<?php echo esc_attr( $length );?>" />
</p>
<?php
}
// Save the movie length
add_action( 'save_post', function( $id ) {
if ( isset($_POST['vh_movie_length']) ) {
update_post_meta(
$id,
'vh_movie_length',
strip_tags($_POST['vh_movie_length'])
);
}
} );
}
}
// Create a new instance of this class with WP init
add_action( 'init', function() {
new VH_Movies_Post_Type();
include dirname( __FILE__ ) . '/movies_post_type_shortcode.php';
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment