Advertisement
Guest User

Untitled

a guest
Nov 10th, 2010
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.43 KB | None | 0 0
  1. /******
  2.  * SD_Register_Post_Type class
  3.  *
  4.  * @author Matt Wiebe
  5.  * @link http://somadesign.ca
  6.  *
  7.  * @param string $post_type The post type to register
  8.  * @param array $args The arguments to pass into @link register_post_type(). Some defaults provided to ensure the UI is available.
  9.  * @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.)
  10.  ******/
  11.  
  12. if ( ! class_exists('SD_Register_Post_Type') ) {
  13.  
  14.     class SD_Register_Post_Type {
  15.  
  16.         private $post_type;
  17.         private $post_slug;
  18.         private $args;
  19.  
  20.         private $defaults = array(
  21.             'show_ui' => true,
  22.             'public' => true,
  23.             'supports' => array('title', 'editor', 'thumbnail', 'custom-fields', 'comments')
  24.         );
  25.  
  26.         public function __construct( $post_type = null, $args=array(), $custom_plural = false ) {
  27.             if ( $post_type ) {
  28.                 $this->post_type = $post_type;
  29.                 $this->args = wp_parse_args($args, $this->defaults);
  30.                 // Uppercase the post type for label if there isn't one
  31.                 if ( ! $this->args['label'] ) {
  32.                     $this->args['label'] = ucwords($post_type);
  33.                 }
  34.                 $this->post_slug = ( $custom_plural ) ? $custom_plural : $post_type . 's';
  35.                
  36.                 $this->defaults['permalink_epmask'] = $this->post_slug;
  37.  
  38.                 $this->add_actions();
  39.                 $this->add_filters();
  40.             }
  41.         }
  42.        
  43.         public function add_actions() {
  44.             add_action( 'init', array($this, 'register_post_type'));
  45.             add_action('template_redirect', array($this, 'context_fixer') );
  46.         }
  47.  
  48.         public function add_filters() {
  49.  
  50.             add_filter( 'generate_rewrite_rules', array($this, 'add_rewrite_rules') );
  51.             add_filter( 'template_include', array($this, 'template_include') );
  52.             add_filter( 'body_class', array($this, 'body_classes') );
  53.         }
  54.        
  55.         public function context_fixer() {
  56.             if ( get_query_var( 'post_type' ) == $this->post_type ) {
  57.                 global $wp_query;
  58.                 $wp_query->is_home = false;
  59.             }
  60.         }
  61.  
  62.         public function add_rewrite_rules( $wp_rewrite ) {
  63.             $new_rules = array();
  64.             $new_rules[$this->post_slug . '/page/?$'] = 'archive.php?post_type=' . $this->post_type;
  65.             $new_rules[$this->post_slug . '/page/?([0-9]{1,})/?$'] = 'index.php?post_type=' . $this->post_type . '&paged=' . $wp_rewrite->preg_index(1);
  66.             $new_rules[$this->post_slug . '/(feed|rdf|rss|rss2|atom)/?$'] = 'index.php?post_type=' . $this->post_type . '&feed=' . $wp_rewrite->preg_index(1);
  67.             $new_rules[$this->post_slug . '/?$'] = 'index.php?post_type=' . $this->post_type;
  68.  
  69.             $wp_rewrite->rules = array_merge($new_rules, $wp_rewrite->rules);
  70.             return $wp_rewrite;
  71.         }
  72.  
  73.         public function register_post_type() {
  74.             register_post_type( $this->post_type, $this->args );       
  75.         }
  76.  
  77.         public function template_include( $template ) {
  78.             if ( get_query_var('post_type') == $this->post_type ) {
  79.                
  80.                 if ( is_single() ) {
  81.                     if ( $single = locate_template( array( $this->post_type.'/single.php') ) )
  82.                         return $single;
  83.                 }
  84.                 if ( is_paged() ) {
  85.                     if ($paged = locate_template( array( $this->post_type.'/archive.php') ) )
  86.                         return $paged;
  87.                 }
  88.                 else { // loop
  89.                     return locate_template( array(
  90.                         $this->post_type . '/index.php',
  91.                         $this->post_type . '.php',
  92.                         'index.php'
  93.                     ));
  94.                 }
  95.  
  96.             }
  97.             return $template;
  98.         }
  99.  
  100.         public function body_classes( $c ) {
  101.             if ( get_query_var('post_type') === $this->post_type ) {
  102.                 $c[] = $this->post_type;
  103.                 $c[] = 'type-' . $this->post_type;
  104.             }
  105.             return $c;
  106.         }
  107.  
  108.  
  109.     } // end SD_Register_Post_Type class
  110.    
  111.     /**
  112.      * A helper function for the SD_Register_Post_Type class. Because typing "new" is hard.
  113.      *
  114.      * @author Matt Wiebe
  115.      * @link http://somadesign.ca
  116.      *
  117.      * @uses SD_Register_Post_Type class
  118.      * @param string $post_type The post type to register
  119.      * @param array $args The arguments to pass into @link register_post_type(). Some defaults provided to ensure the UI is available.
  120.      * @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.)
  121.      **/
  122.  
  123.     if ( ! function_exists( 'sd_register_post_type' ) && class_exists( 'SD_Register_Post_Type' ) ) {
  124.         function sd_register_post_type( $post_type = null, $args=array(), $custom_plural = false ) {
  125.             $custom_post = new SD_Register_Post_Type( $post_type, $args, $custom_plural );
  126.         }
  127.     }
  128.  
  129. }
  130.  
  131. sd_register_post_type('photogallery',
  132. array(
  133.     'labels' =>array(
  134.         'name' => _x('Photogalleries', 'post type general name'),
  135.         'singular_name' => _x('Photogallery', 'post type singular name'),
  136.         'add_new' => _x('Add New', 'photogallery'),
  137.         'add_new_item' => __('Add New photogallery'),
  138.         'edit_item' => __('Edit photogallery'),
  139.         'edit' => _x('Edit', 'photogallery'),
  140.         'new_item' => __('New Photogallery'),
  141.         'view_item' => __('View photogallery'),
  142.         'search_items' => __('Search Photogalleries'),
  143.         'not_found' =>  __('No photogalleries found'),
  144.         'not_found_in_trash' => __('No photogalleries found in Trash'),
  145.         'view' =>  __('View Photogallery'),
  146.             'parent_item_colon' => ''
  147.             ),
  148.          'public' => true,
  149.          'publicly_queryable' => true,
  150.      'show_ui' => true,
  151.      'query_var' => true,
  152.      'rewrite' => true,
  153.      'capability_type' => 'photogallery',
  154.      'hierarchical' => true,
  155.      'menu_position' => null,
  156.      '_builtin' => false,
  157.      'exclude_from_search' => 'false',
  158.      'menu_icon' => get_stylesheet_directory_uri() . '/images/home-16.gif',
  159.      'supports' => array('title','editor','thumbnail','comments','custom-fields'),
  160.      ),'photogalleries'
  161.      );
  162.  
  163. add_action( 'init', 'create_photogallery_taxonomies', 0 );
  164.  
  165. //create two taxonomies, Photogallery Categories and Photogallery Tags for the post type "photogallery"
  166. function create_photogallery_taxonomies()
  167. {
  168.   // Add new taxonomy, make it hierarchical (like categories)
  169.   $labels = array(
  170.     'name' => _x( 'Photogallery Categories', 'taxonomy general name' ),
  171.     'singular_name' => _x( 'Photogallery Category', 'taxonomy singular name' ),
  172.     'search_items' =>  __( 'Search Photogallery Categories' ),
  173.     'all_items' => __( 'All Photogallery Categories' ),
  174.     'parent_item' => __( 'Parent Photogallery Categories' ),
  175.     'parent_item_colon' => __( 'Parent Photogallery Category:' ),
  176.     'edit_item' => __( 'Edit Photogallery Category' ),
  177.     'update_item' => __( 'Update Photogallery Category' ),
  178.     'add_new_item' => __( 'Add New Photogallery Category' ),
  179.     'new_item_name' => __( 'New Photogallery Category Name' ),
  180.   );    
  181.  
  182.   register_taxonomy('gallerycategories',array('photogallery'), array(
  183.     'hierarchical' => true,
  184.     'labels' => $labels,
  185.     'show_ui' => true,
  186.     'query_var' => true,
  187.     'rewrite' => array( 'slug' => 'gallerycategories' ),
  188.   ));
  189.   // Add new taxonomy, make it non-hierarchical
  190.   $labels = array(
  191.     'name' => _x( 'Photogallery Tags', 'taxonomy general name' ),
  192.     'singular_name' => _x( 'Photogallery Tag', 'taxonomy singular name' ),
  193.     'search_items' =>  __( 'Search Photogallery Tags' ),
  194.     'all_items' => __( 'All Photogallery Tags' ),
  195.     'parent_item' => __( 'Parent Photogallery Tags' ),
  196.     'parent_item_colon' => __( 'Parent Photogallery Tag:' ),
  197.     'edit_item' => __( 'Edit Photogallery Tag' ),
  198.     'update_item' => __( 'Update Photogallery Tag' ),
  199.     'add_new_item' => __( 'Add New Photogallery Tag' ),
  200.     'new_item_name' => __( 'New Photogallery Tag Name' ),
  201.   );    
  202.  
  203.   register_taxonomy('gallery-tags',array('photogallery'), array(
  204.     'hierarchical' => false,
  205.     'labels' => $labels,
  206.     'show_ui' => true,
  207.     'query_var' => true,
  208.     'rewrite' => array( 'slug' => 'gallery-tags' ),
  209.   ));
  210. }
  211. /******************************
  212.  
  213. Create post_type filters here
  214.  
  215. ******************************/
  216. //add filter to insure the text Book, or book, is displayed when user updates a photogallery
  217. add_filter('post_updated_messages', 'photogallery_updated_messages');
  218. function photogallery_updated_messages( $messages ) {
  219.  
  220.   $messages['photogallery'] = array(
  221.     0 => '', // Unused. Messages start at index 1.
  222.     1 => sprintf( __('photogallery updated. <a href="%s">View photogallery</a>'), esc_url( get_permalink($post_ID) ) ),
  223.     2 => __('Custom field updated.'),
  224.     3 => __('Custom field deleted.'),
  225.     4 => __('photogallery updated.'),
  226.     /* translators: %s: date and time of the revision */
  227.     5 => isset($_GET['revision']) ? sprintf( __('photogallery restored to revision from %s'), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
  228.     6 => sprintf( __('photogallery published. <a href="%s">View photogallery</a>'), esc_url( get_permalink($post_ID) ) ),
  229.     7 => __('photogallery saved.'),
  230.     8 => sprintf( __('photogallery submitted. <a target="_blank" href="%s">Preview photogallery</a>'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
  231.     9 => sprintf( __('photogallery scheduled for: <strong>%1$s</strong>. <a target="_blank" href="%2$s">Preview photogallery</a>'),
  232.       // translators: Publish box date format, see http://php.net/date
  233.       date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) ), esc_url( get_permalink($post_ID) ) ),
  234.     10 => sprintf( __('photogallery draft updated. <a target="_blank" href="%s">Preview photogallery</a>'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
  235.   );
  236.  
  237.   return $messages;
  238. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement