Multiple Post Thumbnails - Version 1.5

Version Description

  • Add a size parameter to MultiPostThumbnails::get_post_thumbnail_url to allow getting any registered size.
  • Add context option to the args accepted when instantiating a new MultiPostThumbnails to specify the metabox context. Defaults to side (which it was previously hard coded to).
  • Filter is_protected_meta to hide meta from the Custom Fields metabox by default (props willroy). To unhide them, add add_filter('mpt_unprotect_meta', '__return_true'); to your theme's functions.php.
  • il8n courtesy Horttcore
Download this release

Release Info

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

Code changes from version 1.4 to 1.5

languages/multiple-post-thumbnails-de_DE.mo ADDED
Binary file
languages/multiple-post-thumbnails-de_DE.po ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ msgid ""
2
+ msgstr ""
3
+ "Project-Id-Version: Multiple Post Thumbnails v1.3\n"
4
+ "Report-Msgid-Bugs-To: \n"
5
+ "POT-Creation-Date: \n"
6
+ "PO-Revision-Date: 2012-11-20 17:43:38+0000\n"
7
+ "Last-Translator: horttcore <me@horttcore.de>\n"
8
+ "Language-Team: \n"
9
+ "MIME-Version: 1.0\n"
10
+ "Content-Type: text/plain; charset=UTF-8\n"
11
+ "Content-Transfer-Encoding: 8bit\n"
12
+ "Plural-Forms: nplurals=2; plural=n != 1;\n"
13
+ "X-Poedit-Language: German\n"
14
+ "X-Poedit-Country: GERMANY\n"
15
+ "X-Poedit-SourceCharset: utf-8\n"
16
+ "X-Poedit-KeywordsList: __;_e;__ngettext:1,2;_n:1,2;__ngettext_noop:1,2;_n_noop:1,2;_c,_nc:4c,1,2;_x:1,2c;_ex:1,2c;_nx:4c,1,2;_nx_noop:4c,1,2;\n"
17
+ "X-Poedit-Basepath: ../\n"
18
+ "X-Poedit-Bookmarks: \n"
19
+ "X-Poedit-SearchPath-0: .\n"
20
+ "X-Textdomain-Support: yes"
21
+
22
+ #: multi-post-thumbnails.php:72
23
+ #, php-format
24
+ #@ multiple-post-thumbnails
25
+ msgid "The 'label' and 'id' values of the 'args' parameter of '%s::%s()' are required"
26
+ msgstr "Die 'label' und 'id' Werte des 'args' Parameter von '%s::%s()' sind Pflicht"
27
+
28
+ #: multi-post-thumbnails.php:133
29
+ #, php-format
30
+ #@ multiple-post-thumbnails
31
+ msgid "Set as %3$s"
32
+ msgstr "Als %3$s setzen"
33
+
34
+ #: multi-post-thumbnails.php:293
35
+ #, php-format
36
+ #@ multiple-post-thumbnails
37
+ msgid "Set %s"
38
+ msgstr " %s festlegen"
39
+
languages/multiple-post-thumbnails-xx_XX.pot ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ msgid ""
2
+ msgstr ""
3
+ "Project-Id-Version: PlaceHolder\n"
4
+ "Report-Msgid-Bugs-To: \n"
5
+ "POT-Creation-Date: \n"
6
+ "PO-Revision-Date: 2012-11-20 16:30:56+0000\n"
7
+ "Last-Translator: none\n"
8
+ "Language-Team: \n"
9
+ "MIME-Version: 1.0\n"
10
+ "Content-Type: text/plain; charset=UTF-8\n"
11
+ "Content-Transfer-Encoding: 8bit\n"
12
+ "Plural-Forms: nplurals=2; plural=n != 1;\n"
13
+ "X-Poedit-Language: English\n"
14
+ "X-Poedit-Country: UNITED STATES\n"
15
+ "X-Poedit-SourceCharset: utf-8\n"
16
+ "X-Poedit-KeywordsList: __;_e;__ngettext:1,2;_n:1,2;__ngettext_noop:1,2;_n_noop:1,2;_c,_nc:4c,1,2;_x:1,2c;_ex:1,2c;_nx:4c,1,2;_nx_noop:4c,1,2;\n"
17
+ "X-Poedit-Basepath: ../\n"
18
+ "X-Poedit-Bookmarks: \n"
19
+ "X-Poedit-SearchPath-0: .\n"
20
+ "X-Textdomain-Support: yes"
21
+
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: 1.4
7
  Author: Chris Scott
8
  Author URI: http://voceplatforms.com/
9
  */
@@ -46,7 +46,9 @@ if (!class_exists('MultiPostThumbnails')) {
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
@@ -57,6 +59,7 @@ if (!class_exists('MultiPostThumbnails')) {
57
  'id' => null,
58
  'post_type' => 'post',
59
  'priority' => 'low',
 
60
  );
61
 
62
  $args = wp_parse_args($args, $defaults);
@@ -69,7 +72,7 @@ if (!class_exists('MultiPostThumbnails')) {
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
  }
@@ -86,6 +89,16 @@ if (!class_exists('MultiPostThumbnails')) {
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
  }
90
 
91
  /**
@@ -94,7 +107,7 @@ if (!class_exists('MultiPostThumbnails')) {
94
  * @return void
95
  */
96
  public function add_metabox() {
97
- add_meta_box("{$this->post_type}-{$this->id}", __($this->label), array($this, 'thumbnail_meta_box'), $this->post_type, 'side', $this->priority);
98
  }
99
 
100
  /**
@@ -104,7 +117,7 @@ if (!class_exists('MultiPostThumbnails')) {
104
  */
105
  public function thumbnail_meta_box() {
106
  global $post;
107
- $thumbnail_id = get_post_meta($post->ID, "{$this->post_type}_{$this->id}_thumbnail_id", true);
108
  echo $this->post_thumbnail_html($thumbnail_id);
109
  }
110
 
@@ -121,7 +134,7 @@ if (!class_exists('MultiPostThumbnails')) {
121
  $calling_post_id = absint($_GET['post_id']);
122
  elseif (isset($_POST) && count($_POST)) // Like for async-upload where $_GET['post_id'] isn't set
123
  $calling_post_id = $post->post_parent;
124
-
125
  if (!$calling_post_id)
126
  return $form_fields;
127
 
@@ -138,7 +151,7 @@ if (!class_exists('MultiPostThumbnails')) {
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',
@@ -173,8 +186,27 @@ if (!class_exists('MultiPostThumbnails')) {
173
  */
174
  public function action_delete_attachment($post_id) {
175
  global $wpdb;
176
- $meta_key = "{$this->post_type}_{$this->id}_thumbnail_id";
177
- $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->postmeta WHERE meta_key = '%s' AND meta_value = %d", $meta_key, $post_id ));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
178
  }
179
 
180
  private function plugins_url($relative_path, $plugin_path) {
@@ -280,16 +312,27 @@ if (!class_exists('MultiPostThumbnails')) {
280
  * @param string $post_type The post type.
281
  * @param string $id The id used to register the thumbnail.
282
  * @param int $post_id Optional. The post ID. If not set, will attempt to get it.
 
283
  * @return mixed Thumbnail url or false if the post doesn't have a thumbnail for the given post type, and id.
284
  */
285
- public static function get_post_thumbnail_url($post_type, $id, $post_id = 0) {
286
  if (!$post_id) {
287
  $post_id = get_the_ID();
288
  }
289
 
290
  $post_thumbnail_id = self::get_post_thumbnail_id($post_type, $id, $post_id);
291
 
292
- return wp_get_attachment_url($post_thumbnail_id);
 
 
 
 
 
 
 
 
 
 
293
  }
294
 
295
  /**
@@ -303,8 +346,9 @@ if (!class_exists('MultiPostThumbnails')) {
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
 
310
  if ($thumbnail_id && get_post($thumbnail_id)) {
@@ -317,7 +361,8 @@ if (!class_exists('MultiPostThumbnails')) {
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
  }
@@ -340,7 +385,7 @@ if (!class_exists('MultiPostThumbnails')) {
340
  check_ajax_referer("set_post_thumbnail-{$this->post_type}-{$this->id}-{$post_ID}");
341
 
342
  if ($thumbnail_id == '-1') {
343
- delete_post_meta($post_ID, "{$this->post_type}_{$this->id}_thumbnail_id");
344
  die($this->post_thumbnail_html(null));
345
  }
346
 
@@ -369,4 +414,7 @@ if (!class_exists('MultiPostThumbnails')) {
369
  }
370
 
371
  }
 
 
 
372
  }
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.5
7
  Author: Chris Scott
8
  Author URI: http://voceplatforms.com/
9
  */
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'.
50
+ *
51
+ * context - The admin metabox context. Defaults to 'side'.
52
  *
53
  * @param array|string $args See above description.
54
  * @return void
59
  'id' => null,
60
  'post_type' => 'post',
61
  'priority' => 'low',
62
+ 'context' => 'side',
63
  );
64
 
65
  $args = wp_parse_args($args, $defaults);
72
  // Need these args to be set at a minimum
73
  if (null === $this->label || null === $this->id) {
74
  if (WP_DEBUG) {
75
+ trigger_error(sprintf(__("The 'label' and 'id' values of the 'args' parameter of '%s::%s()' are required", 'multiple-post-thumbnails'), __CLASS__, __FUNCTION__));
76
  }
77
  return;
78
  }
89
  add_action('admin_print_styles-post.php', array($this, 'hide_media_sidebar_fields'));
90
  add_action("wp_ajax_set-{$this->post_type}-{$this->id}-thumbnail", array($this, 'set_thumbnail'));
91
  add_action('delete_attachment', array($this, 'action_delete_attachment'));
92
+ add_filter('is_protected_meta', array($this, 'filter_is_protected_meta'), 20, 2);
93
+ }
94
+
95
+ /**
96
+ * get the meta key used to store a post's thumbnail
97
+ *
98
+ * @return string
99
+ */
100
+ public function get_meta_key() {
101
+ return "{$this->post_type}_{$this->id}_thumbnail_id";
102
  }
103
 
104
  /**
107
  * @return void
108
  */
109
  public function add_metabox() {
110
+ add_meta_box("{$this->post_type}-{$this->id}", __($this->label, 'multiple-post-thumbnails'), array($this, 'thumbnail_meta_box'), $this->post_type, $this->context, $this->priority);
111
  }
112
 
113
  /**
117
  */
118
  public function thumbnail_meta_box() {
119
  global $post;
120
+ $thumbnail_id = get_post_meta($post->ID, $this->get_meta_key(), true);
121
  echo $this->post_thumbnail_html($thumbnail_id);
122
  }
123
 
134
  $calling_post_id = absint($_GET['post_id']);
135
  elseif (isset($_POST) && count($_POST)) // Like for async-upload where $_GET['post_id'] isn't set
136
  $calling_post_id = $post->post_parent;
137
+
138
  if (!$calling_post_id)
139
  return $form_fields;
140
 
151
  return $form_fields;
152
 
153
  $ajax_nonce = wp_create_nonce("set_post_thumbnail-{$this->post_type}-{$this->id}-{$calling_post_id}");
154
+ $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', 'multiple-post-thumbnails' ) . '</a>', $this->id, $post->ID, $this->label, $this->post_type, $ajax_nonce);
155
  $form_fields["{$this->post_type}-{$this->id}-thumbnail"] = array(
156
  'label' => $this->label,
157
  'input' => 'html',
186
  */
187
  public function action_delete_attachment($post_id) {
188
  global $wpdb;
189
+
190
+ $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->postmeta WHERE meta_key = '%s' AND meta_value = %d", $this->get_meta_key(), $post_id ));
191
+ }
192
+
193
+ /**
194
+ * make the meta for storing thumbnails protected so it doesn't show in the Custom Fields metabox
195
+ *
196
+ * @param boolean $protected Passed in from filter
197
+ * @param type $meta_key Passed in from filter
198
+ * @return boolean
199
+ */
200
+ public function filter_is_protected_meta($protected, $meta_key) {
201
+ if (apply_filters('mpt_unprotect_meta', false)) {
202
+ return $protected;
203
+ }
204
+
205
+ if ($meta_key == $this->get_meta_key()) {
206
+ $protected = true;
207
+ }
208
+
209
+ return $protected;
210
  }
211
 
212
  private function plugins_url($relative_path, $plugin_path) {
312
  * @param string $post_type The post type.
313
  * @param string $id The id used to register the thumbnail.
314
  * @param int $post_id Optional. The post ID. If not set, will attempt to get it.
315
+ * @param string $size Optional. The thumbnail size to use. If set, use wp_get_attachment_image_src() instead of wp_get_attachment_url()
316
  * @return mixed Thumbnail url or false if the post doesn't have a thumbnail for the given post type, and id.
317
  */
318
+ public static function get_post_thumbnail_url($post_type, $id, $post_id = 0, $size = null) {
319
  if (!$post_id) {
320
  $post_id = get_the_ID();
321
  }
322
 
323
  $post_thumbnail_id = self::get_post_thumbnail_id($post_type, $id, $post_id);
324
 
325
+ if ($size) {
326
+ if ($url = wp_get_attachment_image_src($post_thumbnail_id, $size)) {
327
+ $url = $url[0];
328
+ } else {
329
+ $url = '';
330
+ }
331
+ } else {
332
+ $url = wp_get_attachment_url($post_thumbnail_id);
333
+ }
334
+
335
+ return $url;
336
  }
337
 
338
  /**
346
  $image_library_url = get_upload_iframe_src('image');
347
  // if TB_iframe is not moved to end of query string, thickbox will remove all query args after it.
348
  $image_library_url = add_query_arg( array( 'context' => $this->id, 'TB_iframe' => 1 ), remove_query_arg( 'TB_iframe', $image_library_url ) );
349
+ $format_string = '<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>';
350
+ $set_thumbnail_link = sprintf( $format_string, sprintf( esc_attr__( "Set %s" , 'multiple-post-thumbnails' ), $this->label ), $image_library_url, $this->post_type, $this->id );
351
+ $content = sprintf( $set_thumbnail_link, sprintf( esc_html__( "Set %s", 'multiple-post-thumbnails' ), $this->label ) );
352
 
353
 
354
  if ($thumbnail_id && get_post($thumbnail_id)) {
361
  if (!empty($thumbnail_html)) {
362
  $ajax_nonce = wp_create_nonce("set_post_thumbnail-{$this->post_type}-{$this->id}-{$post_ID}");
363
  $content = sprintf($set_thumbnail_link, $thumbnail_html);
364
+ $format_string = '<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>';
365
+ $content .= sprintf( $format_string, $this->post_type, $this->id, sprintf( esc_html__( "Remove %s", 'multiple-post-thumbnails' ), $this->label ), $ajax_nonce );
366
  }
367
  $content_width = $old_content_width;
368
  }
385
  check_ajax_referer("set_post_thumbnail-{$this->post_type}-{$this->id}-{$post_ID}");
386
 
387
  if ($thumbnail_id == '-1') {
388
+ delete_post_meta($post_ID, $this->get_meta_key());
389
  die($this->post_thumbnail_html(null));
390
  }
391
 
414
  }
415
 
416
  }
417
+
418
+ if ( is_admin() )
419
+ load_plugin_textdomain( 'multiple-post-thumbnails', FALSE, 'multi-post-thumbnails/languages/' );
420
  }
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.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
 
@@ -85,6 +85,10 @@ For example, for a thumbnail registered with an `id` of `secondary-image` and `p
85
 
86
  If you are using a symlink to include the plugin directory in your project, the admin js file will not load and cause this. Unfortunately, the solution is to not use symlinks due to the behavior of PHP's `__FILE__`
87
 
 
 
 
 
88
  = Is there a github repo? I love me some submodules! =
89
 
90
  Yes. https://github.com/voceconnect/multi-post-thumbnails
@@ -101,6 +105,13 @@ Pancakes.
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.
2
  Contributors: chrisscott, voceplatforms
3
  Tags: thumbnails, image, featured image
4
  Requires at least: 2.9.2
5
+ Tested up to: 3.5.1
6
+ Stable tag: 1.5
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
 
85
 
86
  If you are using a symlink to include the plugin directory in your project, the admin js file will not load and cause this. Unfortunately, the solution is to not use symlinks due to the behavior of PHP's `__FILE__`
87
 
88
+ = Is there a way to show the post meta where the thumbnail IDs are stored in the Custom Fields metabox?
89
+
90
+ Since version 1.5 these are hidden by default. To unhide them, add `add_filter('mpt_unprotect_meta', '__return_true');` to your theme's `functions.php`
91
+
92
  = Is there a github repo? I love me some submodules! =
93
 
94
  Yes. https://github.com/voceconnect/multi-post-thumbnails
105
 
106
  == Changelog ==
107
 
108
+ = 1.5 =
109
+
110
+ * Add a `size` parameter to `MultiPostThumbnails::get_post_thumbnail_url` to allow getting any registered size.
111
+ * Add `context` option to the args accepted when instantiating a new `MultiPostThumbnails` to specify the metabox context. Defaults to `side` (which it was previously hard coded to).
112
+ * Filter `is_protected_meta` to hide meta from the Custom Fields metabox by default (props willroy). To unhide them, add `add_filter('mpt_unprotect_meta', '__return_true');` to your theme's `functions.php`.
113
+ * il8n courtesy Horttcore
114
+
115
  = 1.4 =
116
 
117
  * 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.