Multiple Post Thumbnails - Version 1.4

Version Description

  • Add a context parameter to the thickbox opener to narrow down the selection in the media upload tabs to the one being set/viewed (props kevinlangleyjr) which reduces clutter when many thumbnails are registered. Refactor js to use an object (props markparolisi). Hide attachment fields on 3.5 media sidebar.
Download this release

Release Info

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

Code changes from version 1.3 to 1.4

js/multi-post-thumbnails-admin.js CHANGED
@@ -1,48 +1,51 @@
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
  }
1
+ window.MultiPostThumbnails = {
2
+
3
+ setThumbnailHTML: function(html, id, post_type){
4
+ jQuery('.inside', '#' + post_type + '-' + id).html(html);
5
+ },
6
 
7
+ setThumbnailID: function(thumb_id, id, post_type){
8
+ var field = jQuery('input[value=_' + post_type + '_' + id + '_thumbnail_id]', '#list-table');
9
+ if ( field.size() > 0 ) {
10
+ jQuery('#meta\\[' + field.attr('id').match(/[0-9]+/) + '\\]\\[value\\]').text(thumb_id);
11
+ }
12
+ },
13
 
14
+ removeThumbnail: function(id, post_type, nonce){
15
+ jQuery.post(ajaxurl, {
16
+ action:'set-' + post_type + '-' + id + '-thumbnail', post_id: jQuery('#post_ID').val(), thumbnail_id: -1, _ajax_nonce: nonce, cookie: encodeURIComponent(document.cookie)
17
+ }, function(str){
18
+ if ( str == '0' ) {
19
+ alert( setPostThumbnailL10n.error );
20
+ } else {
21
+ MultiPostThumbnails.setThumbnailHTML(str, id, post_type);
22
+ }
23
+ }
24
+ );
25
+ },
26
 
27
 
28
+ setAsThumbnail: function(thumb_id, id, post_type, nonce){
29
+ var $link = jQuery('a#' + post_type + '-' + id + '-thumbnail-' + thumb_id);
30
 
31
+ $link.text( setPostThumbnailL10n.saving );
32
+ jQuery.post(ajaxurl, {
33
+ action:'set-' + post_type + '-' + id + '-thumbnail', post_id: post_id, thumbnail_id: thumb_id, _ajax_nonce: nonce, cookie: encodeURIComponent(document.cookie)
34
+ }, function(str){
35
+ var win = window.dialogArguments || opener || parent || top;
36
+ $link.text( setPostThumbnailL10n.setThumbnail );
37
+ if ( str == '0' ) {
38
+ alert( setPostThumbnailL10n.error );
39
+ } else {
40
+ $link.show();
41
+ $link.text( setPostThumbnailL10n.done );
42
+ $link.fadeOut( 2000, function() {
43
+ jQuery('tr.' + post_type + '-' + id + '-thumbnail').hide();
44
+ });
45
+ win.MultiPostThumbnails.setThumbnailID(thumb_id, id, post_type);
46
+ win.MultiPostThumbnails.setThumbnailHTML(str, id, post_type);
47
+ }
48
+ }
49
+ );
50
+ }
51
  }
multi-post-thumbnails.php CHANGED
@@ -3,9 +3,9 @@
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: 1.3
7
  Author: Chris Scott
8
- Author URI: http://vocecommuncations.com/
9
  */
10
 
11
  /* Copyright 2010 Chris Scott (cscott@voceconnect.com)
@@ -82,6 +82,8 @@ if (!class_exists('MultiPostThumbnails')) {
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_enqueue_scripts', array($this, 'enqueue_admin_scripts'));
 
 
85
  add_action("wp_ajax_set-{$this->post_type}-{$this->id}-thumbnail", array($this, 'set_thumbnail'));
86
  add_action('delete_attachment', array($this, 'action_delete_attachment'));
87
  }
@@ -129,8 +131,14 @@ if (!class_exists('MultiPostThumbnails')) {
129
  return $form_fields;
130
  }
131
 
 
 
 
 
 
 
132
  $ajax_nonce = wp_create_nonce("set_post_thumbnail-{$this->post_type}-{$this->id}-{$calling_post_id}");
133
- $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);
134
  $form_fields["{$this->post_type}-{$this->id}-thumbnail"] = array(
135
  'label' => $this->label,
136
  'input' => 'html',
@@ -151,6 +159,10 @@ if (!class_exists('MultiPostThumbnails')) {
151
  add_thickbox();
152
  wp_enqueue_script( "featured-image-custom", $this->plugins_url( 'js/multi-post-thumbnails-admin.js', __FILE__ ), array( 'jquery', 'media-upload' ) );
153
  }
 
 
 
 
154
 
155
  /**
156
  * Deletes the post meta data for posts when an attachment used as a
@@ -288,8 +300,10 @@ if (!class_exists('MultiPostThumbnails')) {
288
  */
289
  private function post_thumbnail_html($thumbnail_id = null) {
290
  global $content_width, $_wp_additional_image_sizes, $post_ID;
291
-
292
- $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);
 
 
293
  $content = sprintf($set_thumbnail_link, esc_html__( "Set {$this->label}" ));
294
 
295
 
@@ -303,7 +317,7 @@ if (!class_exists('MultiPostThumbnails')) {
303
  if (!empty($thumbnail_html)) {
304
  $ajax_nonce = wp_create_nonce("set_post_thumbnail-{$this->post_type}-{$this->id}-{$post_ID}");
305
  $content = sprintf($set_thumbnail_link, $thumbnail_html);
306
- $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);
307
  }
308
  $content_width = $old_content_width;
309
  }
@@ -333,13 +347,26 @@ if (!class_exists('MultiPostThumbnails')) {
333
  if ($thumbnail_id && get_post($thumbnail_id)) {
334
  $thumbnail_html = wp_get_attachment_image($thumbnail_id, 'thumbnail');
335
  if (!empty($thumbnail_html)) {
336
- update_post_meta($post_ID, "{$this->post_type}_{$this->id}_thumbnail_id", $thumbnail_id);
337
  die($this->post_thumbnail_html($thumbnail_id));
338
  }
339
  }
340
 
341
  die('0');
342
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
343
 
344
  }
345
  }
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: 1.4
7
  Author: Chris Scott
8
+ Author URI: http://voceplatforms.com/
9
  */
10
 
11
  /* Copyright 2010 Chris Scott (cscott@voceconnect.com)
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_enqueue_scripts', array($this, 'enqueue_admin_scripts'));
85
+ add_action('admin_print_styles-post-new.php', array($this, 'hide_media_sidebar_fields'));
86
+ add_action('admin_print_styles-post.php', array($this, 'hide_media_sidebar_fields'));
87
  add_action("wp_ajax_set-{$this->post_type}-{$this->id}-thumbnail", array($this, 'set_thumbnail'));
88
  add_action('delete_attachment', array($this, 'action_delete_attachment'));
89
  }
131
  return $form_fields;
132
  }
133
 
134
+ $referer = wp_get_referer();
135
+ $query_vars = wp_parse_args(parse_url($referer, PHP_URL_QUERY));
136
+
137
+ if( (isset($_REQUEST['context']) && $_REQUEST['context'] != $this->id) || (isset($query_vars['context']) && $query_vars['context'] != $this->id) )
138
+ return $form_fields;
139
+
140
  $ajax_nonce = wp_create_nonce("set_post_thumbnail-{$this->post_type}-{$this->id}-{$calling_post_id}");
141
+ $link = sprintf('<a id="%4$s-%1$s-thumbnail-%2$s" class="%1$s-thumbnail" href="#" onclick="MultiPostThumbnails.setAsThumbnail(\'%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);
142
  $form_fields["{$this->post_type}-{$this->id}-thumbnail"] = array(
143
  'label' => $this->label,
144
  'input' => 'html',
159
  add_thickbox();
160
  wp_enqueue_script( "featured-image-custom", $this->plugins_url( 'js/multi-post-thumbnails-admin.js', __FILE__ ), array( 'jquery', 'media-upload' ) );
161
  }
162
+
163
+ public function hide_media_sidebar_fields () {
164
+ echo sprintf('<style type="text/css">.media-sidebar tr.compat-field-%s-%s-thumbnail {display: none;}</style>', $this->post_type, $this->id);
165
+ }
166
 
167
  /**
168
  * Deletes the post meta data for posts when an attachment used as a
300
  */
301
  private function post_thumbnail_html($thumbnail_id = null) {
302
  global $content_width, $_wp_additional_image_sizes, $post_ID;
303
+ $image_library_url = get_upload_iframe_src('image');
304
+ // if TB_iframe is not moved to end of query string, thickbox will remove all query args after it.
305
+ $image_library_url = add_query_arg( array( 'context' => $this->id, 'TB_iframe' => 1 ), remove_query_arg( 'TB_iframe', $image_library_url ) );
306
+ $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}" ), $image_library_url, $this->post_type, $this->id);
307
  $content = sprintf($set_thumbnail_link, esc_html__( "Set {$this->label}" ));
308
 
309
 
317
  if (!empty($thumbnail_html)) {
318
  $ajax_nonce = wp_create_nonce("set_post_thumbnail-{$this->post_type}-{$this->id}-{$post_ID}");
319
  $content = sprintf($set_thumbnail_link, $thumbnail_html);
320
+ $content .= sprintf('<p class="hide-if-no-js"><a href="#" id="remove-%1$s-%2$s-thumbnail" onclick="MultiPostThumbnails.removeThumbnail(\'%2$s\', \'%1$s\', \'%4$s\');return false;">%3$s</a></p>', $this->post_type, $this->id, esc_html__( "Remove {$this->label}" ), $ajax_nonce);
321
  }
322
  $content_width = $old_content_width;
323
  }
347
  if ($thumbnail_id && get_post($thumbnail_id)) {
348
  $thumbnail_html = wp_get_attachment_image($thumbnail_id, 'thumbnail');
349
  if (!empty($thumbnail_html)) {
350
+ $this->set_meta($post_ID, $this->post_type, $this->id, $thumbnail_id);
351
  die($this->post_thumbnail_html($thumbnail_id));
352
  }
353
  }
354
 
355
  die('0');
356
  }
357
+
358
+ /**
359
+ * set thumbnail meta
360
+ *
361
+ * @param int $post_ID
362
+ * @param string $post_type
363
+ * @param string $thumbnail_id ID used to register the thumbnail
364
+ * @param int $thumbnail_post_id ID of the attachment to use as the thumbnail
365
+ * @return bool result of update_post_meta
366
+ */
367
+ public static function set_meta($post_ID, $post_type, $thumbnail_id, $thumbnail_post_id) {
368
+ return update_post_meta($post_ID, "{$post_type}_{$thumbnail_id}_thumbnail_id", $thumbnail_post_id);
369
+ }
370
 
371
  }
372
  }
readme.txt CHANGED
@@ -2,8 +2,8 @@
2
  Contributors: chrisscott, voceplatforms
3
  Tags: thumbnails, image, featured image
4
  Requires at least: 2.9.2
5
- Tested up to: 3.3.2
6
- Stable tag: 1.3
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
 
@@ -101,6 +101,10 @@ Pancakes.
101
 
102
  == Changelog ==
103
 
 
 
 
 
104
  = 1.3 =
105
 
106
  * Don't show set as links in media screens when not in context (props prettyboymp). Add voceplatforms as an author. Updated FAQ.
2
  Contributors: chrisscott, voceplatforms
3
  Tags: thumbnails, image, featured image
4
  Requires at least: 2.9.2
5
+ Tested up to: 3.5
6
+ Stable tag: 1.4
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
 
101
 
102
  == Changelog ==
103
 
104
+ = 1.4 =
105
+
106
+ * Add a context parameter to the thickbox opener to narrow down the selection in the media upload tabs to the one being set/viewed (props kevinlangleyjr) which reduces clutter when many thumbnails are registered. Refactor js to use an object (props markparolisi). Hide attachment fields on 3.5 media sidebar.
107
+
108
  = 1.3 =
109
 
110
  * Don't show set as links in media screens when not in context (props prettyboymp). Add voceplatforms as an author. Updated FAQ.
screenshot-1.png CHANGED
Binary file
screenshot-2.png CHANGED
Binary file
screenshot-3.png CHANGED
Binary file