Advertisement
luisabarca

Work CPT

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