Advertisement
mikelittle

Example Custom Post Type

Nov 28th, 2012
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. /*
  3. Plugin Name: Custom Post Type and taxonomy Test
  4. Plugin URI: http://zed1.com/
  5. Description: Testing Custom Post Types in WordPres 3.4
  6. Author: Mike Little
  7. Version: 0.1.0
  8. Author URI: http://zed1.com/
  9. License: GPLv2 or later
  10. */
  11.  
  12. if ( !defined( 'NL' ) ) define( 'NL' , "\n" );
  13.  
  14. define( 'Z1_CPT_POST_TYPE',         'cpt' );
  15. define( 'Z1_CPT_POST_TYPE_SLUG',    'cpts' );
  16.  
  17. new z1_cpt(); // kick it all off
  18.  
  19. register_activation_hook( __FILE__,   array( 'z1_cpt', 'activation' ) );
  20. register_deactivation_hook( __FILE__, array( 'z1_cpt', 'deactivation' ) );
  21.  
  22. class z1_cpt {
  23.     function __construct() {
  24.         add_action( 'init',         array( 'z1_cpt', 'init' ) );
  25.         add_filter( 'the_content',  array( 'z1_cpt', 'extra_content' ) );
  26.         add_action('parse_query',   array( 'z1_cpt', 'parse_query' ) );
  27.  
  28.         // remove add new from admin bar
  29.         add_action( 'admin_bar_menu', array( 'z1_cpt', 'admin_bar_menu_remove_add_new' ), 75 );
  30.  
  31.         if ( !is_admin() )
  32.             return;
  33.  
  34.         // remove add new the menu
  35.         add_action( 'admin_menu', array( 'z1_cpt', 'admin_menu_hide_add_new' ) );
  36.         // remove the add-new link at the top of the list and edit/new pages
  37.         add_action('admin_head', array( 'z1_cpt', 'admin_head_hide_add_new_link' ) );
  38.  
  39.         // help
  40.         add_action( 'load-post.php',     array( 'z1_cpt', 'add_help_tabs' ) );
  41.         add_action( 'load-post-new.php', array( 'z1_cpt', 'add_help_tabs' ) );
  42.     } // end constructor
  43.  
  44.     static function activation() {
  45.         self::register_cpt();
  46.         self::register_taxes();
  47.         self::flush_rules();
  48.     } // end activation
  49.  
  50.     static function deactivation() {
  51.         self::flush_rules();
  52.     } // end deactivation
  53.  
  54.     static function flush_rules() {
  55.         flush_rewrite_rules();
  56.     } // end deactivation
  57.  
  58.     static function init() {
  59.         self::register_cpt();
  60.         self::register_taxes();
  61.     } // end init
  62.  
  63.     static function register_cpt() {
  64.         if ( !post_type_exists( Z1_CPT_POST_TYPE ) ) {
  65.             $labels = array( 'name' => _x( 'CPTs', 'post type general name', 'z1_cpt' ),
  66.                              'singular_name' => _x( 'CPT', 'post type singular name', 'z1_cpt' ),
  67.                              'add_new' => _x( 'New CPT', 'add new singular name', 'z1_cpt' ),
  68.                              'add_new_item' => __( 'Add New CPT', 'z1_cpt' ),
  69.                              'edit_item' => __( 'Edit CPT', 'z1_cpt' ),
  70.                              'new_item' => __( 'New CPT', 'z1_cpt' ),
  71.                              'view_item' => __( 'View CPT', 'z1_cpt' ),
  72.                              'search_items' => __( 'Search CPTs', 'z1_cpt' ),
  73.                              'not_found' =>  __( 'No CPTs Found', 'z1_cpt' ),
  74.                              'not_found_in_trash' => __( 'No CPTs found in Trash', 'z1_cpt' ),
  75.                              'parent_item_colon' => '-:-'
  76.                            );
  77.             $args = array( 'labels' => $labels,
  78.                            'public' => true,
  79.                            'publicly_queryable' => true,
  80.                            'show_ui' => true,
  81.                            'query_var' => true,
  82.                            'rewrite' => true,
  83.                            'has_archive' => Z1_CPT_POST_TYPE_SLUG,
  84.                            'capability_type' => 'post',
  85.                            'hierarchical' => true,
  86.                            'show_in_nav_menus' => true,
  87.                            'menu_position' => 100, // last block
  88.                            'supports' => array( 'title',
  89.                                                 'editor',
  90.                                                 'excerpt',
  91.                                                 'comments',
  92.                                                 'revisions',
  93.                                                 'page-attributes',
  94.                                                 'shortlink' )
  95.                          );
  96.             register_post_type( Z1_CPT_POST_TYPE, $args );
  97.         }
  98.     } // end register_cpt
  99.  
  100.     static function register_taxes() {
  101.  
  102.         if ( !taxonomy_exists( 'tax1' ) ) {
  103.             $tax_labels = array(
  104.                                 'name' => _x( 'Taxonomy ones', 'taxonomy general name', 'z1_cpt' ), // there will only be one
  105.                                 'singular_name' => _x( 'Taxonomy one', 'taxonomy singular name', 'z1_cpt' ),
  106.                                 'search_items' =>  __( 'Search Taxonomy ones', 'z1_cpt' ),
  107.                                 'popular_items' => __( 'Popular Taxonomy ones', 'z1_cpt' ),
  108.                                 'all_items' => __( 'All Taxonomy ones', 'z1_cpt' ),
  109.                                 'parent_item' => null,
  110.                                 'parent_item_colon' => null,
  111.                                 'edit_item' => __( 'Edit Taxonomy one', 'z1_cpt' ),
  112.                                 'update_item' => __( 'Update Taxonomy one', 'z1_cpt' ),
  113.                                 'add_new_item' => __( 'Add New Taxonomy one', 'z1_cpt' ),
  114.                                 'new_item_name' => __( 'New Taxonomy one Name', 'z1_cpt' ),
  115.                                 'separate_items_with_commas' => __( 'Separate Taxonomy ones with commas', 'z1_cpt' ),
  116.                                 'add_or_remove_items' => __( 'Add or remove Taxonomy ones', 'z1_cpt' ),
  117.                                 'choose_from_most_used' => __( 'Choose from the most used Taxonomy ones', 'z1_cpt' )
  118.                                );
  119.  
  120.             register_taxonomy( 'tax1', Z1_CPT_POST_TYPE, array(
  121.                 'hierarchical' => false,
  122.                 'labels' => $tax_labels,
  123.                 'show_ui' => true,
  124.                 'query_var' => true,
  125.                 'rewrite' => array( 'slug' => 'tax1s' ),
  126.                 ) );
  127.  
  128.             // add some predefined ones
  129.             if ( get_term_by( 'slug', 'tax1-term1', 'tax1' ) === false )
  130.                 wp_insert_term( __( 'Tax One term one', 'z1_cpt' ), 'tax1', array( 'slug' => 'tax1-term1', 'description' => __( 'Term number one for taxonomy one', 'z1_cpt' ) ) );
  131.         } // end if tax ! exists
  132.  
  133.         if ( !taxonomy_exists( 'tax2' ) ) {
  134.             $tax_labels = array(
  135.                                 'name' => _x( 'Taxonomy twos', 'taxonomy general name', 'z1_cpt' ),
  136.                                 'singular_name' => _x( 'Taxonomy two', 'taxonomy singular name', 'z1_cpt' ),
  137.                                 'search_items' =>  __( 'Search Taxonomy twos', 'z1_cpt' ),
  138.                                 'popular_items' => __( 'Popular Taxonomy twos', 'z1_cpt' ),
  139.                                 'all_items' => __( 'All Taxonomy twos', 'z1_cpt' ),
  140.                                 'parent_item' => null,
  141.                                 'parent_item_colon' => null,
  142.                                 'edit_item' => __( 'Edit Taxonomy two', 'z1_cpt' ),
  143.                                 'update_item' => __( 'Update Taxonomy two', 'z1_cpt' ),
  144.                                 'add_new_item' => __( 'Add New Taxonomy two', 'z1_cpt' ),
  145.                                 'new_item_name' => __( 'New Taxonomy two Name', 'z1_cpt' ),
  146.                                 'separate_items_with_commas' => __( 'Separate Taxonomy twos with commas', 'z1_cpt' ),
  147.                                 'add_or_remove_items' => __( 'Add or remove Taxonomy twos', 'z1_cpt' ),
  148.                                 'choose_from_most_used' => __( 'Choose from the most used Taxonomy twos', 'z1_cpt' )
  149.                                );
  150.  
  151.             register_taxonomy( 'tax2', Z1_CPT_POST_TYPE, array(
  152.                 'hierarchical' => true,
  153.                 'labels' => $tax_labels,
  154.                 'show_ui' => true,
  155.                 'query_var' => true,
  156.                 'rewrite' => array( 'slug' => 'tax2s' ),
  157.                 ) );
  158.  
  159.             // add some predefined ones
  160.             if ( get_term_by( 'slug', 'tax2-term1', 'tax2' ) === false ) {
  161.                 wp_insert_term( __( 'Tax two term one', 'z1_cpt' ), 'tax2', array( 'slug' => 'tax2-term1', 'description' => __( 'Term number one for tax two', 'z1_cpt' ) ) );
  162.             }
  163. /*
  164.             // stragely this doesn't always work
  165.             if ( get_term_by( 'slug', 'tax2-sub-term2', 'tax2' ) === false ) {
  166.                 $term = get_term_by( 'slug', 'tax2-term1', 'tax2' );
  167.                 wp_insert_term( __( 'Tax two sub term two', 'z1_cpt' ), 'tax2', array( 'slug' => 'tax2-sub-term2', 'parent' => $term->term_id, 'description' => __( 'Sub Term number two for tax two', 'z1_cpt' ) ) );
  168.             }
  169. //*/
  170.         } // end if tax ! exists
  171.  
  172.     } // end register_taxes
  173.  
  174.  
  175.  
  176.     static function add_help_tabs() {
  177.         $screen = get_current_screen();
  178.         error_log(var_export($screen, true));
  179.         if ( Z1_CPT_POST_TYPE == $screen->id ) {
  180.             if ( 'add' == $screen->action ) {
  181.                 $screen->add_help_tab( array(
  182.                                              'id'      => 'additional-plugin-help', // This should be unique for the screen.
  183.                                              'title'   => __( 'Special Instructions', 'z1_cpt' ),
  184.                                              'content' => __( '<p>This is the help for adding a new CPT.</p>', 'z1_cpt' ),
  185.                                              // Use 'callback' instead of 'content' for a function callback that renders the tab content.
  186.                                             ) );
  187.             } else {
  188.                 $screen->add_help_tab( array(
  189.                                              'id'      => 'additional-plugin-help', // This should be unique for the screen.
  190.                                              'title'   => __( 'Special Instructions', 'z1_cpt' ),
  191.                                              'content' => __( '<p>This is the help for updating a CPT.</p>', 'z1_cpt' ),
  192.                                              // Use 'callback' instead of 'content' for a function callback that renders the tab content.
  193.                                             ) );
  194.             }
  195.         } // end if our cpt
  196.     } // end add_help_tabs
  197.  
  198.  
  199.     static function admin_menu_hide_add_new() {
  200.         global $submenu;
  201.         if ( is_admin() && isset( $submenu['edit.php?post_type=cpt'][10] ) ) {
  202.             unset( $submenu['edit.php?post_type=cpt'][10] );
  203.         }
  204.     } // admin_menu_hide_add_new
  205.  
  206.     static function admin_head_hide_add_new_link() {
  207.         global $pagenow, $post_type;
  208.         if ( ( $pagenow == 'edit.php' ) || ( $pagenow == 'post.php' ) && isset( $post_type ) && ( Z1_CPT_POST_TYPE == $post_type ) ) {
  209.             echo NL . '<style type="text/css">' . NL
  210.                     .  '.add-new-h2 { display: none; }' . NL
  211.                     . '</style>' . NL;
  212.         }
  213.     } // end admin_head_hide_add_new_link
  214.  
  215.     static function admin_bar_menu_remove_add_new( $wp_admin_bar) {
  216.         $wp_admin_bar->remove_node('new-cpt');
  217.     } // end admin_bar_menu_remove_add_new
  218.  
  219.     static function parse_query( $query ) {
  220.     } // end parse_query
  221.  
  222.  
  223.     static function extra_content($content) {
  224.         global $post;
  225.         if ( is_singular( Z1_CPT_POST_TYPE ) )
  226.             return $content
  227.                     . get_the_term_list( $post->ID, 'tax1', 'Tax One: ', ', ' , '' )
  228.                     . get_the_term_list( $post->ID, 'tax2', 'Tax Two: ', ', ', '' );
  229.         return $content;
  230.     } //end extra_content
  231.  
  232. } // end class z1_cpt
  233.  
  234.  
  235. /* Put this in your side bar
  236. <?php z1_list_cpts(); ?>
  237. */
  238.  
  239. function z1_list_cpts( $args = array() ) {
  240.     global $post;
  241.     $default_args = array( 'post_type' => Z1_CPT_POST_TYPE, 'title_li'=> __('CPTs') );
  242.  
  243.     if ( is_single() && ( Z1_CPT_POST_TYPE == $post->post_type ) ) {
  244.         if ( $parent_id = wp_get_post_parent_id( $post->ID ) ) {
  245.             $default_args['child_of'] = $parent_id; // get siblings
  246.         } else {
  247.             $default_args['child_of'] = $post->ID; // get children
  248.         }
  249.     }
  250.  
  251.     $args = array_merge( $default_args, $args );
  252.     return wp_list_pages( $args );
  253. } // end z1_list_cpts
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement