Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /******
- * SD_Register_Post_Type class
- *
- * @author Matt Wiebe
- * @link http://somadesign.ca
- *
- * @param string $post_type The post type to register
- * @param array $args The arguments to pass into @link register_post_type(). Some defaults provided to ensure the UI is available.
- * @param string $custom_plural The plural name to be used in rewriting (http://yourdomain.com/custom_plural/ ). If left off, an "s" will be appended to your post type, which will break some words. (person, box, ox. Oh, English.)
- ******/
- if ( ! class_exists('SD_Register_Post_Type') ) {
- class SD_Register_Post_Type {
- private $post_type;
- private $post_slug;
- private $args;
- private $defaults = array(
- 'show_ui' => true,
- 'public' => true,
- 'supports' => array('title', 'editor', 'thumbnail', 'custom-fields', 'comments')
- );
- public function __construct( $post_type = null, $args=array(), $custom_plural = false ) {
- if ( $post_type ) {
- $this->post_type = $post_type;
- $this->args = wp_parse_args($args, $this->defaults);
- // Uppercase the post type for label if there isn't one
- if ( ! $this->args['label'] ) {
- $this->args['label'] = ucwords($post_type);
- }
- $this->post_slug = ( $custom_plural ) ? $custom_plural : $post_type . 's';
- $this->defaults['permalink_epmask'] = $this->post_slug;
- $this->add_actions();
- $this->add_filters();
- }
- }
- public function add_actions() {
- add_action( 'init', array($this, 'register_post_type'));
- add_action('template_redirect', array($this, 'context_fixer') );
- }
- public function add_filters() {
- add_filter( 'generate_rewrite_rules', array($this, 'add_rewrite_rules') );
- add_filter( 'template_include', array($this, 'template_include') );
- add_filter( 'body_class', array($this, 'body_classes') );
- }
- public function context_fixer() {
- if ( get_query_var( 'post_type' ) == $this->post_type ) {
- global $wp_query;
- $wp_query->is_home = false;
- }
- }
- public function add_rewrite_rules( $wp_rewrite ) {
- $new_rules = array();
- $new_rules[$this->post_slug . '/page/?$'] = 'archive.php?post_type=' . $this->post_type;
- $new_rules[$this->post_slug . '/page/?([0-9]{1,})/?$'] = 'index.php?post_type=' . $this->post_type . '&paged=' . $wp_rewrite->preg_index(1);
- $new_rules[$this->post_slug . '/(feed|rdf|rss|rss2|atom)/?$'] = 'index.php?post_type=' . $this->post_type . '&feed=' . $wp_rewrite->preg_index(1);
- $new_rules[$this->post_slug . '/?$'] = 'index.php?post_type=' . $this->post_type;
- $wp_rewrite->rules = array_merge($new_rules, $wp_rewrite->rules);
- return $wp_rewrite;
- }
- public function register_post_type() {
- register_post_type( $this->post_type, $this->args );
- }
- public function template_include( $template ) {
- if ( get_query_var('post_type') == $this->post_type ) {
- if ( is_single() ) {
- if ( $single = locate_template( array( $this->post_type.'/single.php') ) )
- return $single;
- }
- if ( is_paged() ) {
- if ($paged = locate_template( array( $this->post_type.'/archive.php') ) )
- return $paged;
- }
- else { // loop
- return locate_template( array(
- $this->post_type . '/index.php',
- $this->post_type . '.php',
- 'index.php'
- ));
- }
- }
- return $template;
- }
- public function body_classes( $c ) {
- if ( get_query_var('post_type') === $this->post_type ) {
- $c[] = $this->post_type;
- $c[] = 'type-' . $this->post_type;
- }
- return $c;
- }
- } // end SD_Register_Post_Type class
- /**
- * A helper function for the SD_Register_Post_Type class. Because typing "new" is hard.
- *
- * @author Matt Wiebe
- * @link http://somadesign.ca
- *
- * @uses SD_Register_Post_Type class
- * @param string $post_type The post type to register
- * @param array $args The arguments to pass into @link register_post_type(). Some defaults provided to ensure the UI is available.
- * @param string $custom_plural The plural name to be used in rewriting (http://yourdomain.com/custom_plural/ ). If left off, an "s" will be appended to your post type, which will break some words. (person, box, ox. Oh, English.)
- **/
- if ( ! function_exists( 'sd_register_post_type' ) && class_exists( 'SD_Register_Post_Type' ) ) {
- function sd_register_post_type( $post_type = null, $args=array(), $custom_plural = false ) {
- $custom_post = new SD_Register_Post_Type( $post_type, $args, $custom_plural );
- }
- }
- }
- sd_register_post_type('photogallery',
- array(
- 'labels' =>array(
- 'name' => _x('Photogalleries', 'post type general name'),
- 'singular_name' => _x('Photogallery', 'post type singular name'),
- 'add_new' => _x('Add New', 'photogallery'),
- 'add_new_item' => __('Add New photogallery'),
- 'edit_item' => __('Edit photogallery'),
- 'edit' => _x('Edit', 'photogallery'),
- 'new_item' => __('New Photogallery'),
- 'view_item' => __('View photogallery'),
- 'search_items' => __('Search Photogalleries'),
- 'not_found' => __('No photogalleries found'),
- 'not_found_in_trash' => __('No photogalleries found in Trash'),
- 'view' => __('View Photogallery'),
- 'parent_item_colon' => ''
- ),
- 'public' => true,
- 'publicly_queryable' => true,
- 'show_ui' => true,
- 'query_var' => true,
- 'rewrite' => true,
- 'capability_type' => 'photogallery',
- 'hierarchical' => true,
- 'menu_position' => null,
- '_builtin' => false,
- 'exclude_from_search' => 'false',
- 'menu_icon' => get_stylesheet_directory_uri() . '/images/home-16.gif',
- 'supports' => array('title','editor','thumbnail','comments','custom-fields'),
- ),'photogalleries'
- );
- add_action( 'init', 'create_photogallery_taxonomies', 0 );
- //create two taxonomies, Photogallery Categories and Photogallery Tags for the post type "photogallery"
- function create_photogallery_taxonomies()
- {
- // Add new taxonomy, make it hierarchical (like categories)
- $labels = array(
- 'name' => _x( 'Photogallery Categories', 'taxonomy general name' ),
- 'singular_name' => _x( 'Photogallery Category', 'taxonomy singular name' ),
- 'search_items' => __( 'Search Photogallery Categories' ),
- 'all_items' => __( 'All Photogallery Categories' ),
- 'parent_item' => __( 'Parent Photogallery Categories' ),
- 'parent_item_colon' => __( 'Parent Photogallery Category:' ),
- 'edit_item' => __( 'Edit Photogallery Category' ),
- 'update_item' => __( 'Update Photogallery Category' ),
- 'add_new_item' => __( 'Add New Photogallery Category' ),
- 'new_item_name' => __( 'New Photogallery Category Name' ),
- );
- register_taxonomy('gallerycategories',array('photogallery'), array(
- 'hierarchical' => true,
- 'labels' => $labels,
- 'show_ui' => true,
- 'query_var' => true,
- 'rewrite' => array( 'slug' => 'gallerycategories' ),
- ));
- // Add new taxonomy, make it non-hierarchical
- $labels = array(
- 'name' => _x( 'Photogallery Tags', 'taxonomy general name' ),
- 'singular_name' => _x( 'Photogallery Tag', 'taxonomy singular name' ),
- 'search_items' => __( 'Search Photogallery Tags' ),
- 'all_items' => __( 'All Photogallery Tags' ),
- 'parent_item' => __( 'Parent Photogallery Tags' ),
- 'parent_item_colon' => __( 'Parent Photogallery Tag:' ),
- 'edit_item' => __( 'Edit Photogallery Tag' ),
- 'update_item' => __( 'Update Photogallery Tag' ),
- 'add_new_item' => __( 'Add New Photogallery Tag' ),
- 'new_item_name' => __( 'New Photogallery Tag Name' ),
- );
- register_taxonomy('gallery-tags',array('photogallery'), array(
- 'hierarchical' => false,
- 'labels' => $labels,
- 'show_ui' => true,
- 'query_var' => true,
- 'rewrite' => array( 'slug' => 'gallery-tags' ),
- ));
- }
- /******************************
- Create post_type filters here
- ******************************/
- //add filter to insure the text Book, or book, is displayed when user updates a photogallery
- add_filter('post_updated_messages', 'photogallery_updated_messages');
- function photogallery_updated_messages( $messages ) {
- $messages['photogallery'] = array(
- 0 => '', // Unused. Messages start at index 1.
- 1 => sprintf( __('photogallery updated. <a href="%s">View photogallery</a>'), esc_url( get_permalink($post_ID) ) ),
- 2 => __('Custom field updated.'),
- 3 => __('Custom field deleted.'),
- 4 => __('photogallery updated.'),
- /* translators: %s: date and time of the revision */
- 5 => isset($_GET['revision']) ? sprintf( __('photogallery restored to revision from %s'), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
- 6 => sprintf( __('photogallery published. <a href="%s">View photogallery</a>'), esc_url( get_permalink($post_ID) ) ),
- 7 => __('photogallery saved.'),
- 8 => sprintf( __('photogallery submitted. <a target="_blank" href="%s">Preview photogallery</a>'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
- 9 => sprintf( __('photogallery scheduled for: <strong>%1$s</strong>. <a target="_blank" href="%2$s">Preview photogallery</a>'),
- // translators: Publish box date format, see http://php.net/date
- date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) ), esc_url( get_permalink($post_ID) ) ),
- 10 => sprintf( __('photogallery draft updated. <a target="_blank" href="%s">Preview photogallery</a>'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
- );
- return $messages;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement