Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Plugin Name: Custom Post Type and taxonomy Test
- Plugin URI: http://zed1.com/
- Description: Testing Custom Post Types in WordPres 3.4
- Author: Mike Little
- Version: 0.1.0
- Author URI: http://zed1.com/
- License: GPLv2 or later
- */
- if ( !defined( 'NL' ) ) define( 'NL' , "\n" );
- define( 'Z1_CPT_POST_TYPE', 'cpt' );
- define( 'Z1_CPT_POST_TYPE_SLUG', 'cpts' );
- new z1_cpt(); // kick it all off
- register_activation_hook( __FILE__, array( 'z1_cpt', 'activation' ) );
- register_deactivation_hook( __FILE__, array( 'z1_cpt', 'deactivation' ) );
- class z1_cpt {
- function __construct() {
- add_action( 'init', array( 'z1_cpt', 'init' ) );
- add_filter( 'the_content', array( 'z1_cpt', 'extra_content' ) );
- add_action('parse_query', array( 'z1_cpt', 'parse_query' ) );
- // remove add new from admin bar
- add_action( 'admin_bar_menu', array( 'z1_cpt', 'admin_bar_menu_remove_add_new' ), 75 );
- if ( !is_admin() )
- return;
- // remove add new the menu
- add_action( 'admin_menu', array( 'z1_cpt', 'admin_menu_hide_add_new' ) );
- // remove the add-new link at the top of the list and edit/new pages
- add_action('admin_head', array( 'z1_cpt', 'admin_head_hide_add_new_link' ) );
- // help
- add_action( 'load-post.php', array( 'z1_cpt', 'add_help_tabs' ) );
- add_action( 'load-post-new.php', array( 'z1_cpt', 'add_help_tabs' ) );
- } // end constructor
- static function activation() {
- self::register_cpt();
- self::register_taxes();
- self::flush_rules();
- } // end activation
- static function deactivation() {
- self::flush_rules();
- } // end deactivation
- static function flush_rules() {
- flush_rewrite_rules();
- } // end deactivation
- static function init() {
- self::register_cpt();
- self::register_taxes();
- } // end init
- static function register_cpt() {
- if ( !post_type_exists( Z1_CPT_POST_TYPE ) ) {
- $labels = array( 'name' => _x( 'CPTs', 'post type general name', 'z1_cpt' ),
- 'singular_name' => _x( 'CPT', 'post type singular name', 'z1_cpt' ),
- 'add_new' => _x( 'New CPT', 'add new singular name', 'z1_cpt' ),
- 'add_new_item' => __( 'Add New CPT', 'z1_cpt' ),
- 'edit_item' => __( 'Edit CPT', 'z1_cpt' ),
- 'new_item' => __( 'New CPT', 'z1_cpt' ),
- 'view_item' => __( 'View CPT', 'z1_cpt' ),
- 'search_items' => __( 'Search CPTs', 'z1_cpt' ),
- 'not_found' => __( 'No CPTs Found', 'z1_cpt' ),
- 'not_found_in_trash' => __( 'No CPTs found in Trash', 'z1_cpt' ),
- 'parent_item_colon' => '-:-'
- );
- $args = array( 'labels' => $labels,
- 'public' => true,
- 'publicly_queryable' => true,
- 'show_ui' => true,
- 'query_var' => true,
- 'rewrite' => true,
- 'has_archive' => Z1_CPT_POST_TYPE_SLUG,
- 'capability_type' => 'post',
- 'hierarchical' => true,
- 'show_in_nav_menus' => true,
- 'menu_position' => 100, // last block
- 'supports' => array( 'title',
- 'editor',
- 'excerpt',
- 'comments',
- 'revisions',
- 'page-attributes',
- 'shortlink' )
- );
- register_post_type( Z1_CPT_POST_TYPE, $args );
- }
- } // end register_cpt
- static function register_taxes() {
- if ( !taxonomy_exists( 'tax1' ) ) {
- $tax_labels = array(
- 'name' => _x( 'Taxonomy ones', 'taxonomy general name', 'z1_cpt' ), // there will only be one
- 'singular_name' => _x( 'Taxonomy one', 'taxonomy singular name', 'z1_cpt' ),
- 'search_items' => __( 'Search Taxonomy ones', 'z1_cpt' ),
- 'popular_items' => __( 'Popular Taxonomy ones', 'z1_cpt' ),
- 'all_items' => __( 'All Taxonomy ones', 'z1_cpt' ),
- 'parent_item' => null,
- 'parent_item_colon' => null,
- 'edit_item' => __( 'Edit Taxonomy one', 'z1_cpt' ),
- 'update_item' => __( 'Update Taxonomy one', 'z1_cpt' ),
- 'add_new_item' => __( 'Add New Taxonomy one', 'z1_cpt' ),
- 'new_item_name' => __( 'New Taxonomy one Name', 'z1_cpt' ),
- 'separate_items_with_commas' => __( 'Separate Taxonomy ones with commas', 'z1_cpt' ),
- 'add_or_remove_items' => __( 'Add or remove Taxonomy ones', 'z1_cpt' ),
- 'choose_from_most_used' => __( 'Choose from the most used Taxonomy ones', 'z1_cpt' )
- );
- register_taxonomy( 'tax1', Z1_CPT_POST_TYPE, array(
- 'hierarchical' => false,
- 'labels' => $tax_labels,
- 'show_ui' => true,
- 'query_var' => true,
- 'rewrite' => array( 'slug' => 'tax1s' ),
- ) );
- // add some predefined ones
- if ( get_term_by( 'slug', 'tax1-term1', 'tax1' ) === false )
- wp_insert_term( __( 'Tax One term one', 'z1_cpt' ), 'tax1', array( 'slug' => 'tax1-term1', 'description' => __( 'Term number one for taxonomy one', 'z1_cpt' ) ) );
- } // end if tax ! exists
- if ( !taxonomy_exists( 'tax2' ) ) {
- $tax_labels = array(
- 'name' => _x( 'Taxonomy twos', 'taxonomy general name', 'z1_cpt' ),
- 'singular_name' => _x( 'Taxonomy two', 'taxonomy singular name', 'z1_cpt' ),
- 'search_items' => __( 'Search Taxonomy twos', 'z1_cpt' ),
- 'popular_items' => __( 'Popular Taxonomy twos', 'z1_cpt' ),
- 'all_items' => __( 'All Taxonomy twos', 'z1_cpt' ),
- 'parent_item' => null,
- 'parent_item_colon' => null,
- 'edit_item' => __( 'Edit Taxonomy two', 'z1_cpt' ),
- 'update_item' => __( 'Update Taxonomy two', 'z1_cpt' ),
- 'add_new_item' => __( 'Add New Taxonomy two', 'z1_cpt' ),
- 'new_item_name' => __( 'New Taxonomy two Name', 'z1_cpt' ),
- 'separate_items_with_commas' => __( 'Separate Taxonomy twos with commas', 'z1_cpt' ),
- 'add_or_remove_items' => __( 'Add or remove Taxonomy twos', 'z1_cpt' ),
- 'choose_from_most_used' => __( 'Choose from the most used Taxonomy twos', 'z1_cpt' )
- );
- register_taxonomy( 'tax2', Z1_CPT_POST_TYPE, array(
- 'hierarchical' => true,
- 'labels' => $tax_labels,
- 'show_ui' => true,
- 'query_var' => true,
- 'rewrite' => array( 'slug' => 'tax2s' ),
- ) );
- // add some predefined ones
- if ( get_term_by( 'slug', 'tax2-term1', 'tax2' ) === false ) {
- wp_insert_term( __( 'Tax two term one', 'z1_cpt' ), 'tax2', array( 'slug' => 'tax2-term1', 'description' => __( 'Term number one for tax two', 'z1_cpt' ) ) );
- }
- /*
- // stragely this doesn't always work
- if ( get_term_by( 'slug', 'tax2-sub-term2', 'tax2' ) === false ) {
- $term = get_term_by( 'slug', 'tax2-term1', 'tax2' );
- 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' ) ) );
- }
- //*/
- } // end if tax ! exists
- } // end register_taxes
- static function add_help_tabs() {
- $screen = get_current_screen();
- error_log(var_export($screen, true));
- if ( Z1_CPT_POST_TYPE == $screen->id ) {
- if ( 'add' == $screen->action ) {
- $screen->add_help_tab( array(
- 'id' => 'additional-plugin-help', // This should be unique for the screen.
- 'title' => __( 'Special Instructions', 'z1_cpt' ),
- 'content' => __( '<p>This is the help for adding a new CPT.</p>', 'z1_cpt' ),
- // Use 'callback' instead of 'content' for a function callback that renders the tab content.
- ) );
- } else {
- $screen->add_help_tab( array(
- 'id' => 'additional-plugin-help', // This should be unique for the screen.
- 'title' => __( 'Special Instructions', 'z1_cpt' ),
- 'content' => __( '<p>This is the help for updating a CPT.</p>', 'z1_cpt' ),
- // Use 'callback' instead of 'content' for a function callback that renders the tab content.
- ) );
- }
- } // end if our cpt
- } // end add_help_tabs
- static function admin_menu_hide_add_new() {
- global $submenu;
- if ( is_admin() && isset( $submenu['edit.php?post_type=cpt'][10] ) ) {
- unset( $submenu['edit.php?post_type=cpt'][10] );
- }
- } // admin_menu_hide_add_new
- static function admin_head_hide_add_new_link() {
- global $pagenow, $post_type;
- if ( ( $pagenow == 'edit.php' ) || ( $pagenow == 'post.php' ) && isset( $post_type ) && ( Z1_CPT_POST_TYPE == $post_type ) ) {
- echo NL . '<style type="text/css">' . NL
- . '.add-new-h2 { display: none; }' . NL
- . '</style>' . NL;
- }
- } // end admin_head_hide_add_new_link
- static function admin_bar_menu_remove_add_new( $wp_admin_bar) {
- $wp_admin_bar->remove_node('new-cpt');
- } // end admin_bar_menu_remove_add_new
- static function parse_query( $query ) {
- } // end parse_query
- static function extra_content($content) {
- global $post;
- if ( is_singular( Z1_CPT_POST_TYPE ) )
- return $content
- . get_the_term_list( $post->ID, 'tax1', 'Tax One: ', ', ' , '' )
- . get_the_term_list( $post->ID, 'tax2', 'Tax Two: ', ', ', '' );
- return $content;
- } //end extra_content
- } // end class z1_cpt
- /* Put this in your side bar
- <?php z1_list_cpts(); ?>
- */
- function z1_list_cpts( $args = array() ) {
- global $post;
- $default_args = array( 'post_type' => Z1_CPT_POST_TYPE, 'title_li'=> __('CPTs') );
- if ( is_single() && ( Z1_CPT_POST_TYPE == $post->post_type ) ) {
- if ( $parent_id = wp_get_post_parent_id( $post->ID ) ) {
- $default_args['child_of'] = $parent_id; // get siblings
- } else {
- $default_args['child_of'] = $post->ID; // get children
- }
- }
- $args = array_merge( $default_args, $args );
- return wp_list_pages( $args );
- } // end z1_list_cpts
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement