forked from devinsays/portfolio-post-type
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathportfolio-post-type.php
313 lines (259 loc) · 11.6 KB
/
portfolio-post-type.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
<?php
/*
Plugin Name: Portfolio Post Type
Plugin URI: http://www.wptheming.com
Description: Enables a portfolio post type and taxonomies.
Version: 0.4
Author: Devin Price
Author URI: http://wptheming.com/portfolio-post-type/
License: GPLv2
*/
if ( ! class_exists( 'Portfolio_Post_Type' ) ) :
class Portfolio_Post_Type {
// Current plugin version
var $version = 0.4;
function __construct() {
// Runs when the plugin is activated
register_activation_hook( __FILE__, array( &$this, 'plugin_activation' ) );
// Add support for translations
load_plugin_textdomain( 'portfolioposttype', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
// Adds the portfolio post type and taxonomies
add_action( 'init', array( &$this, 'portfolio_init' ) );
// Thumbnail support for portfolio posts
add_theme_support( 'post-thumbnails', array( 'portfolio' ) );
// Adds columns in the admin view for thumbnail and taxonomies
add_filter( 'manage_edit-portfolio_columns', array( &$this, 'portfolio_edit_columns' ) );
add_action( 'manage_posts_custom_column', array( &$this, 'portfolio_column_display' ), 10, 2 );
// Allows filtering of posts by taxonomy in the admin view
add_action( 'restrict_manage_posts', array( &$this, 'portfolio_add_taxonomy_filters' ) );
// Show portfolio post counts in the dashboard
add_action( 'right_now_content_table_end', array( &$this, 'add_portfolio_counts' ) );
// Give the portfolio menu item a unique icon
add_action( 'admin_head', array( &$this, 'portfolio_icon' ) );
}
/**
* Flushes rewrite rules on plugin activation to ensure portfolio posts don't 404
* http://codex.wordpress.org/Function_Reference/flush_rewrite_rules
*/
function plugin_activation() {
$this->portfolio_init();
flush_rewrite_rules();
}
function portfolio_init() {
/**
* Enable the Portfolio custom post type
* http://codex.wordpress.org/Function_Reference/register_post_type
*/
$labels = array(
'name' => __( 'Portfolio', 'portfolioposttype' ),
'singular_name' => __( 'Portfolio Item', 'portfolioposttype' ),
'add_new' => __( 'Add New Item', 'portfolioposttype' ),
'add_new_item' => __( 'Add New Portfolio Item', 'portfolioposttype' ),
'edit_item' => __( 'Edit Portfolio Item', 'portfolioposttype' ),
'new_item' => __( 'Add New Portfolio Item', 'portfolioposttype' ),
'view_item' => __( 'View Item', 'portfolioposttype' ),
'search_items' => __( 'Search Portfolio', 'portfolioposttype' ),
'not_found' => __( 'No portfolio items found', 'portfolioposttype' ),
'not_found_in_trash' => __( 'No portfolio items found in trash', 'portfolioposttype' )
);
$args = array(
'labels' => $labels,
'public' => true,
'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail', 'comments', 'custom-fields', 'revisions' ),
'capability_type' => 'post',
'rewrite' => array("slug" => "portfolio"), // Permalinks format
'menu_position' => 5,
'has_archive' => true
);
register_post_type( 'portfolio', $args );
/**
* Register a taxonomy for Portfolio Tags
* http://codex.wordpress.org/Function_Reference/register_taxonomy
*/
$taxonomy_portfolio_tag_labels = array(
'name' => _x( 'Portfolio Tags', 'portfolioposttype' ),
'singular_name' => _x( 'Portfolio Tag', 'portfolioposttype' ),
'search_items' => _x( 'Search Portfolio Tags', 'portfolioposttype' ),
'popular_items' => _x( 'Popular Portfolio Tags', 'portfolioposttype' ),
'all_items' => _x( 'All Portfolio Tags', 'portfolioposttype' ),
'parent_item' => _x( 'Parent Portfolio Tag', 'portfolioposttype' ),
'parent_item_colon' => _x( 'Parent Portfolio Tag:', 'portfolioposttype' ),
'edit_item' => _x( 'Edit Portfolio Tag', 'portfolioposttype' ),
'update_item' => _x( 'Update Portfolio Tag', 'portfolioposttype' ),
'add_new_item' => _x( 'Add New Portfolio Tag', 'portfolioposttype' ),
'new_item_name' => _x( 'New Portfolio Tag Name', 'portfolioposttype' ),
'separate_items_with_commas' => _x( 'Separate portfolio tags with commas', 'portfolioposttype' ),
'add_or_remove_items' => _x( 'Add or remove portfolio tags', 'portfolioposttype' ),
'choose_from_most_used' => _x( 'Choose from the most used portfolio tags', 'portfolioposttype' ),
'menu_name' => _x( 'Portfolio Tags', 'portfolioposttype' )
);
$taxonomy_portfolio_tag_args = array(
'labels' => $taxonomy_portfolio_tag_labels,
'public' => true,
'show_in_nav_menus' => true,
'show_ui' => true,
'show_tagcloud' => true,
'hierarchical' => false,
'rewrite' => array( 'slug' => 'portfolio_tag' ),
'query_var' => true
);
register_taxonomy( 'portfolio_tag', array( 'portfolio' ), $taxonomy_portfolio_tag_args );
/**
* Register a taxonomy for Portfolio Categories
* http://codex.wordpress.org/Function_Reference/register_taxonomy
*/
$taxonomy_portfolio_category_labels = array(
'name' => _x( 'Portfolio Categories', 'portfolioposttype' ),
'singular_name' => _x( 'Portfolio Category', 'portfolioposttype' ),
'search_items' => _x( 'Search Portfolio Categories', 'portfolioposttype' ),
'popular_items' => _x( 'Popular Portfolio Categories', 'portfolioposttype' ),
'all_items' => _x( 'All Portfolio Categories', 'portfolioposttype' ),
'parent_item' => _x( 'Parent Portfolio Category', 'portfolioposttype' ),
'parent_item_colon' => _x( 'Parent Portfolio Category:', 'portfolioposttype' ),
'edit_item' => _x( 'Edit Portfolio Category', 'portfolioposttype' ),
'update_item' => _x( 'Update Portfolio Category', 'portfolioposttype' ),
'add_new_item' => _x( 'Add New Portfolio Category', 'portfolioposttype' ),
'new_item_name' => _x( 'New Portfolio Category Name', 'portfolioposttype' ),
'separate_items_with_commas' => _x( 'Separate portfolio categories with commas', 'portfolioposttype' ),
'add_or_remove_items' => _x( 'Add or remove portfolio categories', 'portfolioposttype' ),
'choose_from_most_used' => _x( 'Choose from the most used portfolio categories', 'portfolioposttype' ),
'menu_name' => _x( 'Portfolio Categories', 'portfolioposttype' ),
);
$taxonomy_portfolio_category_args = array(
'labels' => $taxonomy_portfolio_category_labels,
'public' => true,
'show_in_nav_menus' => true,
'show_ui' => true,
'show_tagcloud' => true,
'hierarchical' => true,
'rewrite' => array( 'slug' => 'portfolio_category' ),
'query_var' => true
);
register_taxonomy( 'portfolio_category', array( 'portfolio' ), $taxonomy_portfolio_category_args );
}
/**
* Add Columns to Portfolio Edit Screen
* http://wptheming.com/2010/07/column-edit-pages/
*/
function portfolio_edit_columns( $portfolio_columns ) {
$portfolio_columns = array(
"cb" => "<input type=\"checkbox\" />",
"title" => _x('Title', 'column name'),
"thumbnail" => __('Thumbnail', 'portfolioposttype'),
"portfolio_category" => __('Category', 'portfolioposttype'),
"portfolio_tag" => __('Tags', 'portfolioposttype'),
"author" => __('Author', 'portfolioposttype'),
"comments" => __('Comments', 'portfolioposttype'),
"date" => __('Date', 'portfolioposttype'),
);
$portfolio_columns['comments'] = '<div class="vers"><img alt="Comments" src="' . esc_url( admin_url( 'images/comment-grey-bubble.png' ) ) . '" /></div>';
return $portfolio_columns;
}
function portfolio_column_display( $portfolio_columns, $post_id ) {
// Code from: http://wpengineer.com/display-post-thumbnail-post-page-overview
switch ( $portfolio_columns ) {
// Display the thumbnail in the column view
case "thumbnail":
$width = (int) 35;
$height = (int) 35;
$thumbnail_id = get_post_meta( $post_id, '_thumbnail_id', true );
// Display the featured image in the column view if possible
if ( $thumbnail_id ) {
$thumb = wp_get_attachment_image( $thumbnail_id, array($width, $height), true );
}
if ( isset( $thumb ) ) {
echo $thumb;
} else {
echo __('None', 'portfolioposttype');
}
break;
// Display the portfolio tags in the column view
case "portfolio_category":
if ( $category_list = get_the_term_list( $post_id, 'portfolio_category', '', ', ', '' ) ) {
echo $category_list;
} else {
echo __('None', 'portfolioposttype');
}
break;
// Display the portfolio tags in the column view
case "portfolio_tag":
if ( $tag_list = get_the_term_list( $post_id, 'portfolio_tag', '', ', ', '' ) ) {
echo $tag_list;
} else {
echo __('None', 'portfolioposttype');
}
break;
}
}
/**
* Adds taxonomy filters to the portfolio admin page
* Code artfully lifed from http://pippinsplugins.com
*/
function portfolio_add_taxonomy_filters() {
global $typenow;
// An array of all the taxonomyies you want to display. Use the taxonomy name or slug
$taxonomies = array( 'portfolio_category', 'portfolio_tag' );
// must set this to the post type you want the filter(s) displayed on
if ( $typenow == 'portfolio' ) {
foreach ( $taxonomies as $tax_slug ) {
$current_tax_slug = isset( $_GET[$tax_slug] ) ? $_GET[$tax_slug] : false;
$tax_obj = get_taxonomy( $tax_slug );
$tax_name = $tax_obj->labels->name;
$terms = get_terms($tax_slug);
if ( count( $terms ) > 0) {
echo "<select name='$tax_slug' id='$tax_slug' class='postform'>";
echo "<option value=''>$tax_name</option>";
foreach ( $terms as $term ) {
echo '<option value=' . $term->slug, $current_tax_slug == $term->slug ? ' selected="selected"' : '','>' . $term->name .' (' . $term->count .')</option>';
}
echo "</select>";
}
}
}
}
/**
* Add Portfolio count to "Right Now" Dashboard Widget
*/
function add_portfolio_counts() {
if ( ! post_type_exists( 'portfolio' ) ) {
return;
}
$num_posts = wp_count_posts( 'portfolio' );
$num = number_format_i18n( $num_posts->publish );
$text = _n( 'Portfolio Item', 'Portfolio Items', intval($num_posts->publish) );
if ( current_user_can( 'edit_posts' ) ) {
$num = "<a href='edit.php?post_type=portfolio'>$num</a>";
$text = "<a href='edit.php?post_type=portfolio'>$text</a>";
}
echo '<td class="first b b-portfolio">' . $num . '</td>';
echo '<td class="t portfolio">' . $text . '</td>';
echo '</tr>';
if ($num_posts->pending > 0) {
$num = number_format_i18n( $num_posts->pending );
$text = _n( 'Portfolio Item Pending', 'Portfolio Items Pending', intval($num_posts->pending) );
if ( current_user_can( 'edit_posts' ) ) {
$num = "<a href='edit.php?post_status=pending&post_type=portfolio'>$num</a>";
$text = "<a href='edit.php?post_status=pending&post_type=portfolio'>$text</a>";
}
echo '<td class="first b b-portfolio">' . $num . '</td>';
echo '<td class="t portfolio">' . $text . '</td>';
echo '</tr>';
}
}
/**
* Displays the custom post type icon in the dashboard
*/
function portfolio_icon() { ?>
<style type="text/css" media="screen">
#menu-posts-portfolio .wp-menu-image {
background: url(<?php echo plugin_dir_url( __FILE__ ); ?>images/portfolio-icon.png) no-repeat 6px 6px !important;
}
#menu-posts-portfolio:hover .wp-menu-image, #menu-posts-portfolio.wp-has-current-submenu .wp-menu-image {
background-position:6px -16px !important;
}
#icon-edit.icon32-posts-portfolio {background: url(<?php echo plugin_dir_url( __FILE__ ); ?>images/portfolio-32x32.png) no-repeat;}
</style>
<?php }
}
new Portfolio_Post_Type;
endif;