mikelittle

Example Custom Post Type and Custom Taxonomies.

Apr 20th, 2012
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.46 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Custom Post Type Test
  4. Plugin URI: http://zed1.com/
  5. Description: Testing Custom Post Types in WordPres 3.3
  6. Author: Mike Little
  7. Version: 0.1.0
  8. Author URI: http://zed1.com/
  9. License: GPLv2 or later
  10. */
  11.  
  12. new cpt(); // kick it all off
  13.  
  14. class cpt {
  15.     function __construct() {
  16.         register_activation_hook( __FILE__, array( 'cpt', 'activation' ) );
  17.         register_deactivation_hook( __FILE__, array( 'cpt', 'deactivation' ) );
  18.         add_action( 'init', array( 'cpt', 'init' ) );
  19.         add_filter( 'the_content', array( 'cpt', 'extra_content' ) );
  20.     } // end constructor
  21.  
  22.     static function activation() {
  23.         self::register_cpt();
  24.         self::register_taxes();
  25.         self::flush_rules();
  26.     } // end activation
  27.  
  28.     static function deactivation() {
  29.         self::flush_rules();
  30.     } // end deactivation
  31.  
  32.     static function flush_rules() {
  33.         flush_rewrite_rules();
  34.     } // end deactivation
  35.  
  36.     static function init() {
  37.         self::register_cpt();
  38.         self::register_taxes();
  39.     } // end init
  40.  
  41.     static function register_cpt() {
  42.         if ( !post_type_exists( 'cpt' ) ) {
  43.             $labels = array( 'name' => _x( 'CPTs', 'post type general name' ),
  44.                              'singular_name' => _x( 'CPT', 'post type singular name' ),
  45.                              'add_new' => _x( 'New CPT', 'add new singular name' ),
  46.                              'add_new_item' => __( 'Add New CPT' ),
  47.                              'edit_item' => __( 'Edit CPT' ),
  48.                              'new_item' => __( 'New CPT' ),
  49.                              'view_item' => __( 'View CPT' ),
  50.                              'search_items' => __( 'Search CPTs' ),
  51.                              'not_found' =>  __( 'No CPTs Found' ),
  52.                              'not_found_in_trash' => __( 'No CPTs found in Trash' ),
  53.                              'parent_item_colon' => '-:-'
  54.                            );
  55.             $args = array( 'labels' => $labels,
  56.                            'public' => true,
  57.                            'publicly_queryable' => true,
  58.                            'show_ui' => true,
  59.                            'query_var' => true,
  60.                            'rewrite' => true,
  61.                            'has_archive' => 'cpts',
  62.                            'capability_type' => 'post',
  63.                            'hierarchical' => true,
  64.                            'show_in_nav_menus' => true,
  65.                            'menu_position' => 100, // last block
  66.                            'supports' => array( 'title',
  67.                                                 'editor',
  68.                                                 'excerpt',
  69.                                                 'comments',
  70.                                                 'revisions',
  71.                                                 'page-attributes',
  72.                                                 'shortlink' )
  73.                          );
  74.             register_post_type( 'cpt', $args );
  75.         }
  76.     } // end register_cpt
  77.  
  78.     static function register_taxes() {
  79.  
  80.         if ( !taxonomy_exists( 'tax1' ) ) {
  81.             $tax_labels = array(
  82.                                 'name' => _x( 'Taxonomy ones', 'taxonomy general name' ), // there will only be one
  83.                                 'singular_name' => _x( 'Taxonomy one', 'taxonomy singular name' ),
  84.                                 'search_items' =>  __( 'Search Taxonomy ones', 'dmd_school' ),
  85.                                 'popular_items' => __( 'Popular Taxonomy ones', 'dmd_school' ),
  86.                                 'all_items' => __( 'All Taxonomy ones', 'dmd_school' ),
  87.                                 'parent_item' => null,
  88.                                 'parent_item_colon' => null,
  89.                                 'edit_item' => __( 'Edit Taxonomy one', 'dmd_school' ),
  90.                                 'update_item' => __( 'Update Taxonomy one', 'dmd_school' ),
  91.                                 'add_new_item' => __( 'Add New Taxonomy one', 'dmd_school' ),
  92.                                 'new_item_name' => __( 'New Taxonomy one Name', 'dmd_school' ),
  93.                                 'separate_items_with_commas' => __( 'Separate Taxonomy ones with commas', 'dmd_school' ),
  94.                                 'add_or_remove_items' => __( 'Add or remove Taxonomy ones', 'dmd_school' ),
  95.                                 'choose_from_most_used' => __( 'Choose from the most used Taxonomy ones', 'dmd_school' )
  96.                                );
  97.  
  98.             register_taxonomy( 'tax1', 'cpt', array(
  99.                 'hierarchical' => false,
  100.                 'labels' => $tax_labels,
  101.                 'show_ui' => true,
  102.                 'query_var' => true,
  103.                 'rewrite' => array( 'slug' => 'tax1s' ),
  104.                 ) );
  105.            
  106.             // add some predefined ones
  107.             if ( get_term_by( 'slug', 'tax1-term1', 'tax1' ) === false )
  108.                 wp_insert_term( 'Tax One term one', 'tax1', array( 'slug' => 'tax1-term1', 'description' => 'Term number one for taxonomy one' ) );
  109.  
  110.         } // end if tax ! exists
  111.  
  112.         if ( !taxonomy_exists( 'tax2' ) ) {
  113.             $tax_labels = array(
  114.                                 'name' => _x( 'Taxonomy twos', 'taxonomy general name' ),
  115.                                 'singular_name' => _x( 'Taxonomy two', 'taxonomy singular name' ),
  116.                                 'search_items' =>  __( 'Search Taxonomy twos', 'dmd_school' ),
  117.                                 'popular_items' => __( 'Popular Taxonomy twos', 'dmd_school' ),
  118.                                 'all_items' => __( 'All Taxonomy twos', 'dmd_school' ),
  119.                                 'parent_item' => null,
  120.                                 'parent_item_colon' => null,
  121.                                 'edit_item' => __( 'Edit Taxonomy two', 'dmd_school' ),
  122.                                 'update_item' => __( 'Update Taxonomy two', 'dmd_school' ),
  123.                                 'add_new_item' => __( 'Add New Taxonomy two', 'dmd_school' ),
  124.                                 'new_item_name' => __( 'New Taxonomy two Name', 'dmd_school' ),
  125.                                 'separate_items_with_commas' => __( 'Separate Taxonomy twos with commas', 'dmd_school' ),
  126.                                 'add_or_remove_items' => __( 'Add or remove Taxonomy twos', 'dmd_school' ),
  127.                                 'choose_from_most_used' => __( 'Choose from the most used Taxonomy twos', 'dmd_school' )
  128.                                );
  129.  
  130.             register_taxonomy( 'tax2', 'cpt', array(
  131.                 'hierarchical' => true,
  132.                 'labels' => $tax_labels,
  133.                 'show_ui' => true,
  134.                 'query_var' => true,
  135.                 'rewrite' => array( 'slug' => 'tax2s' ),
  136.                 ) );
  137.            
  138.             // add some predefined ones
  139.             if ( get_term_by( 'slug', 'tax2-term1', 'tax2' ) === false ) {
  140.                 wp_insert_term( 'Tax two term one', 'tax2', array( 'slug' => 'tax2-term1', 'description' => 'Term number one for tax two' ) );
  141.             }
  142.             if ( get_term_by( 'slug', 'tax2-sub-term2', 'tax2' ) === false ) {
  143.                 $term = get_term_by( 'slug', 'tax2-term1', 'tax2' );
  144.                 wp_insert_term( 'Tax two sub term two', 'tax2', array( 'slug' => 'tax2-sub-term2', 'parent' => $term->term_id, 'description' => 'Sub Term number two for tax two' ) );
  145.             }
  146.         } // end if tax ! exists
  147.  
  148.     } // end register_taxes
  149.  
  150.     static function extra_content($content) {
  151.         return $content
  152.                 . get_the_term_list( $post->ID, 'tax1', 'Tax One: ', ', ' , '' )
  153.                 . get_the_term_list( $post->ID, 'tax2', 'Tax Two: ', ', ', '' );
  154.     } //end extra_content
  155.    
  156. } // end class cpt
  157.  
  158. /* put this in your side bar
  159. <?php list_cpts(); ?>
  160. */
  161.  
  162. function list_cpts( $args = array() ) {
  163.     global $post;
  164.     $default_args = array( 'post_type'=>'cpt', 'title_li'=> __('CPTs') );
  165.  
  166.     if ( is_single() && ( 'cpt' == $post->post_type ) ) {
  167.         if ( $parent_id = wp_get_post_parent_id( $post->ID ) ) {
  168.             $default_args['child_of'] = $parent_id; // get siblings
  169.         } else {
  170.             $default_args['child_of'] = $post->ID; // get children
  171.         }
  172.     }
  173.  
  174.     $args = array_merge( $default_args, $args );
  175.     return wp_list_pages( $args );
  176. }
Advertisement
Add Comment
Please, Sign In to add comment