Created
January 20, 2013 23:37
-
-
Save esgy/4582599 to your computer and use it in GitHub Desktop.
This file contains hidden or 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: p47 Custom Post Types | |
* Plugin URI: http://point47.com | |
* Description: Demo for custom Post types | |
* Version: 1.0 | |
* Author: Sorin Gitlan | |
* Author URI: http://point47.com | |
*/ | |
class P47_Movies_Post_Type{ | |
public function __construct(){ | |
$this->register_post_type(); | |
$this->taxonomies(); | |
$this->metaboxes(); | |
} | |
/** | |
* | |
* POST TYPES | |
* | |
*/ | |
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' | |
), | |
'query_var' => 'movies', | |
'rewrite' => array( | |
'slug' => '/movies' | |
), | |
'public' => true, | |
'menu_position' => 5, | |
'menu_icon' => admin_url() . 'images/media-button-video.gif', | |
'supports' => array( | |
'title', | |
'thumbnail', | |
'excerpt', | |
//'editor', | |
//'custom-fields', | |
) | |
); | |
register_post_type( 'p47_movie', $args ); | |
} | |
/** | |
* | |
* TAXONOMIES | |
* | |
*/ | |
public function taxonomies(){ | |
$taxonomies = array(); | |
$taxonomies['genre'] = array( | |
'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' => 'Separate Genres With Commas', | |
'add_or_remove_items' => 'Add or Remove Genres', | |
'chose_from_most_used' => 'Chose From Most Used Genres', | |
), | |
); | |
$taxonomies['studio'] = array( | |
'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' => 'Separate Studios With Commas', | |
'add_or_remove_items' => 'Add or Remove Studios', | |
'chose_from_most_used' => 'Chose From Most Used Studios', | |
), | |
); | |
$this->register_all_taxonomies($taxonomies); | |
} | |
public function register_all_taxonomies($taxonomies){ | |
foreach ($taxonomies as $name => $arr) { | |
register_taxonomy( $name, array('p47_movie'), $arr ); | |
} | |
} | |
/** | |
* | |
* META BOXES | |
* | |
*/ | |
public function metaboxes(){ | |
add_action( 'add_meta_boxes', function(){ | |
// css id, title, callback, page | |
add_meta_box( 'p47_movie_length', 'Movie Length', 'movie_length', 'p47_movie'); | |
}); | |
function movie_length($post){ | |
$length = get_post_meta($post->ID, 'p47_movie_length', true); // single value | |
?> | |
<p> | |
<label for="p47_movie_length"> Length: </label> | |
<input type="text" class="widefat" name="p47_movie_length" id="p47_movie_length" value="<?php echo esc_attr( $length ) ; ?>"> | |
</p> | |
<?php | |
} | |
add_action( 'save_post', function( $id ){ | |
if( isset($_POST['p47_movie_length']) ){ | |
update_post_meta( $id, 'p47_movie_length', strip_tags($_POST['p47_movie_length']) ); | |
} | |
}); | |
} | |
} | |
add_action('init', function(){ | |
new P47_Movies_Post_Type(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment