Multiple Post Thumbnails - Version 0.8

Version Description

Download this release

Release Info

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

Code changes from version 0.6 to 0.8

multi-post-thumbnails.php CHANGED
@@ -3,7 +3,7 @@
3
  Plugin Name: Multiple Post Thumbnails
4
  Plugin URI: http://wordpress.org/extend/plugins/multiple-post-thumbnails/
5
  Description: Adds the ability to add multiple post thumbnails to a post type.
6
- Version: 0.6
7
  Author: Chris Scott
8
  Author URI: http://vocecommuncations.com/
9
  */
@@ -79,6 +79,10 @@ if (!class_exists('MultiPostThumbnails')) {
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'));
@@ -121,7 +125,7 @@ if (!class_exists('MultiPostThumbnails')) {
121
 
122
  // check the post type to see if link needs to be added
123
  $calling_post = get_post($calling_post_id);
124
- if ($calling_post && $calling_post->post_type != $this->post_type) {
125
  return $form_fields;
126
  }
127
 
@@ -140,7 +144,29 @@ if (!class_exists('MultiPostThumbnails')) {
140
  * @return void
141
  */
142
  public function enqueue_admin_scripts() {
143
- wp_enqueue_script("featured-image-custom", plugins_url(basename(dirname(__FILE__)) . '/js/multi-post-thumbnails-admin.js'), array('jquery'));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
144
  }
145
 
146
  /**
3
  Plugin Name: Multiple Post Thumbnails
4
  Plugin URI: http://wordpress.org/extend/plugins/multiple-post-thumbnails/
5
  Description: Adds the ability to add multiple post thumbnails to a post type.
6
+ Version: 0.7
7
  Author: Chris Scott
8
  Author URI: http://vocecommuncations.com/
9
  */
79
  add_theme_support( 'post-thumbnails' );
80
  }
81
 
82
+ add_action('init', array($this, 'action_init'));
83
+ }
84
+
85
+ public function action_init() {
86
  add_action('add_meta_boxes', array($this, 'add_metabox'));
87
  add_filter('attachment_fields_to_edit', array($this, 'add_attachment_field'), 20, 2);
88
  add_action('admin_init', array($this, 'enqueue_admin_scripts'));
125
 
126
  // check the post type to see if link needs to be added
127
  $calling_post = get_post($calling_post_id);
128
+ if (is_null($calling_post) || $calling_post->post_type != $this->post_type) {
129
  return $form_fields;
130
  }
131
 
144
  * @return void
145
  */
146
  public function enqueue_admin_scripts() {
147
+ wp_enqueue_script("featured-image-custom", $this->plugins_url('js/multi-post-thumbnails-admin.js', __FILE__), array('jquery'));
148
+ }
149
+
150
+ private function plugins_url($relative_path, $plugin_path) {
151
+ $template_dir = get_template_directory();
152
+
153
+ foreach ( array('template_dir', 'plugin_path') as $var ) {
154
+ $$var = str_replace('\\' ,'/', $$var); // sanitize for Win32 installs
155
+ $$var = preg_replace('|/+|', '/', $$var);
156
+ }
157
+ if(0 === strpos($plugin_path, $template_dir)) {
158
+ $url = get_template_directory_uri();
159
+ $folder = str_replace($template_dir, '', dirname($plugin_path));
160
+ if ( '.' != $folder ) {
161
+ $url .= '/' . ltrim($folder, '/');
162
+ }
163
+ if ( !empty($relative_path) && is_string($relative_path) && strpos($relative_path, '..') === false ) {
164
+ $url .= '/' . ltrim($relative_path, '/');
165
+ }
166
+ return $url;
167
+ } else {
168
+ return plugins_url($relative_path, $plugin_path);
169
+ }
170
  }
171
 
172
  /**
readme.txt CHANGED
@@ -2,8 +2,8 @@
2
  Contributors: chrisscott
3
  Tags: thumbnails, image
4
  Requires at least: 2.9.2
5
- Tested up to: 3.1.3
6
- Stable tag: 0.6
7
 
8
  Adds multiple post thumbnails to a post type. If you've ever wanted more than one Featured Image on a post, this plugin is for you.
9
 
@@ -13,14 +13,14 @@ Adds multiple post thumbnails to a post type. If you've ever wanted more than on
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
- if (class_exists('MultiPostThumbnails')) {
17
  new MultiPostThumbnails(array(
18
  'label' => 'Secondary Image',
19
  'id' => 'secondary-image',
20
  'post_type' => 'post'
21
  )
22
  );
23
- }
24
  4. Display the thumbnail in your theme:
25
 
26
  <?php if (class_exists('MultiPostThumbnails')
@@ -69,6 +69,9 @@ You can register multiple image sizes for a given thumbnail if desired.
69
 
70
  == Changelog ==
71
 
 
 
 
72
  = 0.6 =
73
  * Update `get_the_post_thumbnail` return filter to use format `{$post_type}_{$thumb_id}_thumbnail_html` which allows filtering by post type and thumbnail id which was the intent. Props gordonbrander.
74
  * Update plugin URL to point to Plugin Directory
2
  Contributors: chrisscott
3
  Tags: thumbnails, image
4
  Requires at least: 2.9.2
5
+ Tested up to: 3.2.1
6
+ Stable tag: 0.7
7
 
8
  Adds multiple post thumbnails to a post type. If you've ever wanted more than one Featured Image on a post, this plugin is for you.
9
 
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
+ if (class_exists('MultiPostThumbnails')) {
17
  new MultiPostThumbnails(array(
18
  'label' => 'Secondary Image',
19
  'id' => 'secondary-image',
20
  'post_type' => 'post'
21
  )
22
  );
23
+ }
24
  4. Display the thumbnail in your theme:
25
 
26
  <?php if (class_exists('MultiPostThumbnails')
69
 
70
  == Changelog ==
71
 
72
+ = 0.7 +
73
+ * Add actions/filters on init action. Should fix admin metaboxes not showing or showing out of order. props arizzitano.
74
+
75
  = 0.6 =
76
  * Update `get_the_post_thumbnail` return filter to use format `{$post_type}_{$thumb_id}_thumbnail_html` which allows filtering by post type and thumbnail id which was the intent. Props gordonbrander.
77
  * Update plugin URL to point to Plugin Directory
trunk/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
+ }
trunk/multi-post-thumbnails.php ADDED
@@ -0,0 +1,306 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /*
3
+ Plugin Name: Multiple Post Thumbnails
4
+ Plugin URI: http://wordpress.org/extend/plugins/multiple-post-thumbnails/
5
+ Description: Adds the ability to add multiple post thumbnails to a post type.
6
+ Version: 0.8
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
+
122
+ // check the post type to see if link needs to be added
123
+ $calling_post = get_post($calling_post_id);
124
+ if (is_null($calling_post) || $calling_post->post_type != $this->post_type) {
125
+ return $form_fields;
126
+ }
127
+
128
+ $ajax_nonce = wp_create_nonce("set_post_thumbnail-{$this->post_type}-{$this->id}-{$calling_post_id}");
129
+ $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);
130
+ $form_fields["{$this->post_type}-{$this->id}-thumbnail"] = array(
131
+ 'label' => $this->label,
132
+ 'input' => 'html',
133
+ 'html' => $link);
134
+ return $form_fields;
135
+ }
136
+
137
+ /**
138
+ * Enqueue admin JavaScripts
139
+ *
140
+ * @return void
141
+ */
142
+ public function enqueue_admin_scripts() {
143
+ wp_enqueue_script("featured-image-custom", $this->plugins_url('js/multi-post-thumbnails-admin.js', __FILE__), array('jquery'));
144
+ }
145
+
146
+ private function plugins_url($relative_path, $plugin_path) {
147
+ $template_dir = get_template_directory();
148
+
149
+ foreach ( array('template_dir', 'plugin_path') as $var ) {
150
+ $$var = str_replace('\\' ,'/', $$var); // sanitize for Win32 installs
151
+ $$var = preg_replace('|/+|', '/', $$var);
152
+ }
153
+ if(0 === strpos($plugin_path, $template_dir)) {
154
+ $url = get_template_directory_uri();
155
+ $folder = str_replace($template_dir, '', dirname($plugin_path));
156
+ if ( '.' != $folder ) {
157
+ $url .= '/' . ltrim($folder, '/');
158
+ }
159
+ if ( !empty($relative_path) && is_string($relative_path) && strpos($relative_path, '..') === false ) {
160
+ $url .= '/' . ltrim($relative_path, '/');
161
+ }
162
+ return $url;
163
+ } else {
164
+ return plugins_url($relative_path, $plugin_path);
165
+ }
166
+ }
167
+
168
+ /**
169
+ * Check if post has an image attached.
170
+ *
171
+ * @param string $post_type The post type.
172
+ * @param string $id The id used to register the thumbnail.
173
+ * @param string $post_id Optional. Post ID.
174
+ * @return bool Whether post has an image attached.
175
+ */
176
+ public static function has_post_thumbnail($post_type, $id, $post_id = null) {
177
+ if (null === $post_id) {
178
+ $post_id = get_the_ID();
179
+ }
180
+
181
+ if (!$post_id) {
182
+ return false;
183
+ }
184
+
185
+ return get_post_meta($post_id, "{$post_type}_{$id}_thumbnail_id", true);
186
+ }
187
+
188
+ /**
189
+ * Display Post Thumbnail.
190
+ *
191
+ * @param string $post_type The post type.
192
+ * @param string $thumb_id The id used to register the thumbnail.
193
+ * @param string $post_id Optional. Post ID.
194
+ * @param int $size Optional. Image size. Defaults to 'post-thumbnail', which theme sets using set_post_thumbnail_size( $width, $height, $crop_flag );.
195
+ * @param string|array $attr Optional. Query string or array of attributes.
196
+ * @param bool $link_to_original Optional. Wrap link to original image around thumbnail?
197
+ */
198
+ public static function the_post_thumbnail($post_type, $thumb_id, $post_id = null, $size = 'post-thumbnail', $attr = '', $link_to_original = false) {
199
+ echo self::get_the_post_thumbnail($post_type, $thumb_id, $post_id, $size, $attr, $link_to_original);
200
+ }
201
+
202
+ /**
203
+ * Retrieve Post Thumbnail.
204
+ *
205
+ * @param string $post_type The post type.
206
+ * @param string $thumb_id The id used to register the thumbnail.
207
+ * @param int $post_id Optional. Post ID.
208
+ * @param string $size Optional. Image size. Defaults to 'thumbnail'.
209
+ * @param bool $link_to_original Optional. Wrap link to original image around thumbnail?
210
+ * @param string|array $attr Optional. Query string or array of attributes.
211
+ */
212
+ public static function get_the_post_thumbnail($post_type, $thumb_id, $post_id = NULL, $size = 'post-thumbnail', $attr = '' , $link_to_original = false) {
213
+ global $id;
214
+ $post_id = (NULL === $post_id) ? $id : $post_id;
215
+ $post_thumbnail_id = self::get_post_thumbnail_id($post_type, $thumb_id, $post_id);
216
+ $size = apply_filters("{$post_type}_{$post_id}_thumbnail_size", $size);
217
+ if ($post_thumbnail_id) {
218
+ 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
219
+ $html = wp_get_attachment_image( $post_thumbnail_id, $size, false, $attr );
220
+ do_action("end_fetch_multi_{$post_type}_thumbnail_html", $post_id, $post_thumbnail_id, $size);
221
+ } else {
222
+ $html = '';
223
+ }
224
+
225
+ if ($link_to_original) {
226
+ $html = sprintf('<a href="%s">%s</a>', wp_get_attachment_url($post_thumbnail_id), $html);
227
+ }
228
+
229
+ return apply_filters("{$post_type}_{$thumb_id}_thumbnail_html", $html, $post_id, $post_thumbnail_id, $size, $attr);
230
+ }
231
+
232
+ /**
233
+ * Retrieve Post Thumbnail ID.
234
+ *
235
+ * @param string $post_type The post type.
236
+ * @param string $id The id used to register the thumbnail.
237
+ * @param int $post_id Optional. Post ID.
238
+ * @return int
239
+ */
240
+ public static function get_post_thumbnail_id($post_type, $id, $post_id) {
241
+ return get_post_meta($post_id, "{$post_type}_{$id}_thumbnail_id", true);
242
+ }
243
+
244
+ /**
245
+ * Output the post thumbnail HTML for the metabox and AJAX callbacks
246
+ *
247
+ * @param string $thumbnail_id The thumbnail's post ID.
248
+ * @return string HTML
249
+ */
250
+ private function post_thumbnail_html($thumbnail_id = NULL) {
251
+ global $content_width, $_wp_additional_image_sizes, $post_ID;
252
+
253
+ $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);
254
+ $content = sprintf($set_thumbnail_link, esc_html__( "Set {$this->label}" ));
255
+
256
+
257
+ if ($thumbnail_id && get_post($thumbnail_id)) {
258
+ $old_content_width = $content_width;
259
+ $content_width = 266;
260
+ if ( !isset($_wp_additional_image_sizes["{$this->post_type}-{$this->id}-thumbnail"]))
261
+ $thumbnail_html = wp_get_attachment_image($thumbnail_id, array($content_width, $content_width));
262
+ else
263
+ $thumbnail_html = wp_get_attachment_image($thumbnail_id, "{$this->post_type}-{$this->id}-thumbnail");
264
+ if (!empty($thumbnail_html)) {
265
+ $ajax_nonce = wp_create_nonce("set_post_thumbnail-{$this->post_type}-{$this->id}-{$post_ID}");
266
+ $content = sprintf($set_thumbnail_link, $thumbnail_html);
267
+ $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);
268
+ }
269
+ $content_width = $old_content_width;
270
+ }
271
+
272
+ return $content;
273
+ }
274
+
275
+ /**
276
+ * Set/remove the post thumbnail. AJAX handler.
277
+ *
278
+ * @return string Updated post thumbnail HTML.
279
+ */
280
+ public function set_thumbnail() {
281
+ global $post_ID; // have to do this so get_upload_iframe_src() can grab it
282
+ $post_ID = intval($_POST['post_id']);
283
+ if ( !current_user_can('edit_post', $post_ID))
284
+ die('-1');
285
+ $thumbnail_id = intval($_POST['thumbnail_id']);
286
+
287
+ check_ajax_referer("set_post_thumbnail-{$this->post_type}-{$this->id}-{$post_ID}");
288
+
289
+ if ($thumbnail_id == '-1') {
290
+ delete_post_meta($post_ID, "{$this->post_type}_{$this->id}_thumbnail_id");
291
+ die($this->post_thumbnail_html(NULL));
292
+ }
293
+
294
+ if ($thumbnail_id && get_post($thumbnail_id)) {
295
+ $thumbnail_html = wp_get_attachment_image($thumbnail_id, 'thumbnail');
296
+ if (!empty($thumbnail_html)) {
297
+ update_post_meta($post_ID, "{$this->post_type}_{$this->id}_thumbnail_id", $thumbnail_id);
298
+ die($this->post_thumbnail_html($thumbnail_id));
299
+ }
300
+ }
301
+
302
+ die('0');
303
+ }
304
+
305
+ }
306
+ }
trunk/readme.txt ADDED
@@ -0,0 +1,101 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ === Plugin Name ===
2
+ Contributors: chrisscott
3
+ Tags: thumbnails, image
4
+ Requires at least: 2.9.2
5
+ Tested up to: 3.2.1
6
+ Stable tag: 0.8
7
+
8
+ Adds multiple post thumbnails to a post type. If you've ever wanted more than one Featured Image on a post, this plugin is for you.
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
+ if (class_exists('MultiPostThumbnails')) {
17
+ new MultiPostThumbnails(array(
18
+ 'label' => 'Secondary Image',
19
+ 'id' => 'secondary-image',
20
+ 'post_type' => 'post'
21
+ )
22
+ );
23
+ }
24
+ 4. Display the thumbnail in your theme:
25
+
26
+ <?php if (class_exists('MultiPostThumbnails')
27
+ && MultiPostThumbnails::has_post_thumbnail('post', 'secondary-image')) :
28
+ MultiPostThumbnails::the_post_thumbnail('post', 'secondary-image'); endif; ?>
29
+
30
+ == Frequently Asked Questions ==
31
+
32
+ = I'm trying to upgrade to a new verions of WordPress and get an error about `MultiPostThumbnails` =
33
+
34
+ This is caused by using the example in previous readmes that didn't do a check for the `MultiPostThumbnails` class existing first. This has been corrected in the Installation section.
35
+
36
+ = How do I register the same thumbnail for multiple post types? =
37
+
38
+ You can loop through an array of the post types:
39
+
40
+ if (class_exists('MultiPostThumbnails')) {
41
+ $types = array('post', 'page', 'my_post_type');
42
+ foreach($types as $type) {
43
+ $thumb = new MultiPostThumbnails(array(
44
+ 'label' => 'Secondary Image',
45
+ 'id' => 'secondary-image',
46
+ 'post_type' => $type
47
+ )
48
+ );
49
+ }
50
+ }
51
+
52
+ = How do I use a custom thumbnail size in my theme? =
53
+
54
+ 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:
55
+
56
+ `add_image_size('post-secondary-image-thumbnail', 250, 150);`
57
+
58
+ 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:
59
+
60
+ `MultiPostThumbnails::the_post_thumbnail('post', 'secondary-image', NULL, 'post-secondary-image-thumbnail');`
61
+
62
+ You can register multiple image sizes for a given thumbnail if desired.
63
+
64
+ == Screenshots ==
65
+
66
+ 1. Admin meta box showing a new thumbnail named 'Secondary Image'.
67
+ 2. Media screen showing the link to use the image as the 'Secondary Image'.
68
+ 3. Admin meta box with the 'Secondary Image' selected.
69
+
70
+ == Changelog ==
71
+
72
+ = 0.8 =
73
+ * Revert init action changes from 0.7. Fixes admin metaboxes not showing when the MultiPostThumbnails class is instantiated in an action instead of `functions.php`
74
+
75
+ = 0.7 =
76
+ * Add actions/filters on init action. Should fix admin metaboxes not showing or showing out of order. props arizzitano.
77
+
78
+ = 0.6 =
79
+ * Update `get_the_post_thumbnail` return filter to use format `{$post_type}_{$thumb_id}_thumbnail_html` which allows filtering by post type and thumbnail id which was the intent. Props gordonbrander.
80
+ * Update plugin URL to point to Plugin Directory
81
+
82
+ = 0.5 =
83
+ * Update readme to check for `MultiPostThumbnails` class before calling.
84
+
85
+ = 0.4 =
86
+ * Added: optional argument `$link_to_original` to *_the_post_thumbnails template tags. Thanks to gfors for the suggestion.
87
+ * Fixed: PHP warning in media manager due to non-existent object
88
+
89
+ = 0.3 =
90
+ * Fixed: when displaying the insert link in the media library, check the post_type so it only shows for the registered type.
91
+
92
+ = 0.2 =
93
+ * Update docs and screenshots. Update tested through to 3.0 release.
94
+
95
+ = 0.1 =
96
+ * Initial release.
97
+
98
+ == Upgrade Notice ==
99
+
100
+ = 0.6 =
101
+ `get_the_post_thumbnail` return filter changed to use the format `{$post_type}_{$thumb_id}_thumbnail_html` which allows filtering by post type and thumbnail id which was the intent.
trunk/screenshot-1.png ADDED
Binary file
trunk/screenshot-2.png ADDED
Binary file
trunk/screenshot-3.png ADDED
Binary file