Get the Image - Version 0.6.1

Version Description

Download this release

Release Info

Developer greenshady
Plugin Icon 128x128 Get the Image
Version 0.6.1
Comparing to
See all releases

Code changes from version 0.6 to 0.6.1

Files changed (3) hide show
  1. get-the-image.php +75 -61
  2. readme.html +7 -3
  3. readme.txt +8 -2
get-the-image.php CHANGED
@@ -3,26 +3,31 @@
3
  * Plugin Name: Get The Image
4
  * Plugin URI: http://justintadlock.com/archives/2008/05/27/get-the-image-wordpress-plugin
5
  * Description: This is a highly intuitive script that can grab an image by custom field input, post attachment, or extracting it from the post's content.
6
- * Version: 0.6
7
  * Author: Justin Tadlock
8
  * Author URI: http://justintadlock.com
9
  *
10
- * Get the Image was created to solve a problem in the WordPress community about how to handle
11
- * post-specific images. It was created to be a highly-intuitive image script that loads images that are
12
- * related to specific posts in some way. It creates an image-based representation of a WordPress
13
- * post (or any post type).
14
  *
15
- * @copyright 2008 - 2010
16
- * @version 0.6
17
- * @author Justin Tadlock
18
- * @link http://justintadlock.com/archives/2008/05/27/get-the-image-wordpress-plugin
19
- * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 
 
 
 
20
  *
21
- * This program is distributed in the hope that it will be useful,
22
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
23
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
24
  *
25
  * @package GetTheImage
 
 
 
 
 
26
  */
27
 
28
  /* Adds theme support for post images. */
@@ -35,15 +40,16 @@ add_action( 'updated_post_meta', 'get_the_image_delete_cache' );
35
  add_action( 'added_post_meta', 'get_the_image_delete_cache' );
36
 
37
  /**
38
- * This is a highly intuitive function that gets images. It first calls for custom field keys. If no
39
- * custom field key is set, check for the_post_thumbnail(). If no post image, check for images
40
- * attached to post. Check for image order if looking for attached images. Scan the post for
41
- * images if $image_scan = true. Check for default image if $default_image = true. If an image
42
- * is found, call display_the_image() to format it.
 
43
  *
44
- * @since 0.1
45
  * @global $post The current post's DB object.
46
- * @param array $args Parameters for what image to get.
47
  * @return string|array The HTML for the image. | Image attributes in an array.
48
  */
49
  function get_the_image( $args = array() ) {
@@ -67,7 +73,9 @@ function get_the_image( $args = array() ) {
67
  'meta_key_save' => false,
68
  'callback' => null,
69
  'cache' => true,
70
- 'echo' => true
 
 
71
  );
72
 
73
  /* Allow plugins/themes to filter the arguments. */
@@ -77,11 +85,11 @@ function get_the_image( $args = array() ) {
77
  $args = wp_parse_args( $args, $defaults );
78
 
79
  /* If $default_size is given, overwrite $size. */
80
- if ( isset( $args['default_size'] ) )
81
  $args['size'] = $args['default_size']; // Deprecated 0.5 in favor of $size
82
 
83
  /* If $custom_key is set, overwrite $meta_key. */
84
- if ( isset( $args['custom_key'] ) )
85
  $args['meta_key'] = $args['custom_key']; // Deprecated 0.6 in favor of $meta_key
86
 
87
  /* If $format is set to 'array', don't link to the post. */
@@ -161,42 +169,50 @@ function get_the_image( $args = array() ) {
161
  /* Internal Functions */
162
 
163
  /**
164
- * Calls images by custom field key. Script loops through multiple custom field keys.
165
- * If that particular key is found, $image is set and the loop breaks. If an image is
166
- * found, it is returned.
167
  *
168
- * @since 0.3
169
  * @param array $args
170
  * @return array|bool
171
  */
172
  function image_by_custom_field( $args = array() ) {
173
 
174
- /* If $meta_key is a string, we want to split it by spaces into an array. */
175
- if ( !is_array( $args['meta_key'] ) )
176
- $args['meta_key'] = preg_split( '#\s+#', $args['meta_key'] );
 
 
 
 
 
 
 
 
 
 
 
 
177
 
178
- /* If $meta_key is set, loop through each custom field key, searching for values. */
179
- if ( is_array( $args['meta_key'] ) ) {
180
- foreach ( $args['meta_key'] as $custom ) {
181
- $image = get_metadata( 'post', $args['post_id'], $custom, true );
182
- if ( $image )
183
  break;
184
  }
185
  }
186
 
187
  /* If a custom key value has been given for one of the keys, return the image URL. */
188
- if ( $image )
189
  return array( 'url' => $image );
190
 
191
  return false;
192
  }
193
 
194
  /**
195
- * Checks for images using a custom version of the WordPress 2.9+ get_the_post_thumbnail()
196
- * function. If an image is found, return it and the $post_thumbnail_id. The WordPress function's
197
- * other filters are later added in the display_the_image() function.
198
  *
199
- * @since 0.4
200
  * @param array $args
201
  * @return array|bool
202
  */
@@ -223,11 +239,10 @@ function image_by_the_post_thumbnail( $args = array() ) {
223
  }
224
 
225
  /**
226
- * Check for attachment images. Uses get_children() to check if the post has images
227
- * attached. If image attachments are found, loop through each. The loop only breaks
228
- * once $order_of_image is reached.
229
  *
230
- * @since 0.3
231
  * @param array $args
232
  * @return array|bool
233
  */
@@ -265,10 +280,10 @@ function image_by_attachment( $args = array() ) {
265
  }
266
 
267
  /**
268
- * Scans the post for images within the content. Not called by default with get_the_image().
269
- * Shouldn't use if using large images within posts, better to use the other options.
270
  *
271
- * @since 0.3
272
  * @global $post The current post's DB object.
273
  * @param array $args
274
  * @return array|bool
@@ -286,10 +301,10 @@ function image_by_scan( $args = array() ) {
286
  }
287
 
288
  /**
289
- * Used for setting a default image. The function simply returns the image URL it was
290
- * given in an array. Not used with get_the_image() by default.
291
  *
292
- * @since 0.3
293
  * @param array $args
294
  * @return array
295
  */
@@ -298,10 +313,10 @@ function image_by_default( $args = array() ) {
298
  }
299
 
300
  /**
301
- * Formats an image with appropriate alt text and class. Adds a link to the post if argument
302
- * is set. Should only be called if there is an image to display, but will handle it if not.
303
  *
304
- * @since 0.1
305
  * @param array $args
306
  * @param array $image Array of image info ($image, $classes, $alt, $caption).
307
  * @return string $image Formatted image (w/link to post if the option is set).
@@ -358,12 +373,11 @@ function display_the_image( $args = array(), $image = false ) {
358
  }
359
 
360
  /**
361
- * Saves the image URL as the value of the meta key provided. This allows users to set a
362
- * custom meta key for their image. By doing this, users can trim off database queries when
363
- * grabbing attachments or get rid of expensive scans of the content when using the image
364
- * scan feature.
365
  *
366
- * @since 0.6
367
  * @param array $args Parameters for what image to get.
368
  * @param array $image Array of image info ($image, $classes, $alt, $caption).
369
  */
@@ -388,7 +402,7 @@ function get_the_image_meta_key_save( $args = array(), $image = array() ) {
388
  /**
389
  * Deletes the image cache for users that are using a persistent-caching plugin.
390
  *
391
- * @since 0.5
392
  */
393
  function get_the_image_delete_cache() {
394
  wp_cache_delete( 'get_the_image' );
@@ -397,8 +411,8 @@ function get_the_image_delete_cache() {
397
  /**
398
  * Get the image with a link to the post. Use get_the_image() instead.
399
  *
400
- * @since 0.1
401
- * @deprecated 0.3
402
  */
403
  function get_the_image_link( $deprecated = '', $deprecated_2 = '', $deprecated_3 = '' ) {
404
  get_the_image();
3
  * Plugin Name: Get The Image
4
  * Plugin URI: http://justintadlock.com/archives/2008/05/27/get-the-image-wordpress-plugin
5
  * Description: This is a highly intuitive script that can grab an image by custom field input, post attachment, or extracting it from the post's content.
6
+ * Version: 0.6.1
7
  * Author: Justin Tadlock
8
  * Author URI: http://justintadlock.com
9
  *
10
+ * Get the Image - An advanced post image script for WordPress.
 
 
 
11
  *
12
+ * Get the Image was created to be a highly-intuitive image script that displays post-specific images (an
13
+ * image-based representation of a post). The script handles old-style post images via custom fields for
14
+ * backwards compatibility. It also supports WordPress' built-in featured image functionality. On top of
15
+ * those things, it can automatically set attachment images as the post image or scan the post content for
16
+ * the first image element used. It can also fall back to a given default image.
17
+ *
18
+ * This program is free software; you can redistribute it and/or modify it under the terms of the GNU
19
+ * General Public License version 2, as published by the Free Software Foundation. You may NOT assume
20
+ * that you can use any other version of the GPL.
21
  *
22
+ * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
23
+ * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 
24
  *
25
  * @package GetTheImage
26
+ * @version 0.6.1
27
+ * @author Justin Tadlock <justin@justintadlock.com>
28
+ * @copyright Copyright (c) 2008 - 2010, Justin Tadlock
29
+ * @link http://justintadlock.com/archives/2008/05/27/get-the-image-wordpress-plugin
30
+ * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
31
  */
32
 
33
  /* Adds theme support for post images. */
40
  add_action( 'added_post_meta', 'get_the_image_delete_cache' );
41
 
42
  /**
43
+ * The main image function for displaying an image. It supports several arguments that allow developers to
44
+ * customize how the script outputs the image.
45
+ *
46
+ * The image check order is important to note here. If an image is found by any specific check, the script
47
+ * will no longer look for images. The check order is 'meta_key', 'the_post_thumbnail', 'attachment',
48
+ * 'image_scan', 'callback', and 'default_image'.
49
  *
50
+ * @since 0.1.0
51
  * @global $post The current post's DB object.
52
+ * @param array $args Arguments for how to load and display the image.
53
  * @return string|array The HTML for the image. | Image attributes in an array.
54
  */
55
  function get_the_image( $args = array() ) {
73
  'meta_key_save' => false,
74
  'callback' => null,
75
  'cache' => true,
76
+ 'echo' => true,
77
+ 'custom_key' => null, // @deprecated 0.6. Use 'meta_key'.
78
+ 'default_size' => null, // @deprecated 0.5. Use 'size'.
79
  );
80
 
81
  /* Allow plugins/themes to filter the arguments. */
85
  $args = wp_parse_args( $args, $defaults );
86
 
87
  /* If $default_size is given, overwrite $size. */
88
+ if ( !is_null( $args['default_size'] ) )
89
  $args['size'] = $args['default_size']; // Deprecated 0.5 in favor of $size
90
 
91
  /* If $custom_key is set, overwrite $meta_key. */
92
+ if ( !is_null( $args['custom_key'] ) )
93
  $args['meta_key'] = $args['custom_key']; // Deprecated 0.6 in favor of $meta_key
94
 
95
  /* If $format is set to 'array', don't link to the post. */
169
  /* Internal Functions */
170
 
171
  /**
172
+ * Calls images by custom field key. Script loops through multiple custom field keys. If that particular key
173
+ * is found, $image is set and the loop breaks. If an image is found, it is returned.
 
174
  *
175
+ * @since 0.3.0
176
  * @param array $args
177
  * @return array|bool
178
  */
179
  function image_by_custom_field( $args = array() ) {
180
 
181
+ /* If $meta_key is not an array. */
182
+ if ( !is_array( $args['meta_key'] ) ) {
183
+
184
+ /* Get the image URL by the single meta key. */
185
+ $image = get_post_meta( $args['post_id'], $args['meta_key'], true );
186
+ }
187
+
188
+ /* If $meta_key is an array. */
189
+ elseif ( is_array( $args['meta_key'] ) ) {
190
+
191
+ /* Loop through each of the given meta keys. */
192
+ foreach ( $args['meta_key'] as $meta_key ) {
193
+
194
+ /* Get the image URL by the current meta key in the loop. */
195
+ $image = get_post_meta( $args['post_id'], $meta_key, true );
196
 
197
+ /* If an image was found, break out of the loop. */
198
+ if ( !empty( $image ) )
 
 
 
199
  break;
200
  }
201
  }
202
 
203
  /* If a custom key value has been given for one of the keys, return the image URL. */
204
+ if ( !empty( $image ) )
205
  return array( 'url' => $image );
206
 
207
  return false;
208
  }
209
 
210
  /**
211
+ * Checks for images using a custom version of the WordPress 2.9+ get_the_post_thumbnail() function.
212
+ * If an image is found, return it and the $post_thumbnail_id. The WordPress function's other filters are
213
+ * later added in the display_the_image() function.
214
  *
215
+ * @since 0.4.0
216
  * @param array $args
217
  * @return array|bool
218
  */
239
  }
240
 
241
  /**
242
+ * Check for attachment images. Uses get_children() to check if the post has images attached. If image
243
+ * attachments are found, loop through each. The loop only breaks once $order_of_image is reached.
 
244
  *
245
+ * @since 0.3.0
246
  * @param array $args
247
  * @return array|bool
248
  */
280
  }
281
 
282
  /**
283
+ * Scans the post for images within the content. Not called by default with get_the_image(). Shouldn't use
284
+ * if using large images within posts, better to use the other options.
285
  *
286
+ * @since 0.3.0
287
  * @global $post The current post's DB object.
288
  * @param array $args
289
  * @return array|bool
301
  }
302
 
303
  /**
304
+ * Used for setting a default image. The function simply returns the image URL it was given in an array.
305
+ * Not used with get_the_image() by default.
306
  *
307
+ * @since 0.3.0
308
  * @param array $args
309
  * @return array
310
  */
313
  }
314
 
315
  /**
316
+ * Formats an image with appropriate alt text and class. Adds a link to the post if argument is set. Should
317
+ * only be called if there is an image to display, but will handle it if not.
318
  *
319
+ * @since 0.1.0
320
  * @param array $args
321
  * @param array $image Array of image info ($image, $classes, $alt, $caption).
322
  * @return string $image Formatted image (w/link to post if the option is set).
373
  }
374
 
375
  /**
376
+ * Saves the image URL as the value of the meta key provided. This allows users to set a custom meta key
377
+ * for their image. By doing this, users can trim off database queries when grabbing attachments or get rid
378
+ * of expensive scans of the content when using the image scan feature.
 
379
  *
380
+ * @since 0.6.0
381
  * @param array $args Parameters for what image to get.
382
  * @param array $image Array of image info ($image, $classes, $alt, $caption).
383
  */
402
  /**
403
  * Deletes the image cache for users that are using a persistent-caching plugin.
404
  *
405
+ * @since 0.5.0
406
  */
407
  function get_the_image_delete_cache() {
408
  wp_cache_delete( 'get_the_image' );
411
  /**
412
  * Get the image with a link to the post. Use get_the_image() instead.
413
  *
414
+ * @since 0.1.0
415
+ * @deprecated 0.3.0
416
  */
417
  function get_the_image_link( $deprecated = '', $deprecated_2 = '', $deprecated_3 = '' ) {
418
  get_the_image();
readme.html CHANGED
@@ -71,7 +71,7 @@
71
 
72
  <dl>
73
  <dt>meta_key</dt>
74
- <dd>This parameter refers to post meta keys (custom fields) that you use. Remember, meta keys are case-sensitive (defaults are <code>Thumbnail</code> and <code>thumbnail</code>).</dd>
75
  <dt>post_id</dt>
76
  <dd>The ID of the post to get the image for. This defaults to the current post in the loop.</dd>
77
  <dt>attachment</dt>
@@ -116,7 +116,7 @@
116
 
117
  <p><strong>Example 2:</strong> Let's suppose you want a full-sized image and maybe you want to grab it by a custom field key of <code>Feature</code>. Depending on your theme, this will need to go within the Loop in whatever file is calling the featured article.</p>
118
 
119
- <pre><code>&lt;?php get_the_image( array( 'meta_key' => array( 'Feature' ), 'size' => 'full' ) ); ?></code></pre>
120
 
121
  <p>If no feature image exists by custom field, it will look for images attached to your post.</p>
122
 
@@ -132,6 +132,10 @@
132
 
133
  <pre><code>&lt;?php get_the_image( array( 'order_of_image' => 2 ) ); ?></code></pre>
134
 
 
 
 
 
135
  <h2>A real-world example</h2>
136
 
137
  <p>This is an example Loop, which may differ slightly from your theme, but the concept is the same. The call to get the image can go anywhere between the opening and closing lines.</p>
@@ -140,7 +144,7 @@
140
 
141
  &lt;div id="post-&lt;?php the_ID(); ?>" &lt;?php post_class(); ?>>
142
 
143
- &lt;?php get_the_image( array( 'meta_key' => array( 'feature_img' ), 'size' => 'medium', 'width' => '200', 'height' => '200', 'image_class' => 'feature' ) ); ?>
144
 
145
  &lt;h2>&lt;a href="&lt;?php the_permalink(); ?>" title="&lt;?php the_title_attribute(); ?>" rel="bookmark">&lt;?php the_title(); ?>&lt;/a>&lt;/h2>
146
 
71
 
72
  <dl>
73
  <dt>meta_key</dt>
74
+ <dd>This parameter refers to post meta keys (custom fields) that you use. Remember, meta keys are case-sensitive (defaults are <code>Thumbnail</code> and <code>thumbnail</code>). By default, this is an array of meta keys, but it can also be a string for a single meta key.</dd>
75
  <dt>post_id</dt>
76
  <dd>The ID of the post to get the image for. This defaults to the current post in the loop.</dd>
77
  <dt>attachment</dt>
116
 
117
  <p><strong>Example 2:</strong> Let's suppose you want a full-sized image and maybe you want to grab it by a custom field key of <code>Feature</code>. Depending on your theme, this will need to go within the Loop in whatever file is calling the featured article.</p>
118
 
119
+ <pre><code>&lt;?php get_the_image( array( 'meta_key' => 'Feature', 'size' => 'full' ) ); ?></code></pre>
120
 
121
  <p>If no feature image exists by custom field, it will look for images attached to your post.</p>
122
 
132
 
133
  <pre><code>&lt;?php get_the_image( array( 'order_of_image' => 2 ) ); ?></code></pre>
134
 
135
+ <p><strong>Example 6:</strong> Saving an image to the <code>Thumbnail</code> custom field automatically.</p>
136
+
137
+ <pre><code>&lt;?php get_the_image( array( 'meta_key_save' => 'Thumbnail' ) ); ?></code></pre>
138
+
139
  <h2>A real-world example</h2>
140
 
141
  <p>This is an example Loop, which may differ slightly from your theme, but the concept is the same. The call to get the image can go anywhere between the opening and closing lines.</p>
144
 
145
  &lt;div id="post-&lt;?php the_ID(); ?>" &lt;?php post_class(); ?>>
146
 
147
+ &lt;?php get_the_image( array( 'meta_key' => 'feature_img', 'size' => 'medium', 'width' => '200', 'height' => '200', 'image_class' => 'feature' ) ); ?>
148
 
149
  &lt;h2>&lt;a href="&lt;?php the_permalink(); ?>" title="&lt;?php the_title_attribute(); ?>" rel="bookmark">&lt;?php the_title(); ?>&lt;/a>&lt;/h2>
150
 
readme.txt CHANGED
@@ -3,8 +3,8 @@ Contributors: greenshady
3
  Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=3687060
4
  Tags: image, images, thumbnail
5
  Requires at least: 2.9
6
- Tested up to: 3.0
7
- Stable tag: 0.6
8
 
9
  An easy-to-use image script for adding things such as thumbnails and feature images.
10
 
@@ -56,6 +56,12 @@ You can view this plugin in action on my <a href="http://justintadlock.com" titl
56
 
57
  == Changelog ==
58
 
 
 
 
 
 
 
59
  **Version 0.6**
60
 
61
  * Deprecated `custom_key` in favor of `meta_key`.
3
  Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=3687060
4
  Tags: image, images, thumbnail
5
  Requires at least: 2.9
6
+ Tested up to: 3.0.1
7
+ Stable tag: 0.6.1
8
 
9
  An easy-to-use image script for adding things such as thumbnails and feature images.
10
 
56
 
57
  == Changelog ==
58
 
59
+ **Version 0.6.1**
60
+
61
+ * Updated inline documentation of the code.
62
+ * Smarter `meta_key` logic, which allows a single meta key or an array of keys to be used.
63
+ * Set `custom_key` and `default_size` to `null` by default since they're deprecated.
64
+
65
  **Version 0.6**
66
 
67
  * Deprecated `custom_key` in favor of `meta_key`.