Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Plugin Name: Products Vehicles post type
- Description: Plugin for products Vehicles custom post type
- Author: Luis Abarca
- Author Uri: http://luisabarca.com
- Plugin Uri: http://justoalblanco.com
- */
- $product = Product_Vehicles::instance();
- class Product_Vehicles {
- const CUSTOM_TYPE_NAME = 'product_info';
- const TAXONOMY_NAME = 'vehicle';
- private static $instance;
- /***************************************************************************
- * Static functions
- **************************************************************************/
- public static function instance () {
- if ( ! isset( self::$instance ) ) {
- $class_name = __CLASS__;
- self::$instance = new $class_name;
- }
- return self::$instance;
- }
- private function __construct()
- {
- add_action('init', array($this, 'register_custom_types') );
- add_filter('post_type_link', array($this, 'custom_permalinks'), 1, 3);
- register_activation_hook(__FILE__, array($this, 'activate') );
- register_deactivation_hook(__FILE__, array($this, 'deactivate') );
- }
- /*
- *
- */
- public function custom_permalinks($url, $post = null, $leavename = false)
- {
- // products only
- if ($post->post_type != self::CUSTOM_TYPE_NAME) {
- return $url;
- }
- $post_id = $post->ID;
- $taxonomy = self::TAXONOMY_NAME;
- $taxonomy_tag = '%' . $taxonomy . '%';
- // Check if exists the product type tag
- if (strpos($taxonomy_tag, $url) < 0) {
- // replace taxonomy tag
- $url = str_replace($taxonomy_tag, '', $url);
- } else {
- // Get the terms
- $terms = wp_get_post_terms($post_id, $taxonomy);
- if (is_array($terms) && sizeof($terms) > 0) {
- $category = $terms[0];
- // replace taxonomy tag with the term slug
- $url = str_replace($taxonomy_tag, $category->slug, $url);
- }
- }
- return $url;
- }
- /*
- *
- *
- */
- public function register_custom_types()
- {
- global $wp_rewrite;
- global $wp_query;
- register_post_type(self::CUSTOM_TYPE_NAME, array(
- 'label' => 'Products Vehicles',
- 'description' => '',
- 'public' => true,
- 'show_ui' => true,
- 'show_in_menu' => true,
- 'capability_type' => 'post',
- 'hierarchical' => true,
- 'rewrite' => array('slug' => self::CUSTOM_TYPE_NAME),
- 'query_var' => true,
- 'has_archive' => true,
- 'menu_position' => 6,
- 'supports' => array( 'title', 'editor', 'excerpt', 'trackbacks', 'revisions', 'thumbnail', 'author' ),
- 'labels' => array(
- 'name' => 'Products Vehicles',
- 'singular_name' => 'product',
- 'menu_name' => 'Products Vehicles',
- 'add_new' => 'Add product',
- 'add_new_item' => 'Add New product',
- 'edit' => 'Edit',
- 'edit_item' => 'Edit product',
- 'new_item' => 'New product',
- 'view' => 'View product',
- 'view_item' => 'View product',
- 'search_items' => 'Search Products',
- 'not_found' => 'No Products Found',
- 'not_found_in_trash' => 'No Products Found in Trash',
- 'parent' => 'Parent product')
- )
- );
- register_taxonomy(self::TAXONOMY_NAME, self::CUSTOM_TYPE_NAME, array(
- 'hierarchical' => true,
- 'label' => 'Product Vehicles',
- 'show_ui' => true,
- 'query_var' => true,
- 'rewrite' => array('slug' => self::CUSTOM_TYPE_NAME . '/' . self::TAXONOMY_NAME),
- 'singular_label' => 'Product Vehicles')
- );
- $wp_rewrite->extra_permastructs[self::CUSTOM_TYPE_NAME][0] = '/' . self::CUSTOM_TYPE_NAME . '/' . self::TAXONOMY_NAME . '/%' . self::TAXONOMY_NAME . '%/%' . self::CUSTOM_TYPE_NAME . '%';
- }
- /**
- *
- */
- public function activate()
- {
- self::register_custom_types();
- flush_rewrite_rules();
- }
- // }}}
- // {{{
- /**
- *
- */
- public function deactivate()
- {
- flush_rewrite_rules();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement