-
Notifications
You must be signed in to change notification settings - Fork 4
/
britton-posttypes.php
274 lines (232 loc) · 10.7 KB
/
britton-posttypes.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
<?php
/**
* Plugin Name: Add Movie Review Post Type and Taxonomies
* Description: A brief description of the Plugin.
* Version: 0.1
* Author: Morten Rand-Hendriksen
* Author URI: http://mor10.com
* License: GPL2
*/
/* Copyright 2014 Morten Rand-Hendriksen (email : [email protected])
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/*
* Creates a post type for Movie reviews
* Also sets up 5 custom taxonomies
*/
add_action( 'init', 'mortens_reviews_init' );
/**
* Register a book post type.
*
* @link http://codex.wordpress.org/Function_Reference/register_post_type
*/
function mortens_reviews_init() {
$labels = array(
'name' => 'Reviews',
'singular_name' => 'Review',
'menu_name' => 'Reviews',
'name_admin_bar' => 'Review',
'add_new' => 'Add new',
'add_new_item' => 'Add New Review',
'new_item' => 'New Review',
'edit_item' => 'Edit Review',
'view_item' => 'View Review',
'all_items' => 'All Reviews',
'search_items' => 'Search Reviews',
'parent_item_colon' => 'Parent Reviews',
'not_found' => 'No Reviews Found',
'not_found_in_trash' => 'No reviews found in Trash',
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_icon' => 'dashicons-star-half',
'query_var' => true,
'rewrite' => array( 'slug' => 'reviews' ),
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => 5,
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' )
);
register_post_type( 'review', $args );
}
function my_rewrite_flush() {
// First, we "add" the custom post type via the above written function.
// Note: "add" is written with quotes, as CPTs don't get added to the DB,
// They are only referenced in the post_type column with a post entry,
// when you add a post of this CPT.
mortens_reviews_init();
// ATTENTION: This is *only* done during plugin activation hook in this example!
// You should *NEVER EVER* do this on every page load!!
flush_rewrite_rules();
}
register_activation_hook( __FILE__, 'my_rewrite_flush' );
// hook into the init action and call create_book_taxonomies when it fires
add_action( 'init', 'create_review_taxonomies', 0 );
// create two taxonomies, genres and writers for the post type "book"
function create_review_taxonomies() {
// Add Genre taxonomy
$labels = array(
'name' => _x( 'Genres', 'taxonomy general name' ),
'singular_name' => _x( 'Genre', 'taxonomy singular name' ),
'search_items' => __( 'Search Genres' ),
'all_items' => __( 'All Genres' ),
'parent_item' => __( 'Parent Genre' ),
'parent_item_colon' => __( 'Parent Genre:' ),
'edit_item' => __( 'Edit Genre' ),
'update_item' => __( 'Update Genre' ),
'add_new_item' => __( 'Add New Genre' ),
'new_item_name' => __( 'New Genre Name' ),
'menu_name' => __( 'Genre' ),
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'genres' ),
);
register_taxonomy( 'genre', array( 'review' ), $args );
// Add Year of Release taxonomy
$labels = array(
'name' => _x( 'Years of Release', 'taxonomy general name' ),
'singular_name' => _x( 'Year of Release', 'taxonomy singular name' ),
'search_items' => __( 'Search Years of Release' ),
'all_items' => __( 'All Years of Release' ),
'parent_item' => __( 'Parent Year of Release' ),
'parent_item_colon' => __( 'Parent Year of Release:' ),
'edit_item' => __( 'Edit Year of Release' ),
'update_item' => __( 'Update Year of Release' ),
'add_new_item' => __( 'Add New Year of Release' ),
'new_item_name' => __( 'New Year of Release Name' ),
'menu_name' => __( 'Year of Release' ),
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'release-years' ),
);
register_taxonomy( 'release_year', array( 'review' ), $args );
// Add Rating
$labels = array(
'name' => _x( 'Ratings', 'taxonomy general name' ),
'singular_name' => _x( 'Rating', 'taxonomy singular name' ),
'search_items' => __( 'Search Ratings' ),
'all_items' => __( 'All Ratings' ),
'parent_item' => __( 'Parent Rating' ),
'parent_item_colon' => __( 'Parent Rating:' ),
'edit_item' => __( 'Edit Rating' ),
'update_item' => __( 'Update Rating' ),
'add_new_item' => __( 'Add New Rating' ),
'new_item_name' => __( 'New Rating Name' ),
'menu_name' => __( 'Rating' ),
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'ratings' ),
);
register_taxonomy( 'rating', array( 'review' ), $args );
// Add Mood taxonomy
$labels = array(
'name' => 'Moods',
'singular_name' => 'Mood',
'search_items' => 'Search Moods',
'popular_items' => 'Popular Moods',
'all_items' => 'All Moods',
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => 'Edit Mood',
'update_item' => 'Update Mood',
'add_new_item' => 'Add New Mood',
'new_item_name' => 'New Mood Name',
'separate_items_with_commas' => 'Separate Moods with commas',
'add_or_remove_items' => 'Add or remove Moods',
'choose_from_most_used' => 'Choose from the most used Moods',
'not_found' => 'No Moods found',
'menu_name' => 'Moods',
);
$args = array(
'hierarchical' => false,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'update_count_callback' => '_update_post_term_count',
'query_var' => true,
'rewrite' => array( 'slug' => 'moods' ),
);
register_taxonomy( 'mood', 'review', $args );
// Add Features taxonomy
$labels = array(
'name' => 'Features',
'singular_name' => 'Feature',
'search_items' => 'Search Features',
'popular_items' => 'Popular Features',
'all_items' => 'All Features',
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => 'Edit Feature',
'update_item' => 'Update Feature',
'add_new_item' => 'Add New Feature',
'new_item_name' => 'New Feature Name',
'separate_items_with_commas' => 'Separate Features with commas',
'add_or_remove_items' => 'Add or remove Features',
'choose_from_most_used' => 'Choose from the most used Features',
'not_found' => 'No Features found',
'menu_name' => 'Features',
);
$args = array(
'hierarchical' => false,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'update_count_callback' => '_update_post_term_count',
'query_var' => true,
'rewrite' => array( 'slug' => 'features' ),
);
register_taxonomy( 'feature', 'review', $args );
}
// Custom messages in the admin editor notifications bar
function custom_post_type_update_messages( $messages ) {
global $post;
$post_ID = $post->ID;
$post_type = get_post_type( $post_ID );
$obj = get_post_type_object( $post_type );
$singular = $obj->labels->singular_name;
$messages[$post_type] = array(
0 => '', // Unused. Messages start at index 1.
1 => sprintf( __( '%s updated. <a href="%s" target="_blank">View %s</a>' ), esc_attr( $singular ), esc_url( get_permalink( $post_ID ) ), strtolower( $singular ) ),
2 => __( 'Custom field updated.', 'my-theme' ),
3 => __( 'Custom field deleted.', 'my-theme' ),
4 => sprintf( __( '%s updated.', 'my-theme' ), esc_attr( $singular ) ),
5 => isset( $_GET['revision']) ? sprintf( __('%2$s restored to revision from %1$s', 'my-theme' ), wp_post_revision_title( (int) $_GET['revision'], false ), esc_attr( $singular ) ) : false,
6 => sprintf( __( '%s published. <a href="%s">View %s</a>'), $singular, esc_url( get_permalink( $post_ID ) ), strtolower( $singular ) ),
7 => sprintf( __( '%s saved.', 'my-theme' ), esc_attr( $singular ) ),
8 => sprintf( __( '%s submitted. <a href="%s" target="_blank">Preview %s</a>'), $singular, esc_url( add_query_arg( 'preview', 'true', get_permalink( $post_ID ) ) ), strtolower( $singular ) ),
9 => sprintf( __( '%s scheduled for: <strong>%s</strong>. <a href="%s" target="_blank">Preview %s</a>' ), $singular, date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) ), esc_url( get_permalink( $post_ID ) ), strtolower( $singular ) ),
10 => sprintf( __( '%s draft updated. <a href="%s" target="_blank">Preview %s</a>'), $singular, esc_url( add_query_arg( 'preview', 'true', get_permalink( $post_ID ) ) ), strtolower( $singular ) )
);
return $messages;
}
add_filter( 'post_updated_messages', 'custom_post_type_update_messages' );
// http://thomasmaxson.com/update-messages-for-custom-post-types/