Advertisement
luisabarca

Vehicles CPT

Oct 25th, 2011
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.76 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Products Vehicles post type
  4. Description: Plugin for products Vehicles custom post type
  5. Author: Luis Abarca
  6. Author Uri: http://luisabarca.com
  7. Plugin Uri: http://justoalblanco.com
  8. */
  9.  
  10. $product = Product_Vehicles::instance();
  11.  
  12.  
  13. class Product_Vehicles {
  14.     const CUSTOM_TYPE_NAME = 'product_info';
  15.     const TAXONOMY_NAME = 'vehicle';
  16.     private static $instance;
  17.    
  18.     /***************************************************************************
  19.      * Static functions
  20.      **************************************************************************/
  21.     public static function instance () {
  22.  
  23.         if ( ! isset( self::$instance ) ) {
  24.             $class_name = __CLASS__;
  25.             self::$instance = new $class_name;
  26.         }
  27.  
  28.         return self::$instance;
  29.        
  30.     }
  31.    
  32.     private function __construct()
  33.     {                
  34.         add_action('init', array($this, 'register_custom_types') );
  35.         add_filter('post_type_link', array($this, 'custom_permalinks'), 1, 3);
  36.        
  37.         register_activation_hook(__FILE__, array($this, 'activate') );
  38.         register_deactivation_hook(__FILE__, array($this, 'deactivate') );
  39.  
  40.     }
  41.  
  42.     /*
  43.      *
  44.      */
  45.     public function custom_permalinks($url, $post = null, $leavename = false)
  46.     {
  47.         // products only
  48.         if ($post->post_type != self::CUSTOM_TYPE_NAME) {
  49.             return $url;
  50.         }
  51.        
  52.         $post_id = $post->ID;
  53.    
  54.         $taxonomy = self::TAXONOMY_NAME;
  55.         $taxonomy_tag = '%' . $taxonomy . '%';
  56.    
  57.         // Check if exists the product type tag
  58.         if (strpos($taxonomy_tag, $url) < 0) {
  59.             // replace taxonomy tag
  60.             $url = str_replace($taxonomy_tag, '', $url);
  61.         } else {
  62.             // Get the terms
  63.             $terms = wp_get_post_terms($post_id, $taxonomy);
  64.    
  65.             if (is_array($terms) && sizeof($terms) > 0) {
  66.                 $category = $terms[0];
  67.                 // replace taxonomy tag with the term slug
  68.                 $url = str_replace($taxonomy_tag, $category->slug, $url);
  69.             }
  70.         }    
  71.    
  72.         return $url;
  73.     }
  74.    
  75.    
  76.     /*
  77.      *
  78.      *
  79.      */
  80.     public function register_custom_types()
  81.     {
  82.         global $wp_rewrite;
  83.         global $wp_query;
  84.    
  85.         register_post_type(self::CUSTOM_TYPE_NAME, array(
  86.             'label'           => 'Products Vehicles',
  87.             'description'     => '',
  88.             'public'          => true,
  89.             'show_ui'         => true,
  90.             'show_in_menu'    => true,
  91.             'capability_type' => 'post',
  92.             'hierarchical'    => true,
  93.             'rewrite'         => array('slug' => self::CUSTOM_TYPE_NAME),
  94.             'query_var'       => true,
  95.             'has_archive'     => true,
  96.             'menu_position'   => 6,
  97.             'supports'        => array( 'title', 'editor', 'excerpt', 'trackbacks', 'revisions', 'thumbnail', 'author' ),
  98.             'labels'          => array(
  99.                 'name'               => 'Products Vehicles',
  100.                 'singular_name'      => 'product',
  101.                 'menu_name'          => 'Products Vehicles',
  102.                 'add_new'            => 'Add product',
  103.                 'add_new_item'       => 'Add New product',
  104.                 'edit'               => 'Edit',
  105.                 'edit_item'          => 'Edit product',
  106.                 'new_item'           => 'New product',
  107.                 'view'               => 'View product',
  108.                 'view_item'          => 'View product',
  109.                 'search_items'       => 'Search Products',
  110.                 'not_found'          => 'No Products Found',
  111.                 'not_found_in_trash' => 'No Products Found in Trash',
  112.                 'parent'             => 'Parent product')
  113.             )
  114.         );
  115.    
  116.         register_taxonomy(self::TAXONOMY_NAME, self::CUSTOM_TYPE_NAME, array(
  117.             'hierarchical'   => true,
  118.             'label'          => 'Product Vehicles',
  119.             'show_ui'        => true,
  120.             'query_var'      => true,
  121.             'rewrite'        => array('slug' => self::CUSTOM_TYPE_NAME . '/' . self::TAXONOMY_NAME),
  122.             'singular_label' => 'Product Vehicles')
  123.         );
  124.        
  125.         $wp_rewrite->extra_permastructs[self::CUSTOM_TYPE_NAME][0] = '/' . self::CUSTOM_TYPE_NAME . '/' . self::TAXONOMY_NAME . '/%' . self::TAXONOMY_NAME . '%/%' . self::CUSTOM_TYPE_NAME . '%';
  126.  
  127.        
  128.     }
  129.    
  130.    
  131.     /**
  132.      *
  133.      */
  134.     public function activate()
  135.     {
  136.         self::register_custom_types();
  137.         flush_rewrite_rules();
  138.     }
  139.    
  140.     // }}}
  141.     // {{{
  142.    
  143.     /**
  144.      *
  145.      */
  146.     public function deactivate()
  147.     {
  148.         flush_rewrite_rules();
  149.     }
  150.  
  151. }
  152.  
  153.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement