Multiple Post Thumbnails - Version 0.1

Version Description

  • Initial release.
Download this release

Release Info

Developer chrisscott
Plugin Icon wp plugin Multiple Post Thumbnails
Version 0.1
Comparing to
See all releases

Version 0.1

js/multi-post-thumbnails-admin.js ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ function MultiPostThumbnailsSetThumbnailHTML(html, id, post_type){
2
+ jQuery('.inside', '#' + post_type + '-' + id).html(html);
3
+ };
4
+
5
+ function MultiPostThumbnailsSetThumbnailID(thumb_id, id, post_type){
6
+ var field = jQuery('input[value=_' + post_type + '_' + id + '_thumbnail_id]', '#list-table');
7
+ if ( field.size() > 0 ) {
8
+ jQuery('#meta\\[' + field.attr('id').match(/[0-9]+/) + '\\]\\[value\\]').text(thumb_id);
9
+ }
10
+ };
11
+
12
+ function MultiPostThumbnailsRemoveThumbnail(id, post_type, nonce){
13
+ jQuery.post(ajaxurl, {
14
+ action:'set-' + post_type + '-' + id + '-thumbnail', post_id: jQuery('#post_ID').val(), thumbnail_id: -1, _ajax_nonce: nonce, cookie: encodeURIComponent(document.cookie)
15
+ }, function(str){
16
+ if ( str == '0' ) {
17
+ alert( setPostThumbnailL10n.error );
18
+ } else {
19
+ MultiPostThumbnailsSetThumbnailHTML(str, id, post_type);
20
+ }
21
+ }
22
+ );
23
+ };
24
+
25
+
26
+ function MultiPostThumbnailsSetAsThumbnail(thumb_id, id, post_type, nonce){
27
+ var $link = jQuery('a#' + post_type + '-' + id + '-thumbnail-' + thumb_id);
28
+
29
+ $link.text( setPostThumbnailL10n.saving );
30
+ jQuery.post(ajaxurl, {
31
+ action:'set-' + post_type + '-' + id + '-thumbnail', post_id: post_id, thumbnail_id: thumb_id, _ajax_nonce: nonce, cookie: encodeURIComponent(document.cookie)
32
+ }, function(str){
33
+ var win = window.dialogArguments || opener || parent || top;
34
+ $link.text( setPostThumbnailL10n.setThumbnail );
35
+ if ( str == '0' ) {
36
+ alert( setPostThumbnailL10n.error );
37
+ } else {
38
+ $link.show();
39
+ $link.text( setPostThumbnailL10n.done );
40
+ $link.fadeOut( 2000, function() {
41
+ jQuery('tr.' + post_type + '-' + id + '-thumbnail').hide();
42
+ });
43
+ win.MultiPostThumbnailsSetThumbnailID(thumb_id, id, post_type);
44
+ win.MultiPostThumbnailsSetThumbnailHTML(str, id, post_type);
45
+ }
46
+ }
47
+ );
48
+ }
multi-post-thumbnails.php ADDED
@@ -0,0 +1,270 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /*
3
+ Plugin Name: Multi Post Thumnails
4
+ Plugin URI: http://vocecommunications.com/
5
+ Description: Adds the ability to add multiple post thumbnails to a post type.
6
+ Version: 0.1
7
+ Author: Chris Scott
8
+ Author URI: http://vocecommuncations.com/
9
+ */
10
+
11
+ /* Copyright 2010 Chris Scott (cscott@voceconnect.com)
12
+
13
+ This program is free software; you can redistribute it and/or modify
14
+ it under the terms of the GNU General Public License, version 2, as
15
+ published by the Free Software Foundation.
16
+
17
+ This program is distributed in the hope that it will be useful,
18
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
19
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20
+ GNU General Public License for more details.
21
+
22
+ You should have received a copy of the GNU General Public License
23
+ along with this program; if not, write to the Free Software
24
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25
+ */
26
+
27
+
28
+ if (!class_exists('MultiPostThumbnails')) {
29
+
30
+ class MultiPostThumbnails {
31
+
32
+ public function __construct($args = array()) {
33
+ $this->register($args);
34
+ }
35
+
36
+ /**
37
+ * Register a new post thumbnail.
38
+ *
39
+ * Required $args contents:
40
+ *
41
+ * label - The name of the post thumbnail to display in the admin metabox
42
+ *
43
+ * id - Used to build the CSS class for the admin meta box. Needs to be unique and valid in a CSS class selector.
44
+ *
45
+ * Optional $args contents:
46
+ *
47
+ * post_type - The post type to register this thumbnail for. Defaults to post.
48
+ *
49
+ * priority - The admin metabox priority. Defaults to low to show after normal post thumbnail meta box.
50
+ *
51
+ * @param array|string $args See above description.
52
+ * @return void
53
+ */
54
+ public function register($args = array()) {
55
+ $defaults = array(
56
+ 'label' => null,
57
+ 'id' => null,
58
+ 'post_type' => 'post',
59
+ 'priority' => 'low',
60
+ );
61
+
62
+ $args = wp_parse_args($args, $defaults);
63
+
64
+ // Create and set properties
65
+ foreach($args as $k => $v) {
66
+ $this->$k = $v;
67
+ }
68
+
69
+ // Need these args to be set at a minimum
70
+ if (null === $this->label || null === $this->id) {
71
+ if (WP_DEBUG) {
72
+ trigger_error(sprintf("The 'label' and 'id' values of the 'args' parameter of '%s::%s()' are required", __CLASS__, __FUNCTION__));
73
+ }
74
+ return;
75
+ }
76
+
77
+ // add theme support if not already added
78
+ if (!current_theme_supports('post-thumbnails')) {
79
+ add_theme_support( 'post-thumbnails' );
80
+ }
81
+
82
+ add_action('add_meta_boxes', array($this, 'add_metabox'));
83
+ add_filter('attachment_fields_to_edit', array($this, 'add_attachment_field'), 20, 2);
84
+ add_action('admin_init', array($this, 'enqueue_admin_scripts'));
85
+ add_action("wp_ajax_set-{$this->post_type}-{$this->id}-thumbnail", array($this, 'set_thumbnail'));
86
+ }
87
+
88
+ /**
89
+ * Add admin metabox for thumbnail chooser
90
+ *
91
+ * @return void
92
+ */
93
+ public function add_metabox() {
94
+ add_meta_box("{$this->post_type}-{$this->id}", __($this->label), array($this, 'thumbnail_meta_box'), $this->post_type, 'side', $this->priority);
95
+ }
96
+
97
+ /**
98
+ * Output the thumbnail meta box
99
+ *
100
+ * @return string HTML output
101
+ */
102
+ public function thumbnail_meta_box() {
103
+ global $post;
104
+ $thumbnail_id = get_post_meta($post->ID, "{$this->post_type}_{$this->id}_thumbnail_id", true);
105
+ echo $this->post_thumbnail_html($thumbnail_id);
106
+ }
107
+
108
+ /**
109
+ * Throw this in the media attachment fields
110
+ *
111
+ * @param string $form_fields
112
+ * @param string $post
113
+ * @return void
114
+ */
115
+ public function add_attachment_field($form_fields, $post) {
116
+ $calling_post_id = 0;
117
+ if (isset($_GET['post_id']))
118
+ $calling_post_id = absint($_GET['post_id']);
119
+ elseif (isset($_POST) && count($_POST)) // Like for async-upload where $_GET['post_id'] isn't set
120
+ $calling_post_id = $post->post_parent;
121
+ $ajax_nonce = wp_create_nonce("set_post_thumbnail-{$this->post_type}-{$this->id}-{$calling_post_id}");
122
+ $link = sprintf('<a id="%4$s-%1$s-thumbnail-%2$s" class="%1$s-thumbnail" href="#" onclick="MultiPostThumbnailsSetAsThumbnail(\'%2$s\', \'%1$s\', \'%4$s\', \'%5$s\');return false;">Set as %3$s</a>', $this->id, $post->ID, $this->label, $this->post_type, $ajax_nonce);
123
+ $form_fields["{$this->post_type}-{$this->id}-thumbnail"] = array(
124
+ 'label' => $this->label,
125
+ 'input' => 'html',
126
+ 'html' => $link);
127
+ return $form_fields;
128
+ }
129
+
130
+ /**
131
+ * Enqueue admin JavaScripts
132
+ *
133
+ * @return void
134
+ */
135
+ public function enqueue_admin_scripts() {
136
+ wp_enqueue_script("featured-image-custom", plugins_url(basename(dirname(__FILE__)) . '/js/multi-post-thumbnails-admin.js'), array('jquery'));
137
+ }
138
+
139
+ /**
140
+ * Check if post has an image attached.
141
+ *
142
+ * @param string $post_type The post type.
143
+ * @param string $id The id used to register the thumbnail.
144
+ * @param string $post_id Optional. Post ID.
145
+ * @return bool Whether post has an image attached.
146
+ */
147
+ public static function has_post_thumbnail($post_type, $id, $post_id = null) {
148
+ if (null === $post_id) {
149
+ $post_id = get_the_ID();
150
+ }
151
+
152
+ if (!$post_id) {
153
+ return false;
154
+ }
155
+
156
+ return get_post_meta($post_id, "{$post_type}_{$id}_thumbnail_id", true);
157
+ }
158
+
159
+ /**
160
+ * Display Post Thumbnail.
161
+ *
162
+ * @param string $post_type The post type.
163
+ * @param string $id The id used to register the thumbnail.
164
+ * @param string $post_id Optional. Post ID.
165
+ * @param int $size Optional. Image size. Defaults to 'post-thumbnail', which theme sets using set_post_thumbnail_size( $width, $height, $crop_flag );.
166
+ * @param string|array $attr Optional. Query string or array of attributes.
167
+ */
168
+ public static function the_post_thumbnail($post_type, $id, $post_id = null, $size = 'post-thumbnail', $attr = '') {
169
+ echo self::get_the_post_thumbnail($post_type, $id, $post_id, $size, $attr);
170
+ }
171
+
172
+ /**
173
+ * Retrieve Post Thumbnail.
174
+ *
175
+ * @param string $post_type The post type.
176
+ * @param string $id The id used to register the thumbnail.
177
+ * @param int $post_id Optional. Post ID.
178
+ * @param string $size Optional. Image size. Defaults to 'thumbnail'.
179
+ * @param string|array $attr Optional. Query string or array of attributes.
180
+ */
181
+ public static function get_the_post_thumbnail($post_type, $thumb_id, $post_id = NULL, $size = 'post-thumbnail', $attr = '' ) {
182
+ global $id;
183
+ $post_id = (NULL === $post_id) ? $id : $post_id;
184
+ $post_thumbnail_id = self::get_post_thumbnail_id($post_type, $thumb_id, $post_id);
185
+ $size = apply_filters("{$post_type}_{$id}_thumbnail_size", $size);
186
+ if ($post_thumbnail_id) {
187
+ do_action("begin_fetch_multi_{$post_type}_thumbnail_html", $post_id, $post_thumbnail_id, $size); // for "Just In Time" filtering of all of wp_get_attachment_image()'s filters
188
+ $html = wp_get_attachment_image( $post_thumbnail_id, $size, false, $attr );
189
+ do_action("end_fetch_multi_{$post_type}_thumbnail_html", $post_id, $post_thumbnail_id, $size);
190
+ } else {
191
+ $html = '';
192
+ }
193
+ return apply_filters("{$post_type}_{$id}_thumbnail_html", $html, $post_id, $post_thumbnail_id, $size, $attr);
194
+ }
195
+
196
+ /**
197
+ * Retrieve Post Thumbnail ID.
198
+ *
199
+ * @param string $post_type The post type.
200
+ * @param string $id The id used to register the thumbnail.
201
+ * @param int $post_id Optional. Post ID.
202
+ * @return int
203
+ */
204
+ public static function get_post_thumbnail_id($post_type, $id, $post_id) {
205
+ return get_post_meta($post_id, "{$post_type}_{$id}_thumbnail_id", true);
206
+ }
207
+
208
+ /**
209
+ * Output the post thumbnail HTML for the metabox and AJAX callbacks
210
+ *
211
+ * @param string $thumbnail_id The thumbnail's post ID.
212
+ * @return string HTML
213
+ */
214
+ private function post_thumbnail_html($thumbnail_id = NULL) {
215
+ global $content_width, $_wp_additional_image_sizes, $post_ID;
216
+
217
+ $set_thumbnail_link = sprintf('<p class="hide-if-no-js"><a title="%1$s" href="%2$s" id="set-%3$s-%4$s-thumbnail" class="thickbox">%%s</a></p>', esc_attr__( "Set {$this->label}" ), get_upload_iframe_src('image'), $this->post_type, $this->id);
218
+ $content = sprintf($set_thumbnail_link, esc_html__( "Set {$this->label}" ));
219
+
220
+
221
+ if ($thumbnail_id && get_post($thumbnail_id)) {
222
+ $old_content_width = $content_width;
223
+ $content_width = 266;
224
+ if ( !isset($_wp_additional_image_sizes["{$this->post_type}-{$this->id}-thumbnail"]))
225
+ $thumbnail_html = wp_get_attachment_image($thumbnail_id, array($content_width, $content_width));
226
+ else
227
+ $thumbnail_html = wp_get_attachment_image($thumbnail_id, "{$this->post_type}-{$this->id}-thumbnail");
228
+ if (!empty($thumbnail_html)) {
229
+ $ajax_nonce = wp_create_nonce("set_post_thumbnail-{$this->post_type}-{$this->id}-{$post_ID}");
230
+ $content = sprintf($set_thumbnail_link, $thumbnail_html);
231
+ $content .= sprintf('<p class="hide-if-no-js"><a href="#" id="remove-%1$s-%2$s-thumbnail" onclick="MultiPostThumbnailsRemoveThumbnail(\'%2$s\', \'%1$s\', \'%4$s\');return false;">%3$s</a></p>', $this->post_type, $this->id, esc_html__( "Remove {$this->label}" ), $ajax_nonce);
232
+ }
233
+ $content_width = $old_content_width;
234
+ }
235
+
236
+ return $content;
237
+ }
238
+
239
+ /**
240
+ * Set/remove the post thumbnail. AJAX handler.
241
+ *
242
+ * @return string Updated post thumbnail HTML.
243
+ */
244
+ public function set_thumbnail() {
245
+ global $post_ID; // have to do this so get_upload_iframe_src() can grab it
246
+ $post_ID = intval($_POST['post_id']);
247
+ if ( !current_user_can('edit_post', $post_ID))
248
+ die('-1');
249
+ $thumbnail_id = intval($_POST['thumbnail_id']);
250
+
251
+ check_ajax_referer("set_post_thumbnail-{$this->post_type}-{$this->id}-{$post_ID}");
252
+
253
+ if ($thumbnail_id == '-1') {
254
+ delete_post_meta($post_ID, "{$this->post_type}_{$this->id}_thumbnail_id");
255
+ die($this->post_thumbnail_html(NULL));
256
+ }
257
+
258
+ if ($thumbnail_id && get_post($thumbnail_id)) {
259
+ $thumbnail_html = wp_get_attachment_image($thumbnail_id, 'thumbnail');
260
+ if (!empty($thumbnail_html)) {
261
+ update_post_meta($post_ID, "{$this->post_type}_{$this->id}_thumbnail_id", $thumbnail_id);
262
+ die($this->post_thumbnail_html($thumbnail_id));
263
+ }
264
+ }
265
+
266
+ die('0');
267
+ }
268
+
269
+ }
270
+ }
readme.txt ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ === Plugin Name ===
2
+ Contributors: chrisscott
3
+ Tags: thumbnails, image
4
+ Requires at least: 2.9.2
5
+ Tested up to: 3.0-RC2
6
+ Stable tag: 0.1
7
+
8
+ Adds the ability to add multiple post thumbnails to a post type.
9
+
10
+ == Installation ==
11
+
12
+ 1. Upload the `multi-post-thumbnails` directory to the `/wp-content/plugins/` directory
13
+ 2. Activate the plugin through the 'Plugins' menu in WordPress
14
+ 3. Register a new thumbnail for the post type you want it active for. If `post_type` is not set it defaults to `post`.
15
+
16
+ $thumb = new MultiPostThumbnails(array(
17
+ 'label' => 'Secondary Image',
18
+ 'id' => 'secondary-image',
19
+ 'post_type' => 'page'
20
+ )
21
+ );
22
+ 4. Display the thumbnail in your theme:
23
+
24
+ <?php if (class_exists('MultiPostThumbnails') && MultiPostThumbnails::has_post_thumbnail('post', 'secondary-image')) : ?>
25
+ <?php MultiPostThumbnails::the_post_thumbnail('post', 'secondary-image'); ?>
26
+ <?php endif; ?>
27
+
28
+ == Frequently Asked Questions ==
29
+
30
+ = How do I register the same thumbnail for multiple post types? =
31
+
32
+ You can loop through an array of the post types:
33
+
34
+ if (class_exists('MultiPostThumbnails')) {
35
+ $types = array('post', 'page', 'my_post_type');
36
+ foreach($types as $type) {
37
+ $thumb = new MultiPostThumbnails(array(
38
+ 'label' => 'Secondary Image',
39
+ 'id' => 'secondary-image',
40
+ 'post_type' => $type
41
+ )
42
+ );
43
+ }
44
+ }
45
+
46
+ = How do I use a custom thumbnail size in my theme? =
47
+
48
+ After you have registered a new post thumbnail, register a new image size for it. e.g if your post thumbnail `id` is `secondary-image` and it is for a `post`, it probably makes sense to use something like: `add_image_size('post-secondary-image-thumbnail', 250, 150);`
49
+
50
+ This will register a new image size of 250x150 px. Then, when you display the thumbnail in your theme, update the call to `MultiPostThumbnails::the_post_thumbnail()` to pass in the image size: `MultiPostThumbnails::the_post_thumbnail('post', 'secondary-image', NULL, 'post-secondary-image-thumbnail');`
51
+
52
+ You can register multiple image sizes for a given thumbnail if desired.
53
+
54
+ == Screenshots ==
55
+
56
+ 1. Admin meta box showing a new thumbnail named Secondary Image.
57
+
58
+ == Changelog ==
59
+
60
+ = 0.1 =
61
+ * Initial release.
screenshot-1.png ADDED
Binary file