SHOW:
|
|
- or go back to the newest paste.
1 | <?php | |
2 | /* | |
3 | - | Plugin Name: Custom Post Type Test |
3 | + | Plugin Name: Custom Post Type and taxonomy Test |
4 | Plugin URI: http://zed1.com/ | |
5 | - | Description: Testing Custom Post Types in WordPres 3.3 |
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 | - | new cpt(); // kick it all off |
12 | + | if ( !defined( 'NL' ) ) define( 'NL' , "\n" ); |
13 | ||
14 | - | class cpt { |
14 | + | define( 'Z1_CPT_POST_TYPE', 'cpt' ); |
15 | define( 'Z1_CPT_POST_TYPE_SLUG', 'cpts' ); | |
16 | - | register_activation_hook( __FILE__, array( 'cpt', 'activation' ) ); |
16 | + | |
17 | - | register_deactivation_hook( __FILE__, array( 'cpt', 'deactivation' ) ); |
17 | + | new z1_cpt(); // kick it all off |
18 | - | add_action( 'init', array( 'cpt', 'init' ) ); |
18 | + | |
19 | - | add_filter( 'the_content', array( 'cpt', 'extra_content' ) ); |
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 | - | if ( !post_type_exists( 'cpt' ) ) { |
42 | + | |
43 | - | $labels = array( 'name' => _x( 'CPTs', 'post type general name' ), |
43 | + | |
44 | - | 'singular_name' => _x( 'CPT', 'post type singular name' ), |
44 | + | |
45 | - | 'add_new' => _x( 'New CPT', 'add new singular name' ), |
45 | + | |
46 | - | 'add_new_item' => __( 'Add New CPT' ), |
46 | + | |
47 | - | 'edit_item' => __( 'Edit CPT' ), |
47 | + | |
48 | - | 'new_item' => __( 'New CPT' ), |
48 | + | |
49 | - | 'view_item' => __( 'View CPT' ), |
49 | + | |
50 | - | 'search_items' => __( 'Search CPTs' ), |
50 | + | |
51 | - | 'not_found' => __( 'No CPTs Found' ), |
51 | + | |
52 | - | 'not_found_in_trash' => __( 'No CPTs found in Trash' ), |
52 | + | |
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 | - | 'has_archive' => 'cpts', |
61 | + | |
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 | - | 'excerpt', |
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 | - | register_post_type( 'cpt', $args ); |
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 | - | 'name' => _x( 'Taxonomy ones', 'taxonomy general name' ), // there will only be one |
82 | + | |
83 | - | 'singular_name' => _x( 'Taxonomy one', 'taxonomy singular name' ), |
83 | + | 'has_archive' => Z1_CPT_POST_TYPE_SLUG, |
84 | - | 'search_items' => __( 'Search Taxonomy ones', 'dmd_school' ), |
84 | + | |
85 | - | 'popular_items' => __( 'Popular Taxonomy ones', 'dmd_school' ), |
85 | + | |
86 | - | 'all_items' => __( 'All Taxonomy ones', 'dmd_school' ), |
86 | + | |
87 | 'menu_position' => 100, // last block | |
88 | 'supports' => array( 'title', | |
89 | - | 'edit_item' => __( 'Edit Taxonomy one', 'dmd_school' ), |
89 | + | |
90 | - | 'update_item' => __( 'Update Taxonomy one', 'dmd_school' ), |
90 | + | 'excerpt', |
91 | - | 'add_new_item' => __( 'Add New Taxonomy one', 'dmd_school' ), |
91 | + | |
92 | - | 'new_item_name' => __( 'New Taxonomy one Name', 'dmd_school' ), |
92 | + | |
93 | - | 'separate_items_with_commas' => __( 'Separate Taxonomy ones with commas', 'dmd_school' ), |
93 | + | |
94 | - | 'add_or_remove_items' => __( 'Add or remove Taxonomy ones', 'dmd_school' ), |
94 | + | |
95 | - | 'choose_from_most_used' => __( 'Choose from the most used Taxonomy ones', 'dmd_school' ) |
95 | + | |
96 | register_post_type( Z1_CPT_POST_TYPE, $args ); | |
97 | } | |
98 | - | register_taxonomy( 'tax1', 'cpt', array( |
98 | + | |
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 | - | |
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 | - | wp_insert_term( 'Tax One term one', 'tax1', array( 'slug' => 'tax1-term1', 'description' => 'Term number one for taxonomy one' ) ); |
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 | - | 'name' => _x( 'Taxonomy twos', 'taxonomy general name' ), |
114 | + | 'new_item_name' => __( 'New Taxonomy one Name', 'z1_cpt' ), |
115 | - | 'singular_name' => _x( 'Taxonomy two', 'taxonomy singular name' ), |
115 | + | 'separate_items_with_commas' => __( 'Separate Taxonomy ones with commas', 'z1_cpt' ), |
116 | - | 'search_items' => __( 'Search Taxonomy twos', 'dmd_school' ), |
116 | + | 'add_or_remove_items' => __( 'Add or remove Taxonomy ones', 'z1_cpt' ), |
117 | - | 'popular_items' => __( 'Popular Taxonomy twos', 'dmd_school' ), |
117 | + | 'choose_from_most_used' => __( 'Choose from the most used Taxonomy ones', 'z1_cpt' ) |
118 | - | 'all_items' => __( 'All Taxonomy twos', 'dmd_school' ), |
118 | + | |
119 | ||
120 | register_taxonomy( 'tax1', Z1_CPT_POST_TYPE, array( | |
121 | - | 'edit_item' => __( 'Edit Taxonomy two', 'dmd_school' ), |
121 | + | |
122 | - | 'update_item' => __( 'Update Taxonomy two', 'dmd_school' ), |
122 | + | |
123 | - | 'add_new_item' => __( 'Add New Taxonomy two', 'dmd_school' ), |
123 | + | |
124 | - | 'new_item_name' => __( 'New Taxonomy two Name', 'dmd_school' ), |
124 | + | |
125 | - | 'separate_items_with_commas' => __( 'Separate Taxonomy twos with commas', 'dmd_school' ), |
125 | + | |
126 | - | 'add_or_remove_items' => __( 'Add or remove Taxonomy twos', 'dmd_school' ), |
126 | + | |
127 | - | 'choose_from_most_used' => __( 'Choose from the most used Taxonomy twos', 'dmd_school' ) |
127 | + | |
128 | // add some predefined ones | |
129 | if ( get_term_by( 'slug', 'tax1-term1', 'tax1' ) === false ) | |
130 | - | register_taxonomy( 'tax2', 'cpt', array( |
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 | - | |
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 | - | wp_insert_term( 'Tax two term one', 'tax2', array( 'slug' => 'tax2-term1', 'description' => 'Term number one for tax two' ) ); |
140 | + | |
141 | 'parent_item_colon' => null, | |
142 | 'edit_item' => __( 'Edit Taxonomy two', 'z1_cpt' ), | |
143 | 'update_item' => __( 'Update Taxonomy two', 'z1_cpt' ), | |
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' ) ); |
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 | - | return $content |
151 | + | register_taxonomy( 'tax2', Z1_CPT_POST_TYPE, array( |
152 | - | . get_the_term_list( $post->ID, 'tax1', 'Tax One: ', ', ' , '' ) |
152 | + | |
153 | - | . get_the_term_list( $post->ID, 'tax2', 'Tax Two: ', ', ', '' ); |
153 | + | |
154 | 'show_ui' => true, | |
155 | - | |
155 | + | |
156 | - | } // end class cpt |
156 | + | |
157 | ) ); | |
158 | - | /* put this in your side bar |
158 | + | |
159 | - | <?php list_cpts(); ?> |
159 | + | |
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 | - | function list_cpts( $args = array() ) { |
162 | + | |
163 | /* | |
164 | - | $default_args = array( 'post_type'=>'cpt', 'title_li'=> __('CPTs') ); |
164 | + | // stragely this doesn't always work |
165 | if ( get_term_by( 'slug', 'tax2-sub-term2', 'tax2' ) === false ) { | |
166 | - | if ( is_single() && ( 'cpt' == $post->post_type ) ) { |
166 | + | |
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 | - | } |
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 |