Version Description
Download this release
Release Info
Developer | greenshady |
Plugin | Get the Image |
Version | 0.8 |
Comparing to | |
See all releases |
Code changes from version 0.7 to 0.8
- get-the-image.php +125 -79
- readme.html +18 -7
- readme.txt +15 -2
get-the-image.php
CHANGED
@@ -1,9 +1,9 @@
|
|
1 |
<?php
|
2 |
/**
|
3 |
* Plugin Name: Get The Image
|
4 |
-
* Plugin URI: http://
|
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.
|
7 |
* Author: Justin Tadlock
|
8 |
* Author URI: http://justintadlock.com
|
9 |
*
|
@@ -23,9 +23,9 @@
|
|
23 |
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
24 |
*
|
25 |
* @package GetTheImage
|
26 |
-
* @version 0.
|
27 |
* @author Justin Tadlock <justin@justintadlock.com>
|
28 |
-
* @copyright Copyright (c) 2008 -
|
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 |
*/
|
@@ -48,34 +48,37 @@ add_action( 'added_post_meta', 'get_the_image_delete_cache_by_meta', 10, 2 );
|
|
48 |
* 'image_scan', 'callback', and 'default_image'.
|
49 |
*
|
50 |
* @since 0.1.0
|
|
|
51 |
* @global $post The current post's database 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() ) {
|
56 |
-
global $post;
|
57 |
|
58 |
/* Set the default arguments. */
|
59 |
$defaults = array(
|
60 |
-
'meta_key' =>
|
61 |
-
'post_id' =>
|
62 |
-
'attachment' =>
|
63 |
-
'the_post_thumbnail' =>
|
64 |
-
'size' =>
|
65 |
-
'default_image' =>
|
66 |
-
'order_of_image' =>
|
67 |
-
'link_to_post' =>
|
68 |
-
'image_class' =>
|
69 |
-
'image_scan' =>
|
70 |
-
'width' =>
|
71 |
-
'height' =>
|
72 |
-
'format' =>
|
73 |
-
'meta_key_save' =>
|
74 |
-
'
|
75 |
-
'
|
76 |
-
'
|
77 |
-
'
|
78 |
-
'
|
|
|
|
|
|
|
79 |
);
|
80 |
|
81 |
/* Allow plugins/themes to filter the arguments. */
|
@@ -108,6 +111,9 @@ function get_the_image( $args = array() ) {
|
|
108 |
if ( !is_array( $image_cache ) )
|
109 |
$image_cache = array();
|
110 |
|
|
|
|
|
|
|
111 |
/* If there is no cached image, let's see if one exists. */
|
112 |
if ( !isset( $image_cache[$key] ) || empty( $cache ) ) {
|
113 |
|
@@ -143,21 +149,21 @@ function get_the_image( $args = array() ) {
|
|
143 |
get_the_image_meta_key_save( $args, $image['src'] );
|
144 |
|
145 |
/* Format the image HTML. */
|
146 |
-
$
|
147 |
|
148 |
/* Set the image cache for the specific post. */
|
149 |
-
$image_cache[$key] = $
|
150 |
wp_cache_set( $post_id, $image_cache, 'get_the_image' );
|
151 |
}
|
152 |
}
|
153 |
|
154 |
/* If an image was already cached for the post and arguments, use it. */
|
155 |
else {
|
156 |
-
$
|
157 |
}
|
158 |
|
159 |
/* Allow plugins/theme to override the final output. */
|
160 |
-
$
|
161 |
|
162 |
/* If $format is set to 'array', return an array of image attributes. */
|
163 |
if ( 'array' == $format ) {
|
@@ -166,7 +172,7 @@ function get_the_image( $args = array() ) {
|
|
166 |
$out = array();
|
167 |
|
168 |
/* Get the image attributes. */
|
169 |
-
$atts = wp_kses_hair( $
|
170 |
|
171 |
/* Loop through the image attributes and add them in key/value pairs for the return array. */
|
172 |
foreach ( $atts as $att )
|
@@ -180,11 +186,19 @@ function get_the_image( $args = array() ) {
|
|
180 |
|
181 |
/* Or, if $echo is set to false, return the formatted image. */
|
182 |
elseif ( false === $echo ) {
|
183 |
-
return $
|
184 |
}
|
185 |
|
|
|
|
|
|
|
|
|
186 |
/* Display the image if we get to this point. */
|
187 |
-
echo $
|
|
|
|
|
|
|
|
|
188 |
}
|
189 |
|
190 |
/* Internal Functions */
|
@@ -194,31 +208,25 @@ function get_the_image( $args = array() ) {
|
|
194 |
* is found, $image is set and the loop breaks. If an image is found, it is returned.
|
195 |
*
|
196 |
* @since 0.7.0
|
|
|
197 |
* @param array $args Arguments for how to load and display the image.
|
198 |
* @return array|bool Array of image attributes. | False if no image is found.
|
199 |
*/
|
200 |
function get_the_image_by_meta_key( $args = array() ) {
|
201 |
|
202 |
/* If $meta_key is not an array. */
|
203 |
-
if ( !is_array( $args['meta_key'] ) )
|
204 |
-
|
205 |
-
/* Get the image URL by the single meta key. */
|
206 |
-
$image = get_post_meta( $args['post_id'], $args['meta_key'], true );
|
207 |
-
}
|
208 |
|
209 |
-
/*
|
210 |
-
|
211 |
|
212 |
-
/*
|
213 |
-
|
214 |
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
/* If an image was found, break out of the loop. */
|
219 |
-
if ( !empty( $image ) )
|
220 |
-
break;
|
221 |
-
}
|
222 |
}
|
223 |
|
224 |
/* If a custom key value has been given for one of the keys, return the image URL. */
|
@@ -234,6 +242,7 @@ function get_the_image_by_meta_key( $args = array() ) {
|
|
234 |
* later added in the display_the_image() function.
|
235 |
*
|
236 |
* @since 0.7.0
|
|
|
237 |
* @param array $args Arguments for how to load and display the image.
|
238 |
* @return array|bool Array of image attributes. | False if no image is found.
|
239 |
*/
|
@@ -264,40 +273,74 @@ function get_the_image_by_post_thumbnail( $args = array() ) {
|
|
264 |
* attachments are found, loop through each. The loop only breaks once $order_of_image is reached.
|
265 |
*
|
266 |
* @since 0.7.0
|
|
|
267 |
* @param array $args Arguments for how to load and display the image.
|
268 |
* @return array|bool Array of image attributes. | False if no image is found.
|
269 |
*/
|
270 |
function get_the_image_by_attachment( $args = array() ) {
|
271 |
|
272 |
-
/* Get
|
273 |
-
$
|
274 |
|
275 |
-
/*
|
276 |
-
if (
|
277 |
-
|
278 |
-
|
279 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
280 |
}
|
281 |
}
|
282 |
|
283 |
-
/*
|
284 |
-
if ( empty( $
|
285 |
-
return false;
|
286 |
|
287 |
-
|
288 |
-
|
289 |
|
290 |
-
|
291 |
-
|
292 |
-
|
293 |
-
|
294 |
-
|
295 |
-
|
296 |
-
|
|
|
|
|
297 |
}
|
298 |
|
299 |
-
/* Return
|
300 |
-
return
|
301 |
}
|
302 |
|
303 |
/**
|
@@ -305,6 +348,7 @@ function get_the_image_by_attachment( $args = array() ) {
|
|
305 |
* if using large images within posts, better to use the other options.
|
306 |
*
|
307 |
* @since 0.7.0
|
|
|
308 |
* @param array $args Arguments for how to load and display the image.
|
309 |
* @return array|bool Array of image attributes. | False if no image is found.
|
310 |
*/
|
@@ -325,6 +369,7 @@ function get_the_image_by_scan( $args = array() ) {
|
|
325 |
* Not used with get_the_image() by default.
|
326 |
*
|
327 |
* @since 0.7.0
|
|
|
328 |
* @param array $args Arguments for how to load and display the image.
|
329 |
* @return array|bool Array of image attributes. | False if no image is found.
|
330 |
*/
|
@@ -337,6 +382,7 @@ function get_the_image_by_default( $args = array() ) {
|
|
337 |
* only be called if there is an image to display, but will handle it if not.
|
338 |
*
|
339 |
* @since 0.7.0
|
|
|
340 |
* @param array $args Arguments for how to load and display the image.
|
341 |
* @param array $image Array of image attributes ($image, $classes, $alt, $caption).
|
342 |
* @return string $image Formatted image (w/link to post if the option is set).
|
@@ -360,20 +406,16 @@ function get_the_image_format( $args = array(), $image = false ) {
|
|
360 |
/* Loop through the custom field keys and add them as classes. */
|
361 |
if ( is_array( $meta_key ) ) {
|
362 |
foreach ( $meta_key as $key )
|
363 |
-
$classes[] =
|
364 |
}
|
365 |
|
366 |
/* Add the $size and any user-added $image_class to the class. */
|
367 |
-
$classes[] = $size;
|
368 |
-
$classes[] = $image_class;
|
369 |
|
370 |
/* Join all the classes into a single string and make sure there are no duplicates. */
|
371 |
$class = join( ' ', array_unique( $classes ) );
|
372 |
|
373 |
-
/* If there is a $post_thumbnail_id, apply the WP filters normally associated with get_the_post_thumbnail(). */
|
374 |
-
if ( !empty( $image['post_thumbnail_id'] ) )
|
375 |
-
do_action( 'begin_fetch_post_thumbnail_html', $post_id, $image['post_thumbnail_id'], $size );
|
376 |
-
|
377 |
/* Add the image attributes to the <img /> element. */
|
378 |
$html = '<img src="' . $image['src'] . '" alt="' . esc_attr( strip_tags( $image_alt ) ) . '" class="' . esc_attr( $class ) . '"' . $width . $height . ' />';
|
379 |
|
@@ -381,10 +423,6 @@ function get_the_image_format( $args = array(), $image = false ) {
|
|
381 |
if ( $link_to_post )
|
382 |
$html = '<a href="' . get_permalink( $post_id ) . '" title="' . esc_attr( apply_filters( 'the_title', get_post_field( 'post_title', $post_id ) ) ) . '">' . $html . '</a>';
|
383 |
|
384 |
-
/* If there is a $post_thumbnail_id, apply the WP filters normally associated with get_the_post_thumbnail(). */
|
385 |
-
if ( !empty( $image['post_thumbnail_id'] ) )
|
386 |
-
do_action( 'end_fetch_post_thumbnail_html', $post_id, $image['post_thumbnail_id'], $size );
|
387 |
-
|
388 |
/* If there is a $post_thumbnail_id, apply the WP filters normally associated with get_the_post_thumbnail(). */
|
389 |
if ( !empty( $image['post_thumbnail_id'] ) )
|
390 |
$html = apply_filters( 'post_thumbnail_html', $html, $post_id, $image['post_thumbnail_id'], $size, '' );
|
@@ -398,6 +436,7 @@ function get_the_image_format( $args = array(), $image = false ) {
|
|
398 |
* of expensive scans of the content when using the image scan feature.
|
399 |
*
|
400 |
* @since 0.6.0
|
|
|
401 |
* @param array $args Arguments for how to load and display the image.
|
402 |
* @param array $image Array of image attributes ($image, $classes, $alt, $caption).
|
403 |
*/
|
@@ -423,6 +462,9 @@ function get_the_image_meta_key_save( $args = array(), $image = array() ) {
|
|
423 |
* Deletes the image cache for the specific post when the 'save_post' hook is fired.
|
424 |
*
|
425 |
* @since 0.7.0
|
|
|
|
|
|
|
426 |
*/
|
427 |
function get_the_image_delete_cache_by_post( $post_id ) {
|
428 |
wp_cache_delete( $post_id, 'get_the_image' );
|
@@ -433,6 +475,10 @@ function get_the_image_delete_cache_by_post( $post_id ) {
|
|
433 |
* or 'updated_post_meta' hooks are called.
|
434 |
*
|
435 |
* @since 0.7.0
|
|
|
|
|
|
|
|
|
436 |
*/
|
437 |
function get_the_image_delete_cache_by_meta( $meta_id, $post_id ) {
|
438 |
wp_cache_delete( $post_id, 'get_the_image' );
|
1 |
<?php
|
2 |
/**
|
3 |
* Plugin Name: Get The Image
|
4 |
+
* Plugin URI: http://themehybrid.com/plugins/get-the-image
|
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.8
|
7 |
* Author: Justin Tadlock
|
8 |
* Author URI: http://justintadlock.com
|
9 |
*
|
23 |
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
24 |
*
|
25 |
* @package GetTheImage
|
26 |
+
* @version 0.8.0
|
27 |
* @author Justin Tadlock <justin@justintadlock.com>
|
28 |
+
* @copyright Copyright (c) 2008 - 2012, 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 |
*/
|
48 |
* 'image_scan', 'callback', and 'default_image'.
|
49 |
*
|
50 |
* @since 0.1.0
|
51 |
+
* @access public
|
52 |
* @global $post The current post's database object.
|
53 |
* @param array $args Arguments for how to load and display the image.
|
54 |
* @return string|array The HTML for the image. | Image attributes in an array.
|
55 |
*/
|
56 |
function get_the_image( $args = array() ) {
|
|
|
57 |
|
58 |
/* Set the default arguments. */
|
59 |
$defaults = array(
|
60 |
+
'meta_key' => array( 'Thumbnail', 'thumbnail' ), // array|string
|
61 |
+
'post_id' => get_the_ID(),
|
62 |
+
'attachment' => true,
|
63 |
+
'the_post_thumbnail' => true, // WP 2.9+ image function
|
64 |
+
'size' => 'thumbnail',
|
65 |
+
'default_image' => false,
|
66 |
+
'order_of_image' => 1,
|
67 |
+
'link_to_post' => true,
|
68 |
+
'image_class' => false,
|
69 |
+
'image_scan' => false,
|
70 |
+
'width' => false,
|
71 |
+
'height' => false,
|
72 |
+
'format' => 'img',
|
73 |
+
'meta_key_save' => false,
|
74 |
+
'thumbnail_id_save' => false, // Set 'featured image'.
|
75 |
+
'callback' => null,
|
76 |
+
'cache' => true,
|
77 |
+
'before' => '',
|
78 |
+
'after' => '',
|
79 |
+
'echo' => true,
|
80 |
+
'custom_key' => null, // @deprecated 0.6. Use 'meta_key'.
|
81 |
+
'default_size' => null, // @deprecated 0.5. Use 'size'.
|
82 |
);
|
83 |
|
84 |
/* Allow plugins/themes to filter the arguments. */
|
111 |
if ( !is_array( $image_cache ) )
|
112 |
$image_cache = array();
|
113 |
|
114 |
+
/* Set up a default, empty $image_html variable. */
|
115 |
+
$image_html = '';
|
116 |
+
|
117 |
/* If there is no cached image, let's see if one exists. */
|
118 |
if ( !isset( $image_cache[$key] ) || empty( $cache ) ) {
|
119 |
|
149 |
get_the_image_meta_key_save( $args, $image['src'] );
|
150 |
|
151 |
/* Format the image HTML. */
|
152 |
+
$image_html = get_the_image_format( $args, $image );
|
153 |
|
154 |
/* Set the image cache for the specific post. */
|
155 |
+
$image_cache[$key] = $image_html;
|
156 |
wp_cache_set( $post_id, $image_cache, 'get_the_image' );
|
157 |
}
|
158 |
}
|
159 |
|
160 |
/* If an image was already cached for the post and arguments, use it. */
|
161 |
else {
|
162 |
+
$image_html = $image_cache[$key];
|
163 |
}
|
164 |
|
165 |
/* Allow plugins/theme to override the final output. */
|
166 |
+
$image_html = apply_filters( 'get_the_image', $image_html );
|
167 |
|
168 |
/* If $format is set to 'array', return an array of image attributes. */
|
169 |
if ( 'array' == $format ) {
|
172 |
$out = array();
|
173 |
|
174 |
/* Get the image attributes. */
|
175 |
+
$atts = wp_kses_hair( $image_html, array( 'http' ) );
|
176 |
|
177 |
/* Loop through the image attributes and add them in key/value pairs for the return array. */
|
178 |
foreach ( $atts as $att )
|
186 |
|
187 |
/* Or, if $echo is set to false, return the formatted image. */
|
188 |
elseif ( false === $echo ) {
|
189 |
+
return $args['before'] . $image_html . $args['after'];
|
190 |
}
|
191 |
|
192 |
+
/* If there is a $post_thumbnail_id, do the actions associated with get_the_post_thumbnail(). */
|
193 |
+
if ( isset( $image['post_thumbnail_id'] ) )
|
194 |
+
do_action( 'begin_fetch_post_thumbnail_html', $post_id, $image['post_thumbnail_id'], $size );
|
195 |
+
|
196 |
/* Display the image if we get to this point. */
|
197 |
+
echo $args['before'] . $image_html . $args['after'];
|
198 |
+
|
199 |
+
/* If there is a $post_thumbnail_id, do the actions associated with get_the_post_thumbnail(). */
|
200 |
+
if ( isset( $image['post_thumbnail_id'] ) )
|
201 |
+
do_action( 'end_fetch_post_thumbnail_html', $post_id, $image['post_thumbnail_id'], $size );
|
202 |
}
|
203 |
|
204 |
/* Internal Functions */
|
208 |
* is found, $image is set and the loop breaks. If an image is found, it is returned.
|
209 |
*
|
210 |
* @since 0.7.0
|
211 |
+
* @access private
|
212 |
* @param array $args Arguments for how to load and display the image.
|
213 |
* @return array|bool Array of image attributes. | False if no image is found.
|
214 |
*/
|
215 |
function get_the_image_by_meta_key( $args = array() ) {
|
216 |
|
217 |
/* If $meta_key is not an array. */
|
218 |
+
if ( !is_array( $args['meta_key'] ) )
|
219 |
+
$args['meta_key'] = array( $args['meta_key'] );
|
|
|
|
|
|
|
220 |
|
221 |
+
/* Loop through each of the given meta keys. */
|
222 |
+
foreach ( $args['meta_key'] as $meta_key ) {
|
223 |
|
224 |
+
/* Get the image URL by the current meta key in the loop. */
|
225 |
+
$image = get_post_meta( $args['post_id'], $meta_key, true );
|
226 |
|
227 |
+
/* If an image was found, break out of the loop. */
|
228 |
+
if ( !empty( $image ) )
|
229 |
+
break;
|
|
|
|
|
|
|
|
|
230 |
}
|
231 |
|
232 |
/* If a custom key value has been given for one of the keys, return the image URL. */
|
242 |
* later added in the display_the_image() function.
|
243 |
*
|
244 |
* @since 0.7.0
|
245 |
+
* @access private
|
246 |
* @param array $args Arguments for how to load and display the image.
|
247 |
* @return array|bool Array of image attributes. | False if no image is found.
|
248 |
*/
|
273 |
* attachments are found, loop through each. The loop only breaks once $order_of_image is reached.
|
274 |
*
|
275 |
* @since 0.7.0
|
276 |
+
* @access private
|
277 |
* @param array $args Arguments for how to load and display the image.
|
278 |
* @return array|bool Array of image attributes. | False if no image is found.
|
279 |
*/
|
280 |
function get_the_image_by_attachment( $args = array() ) {
|
281 |
|
282 |
+
/* Get the post type of the current post. */
|
283 |
+
$post_type = get_post_type( $args['post_id'] );
|
284 |
|
285 |
+
/* Check if the post itself is an image attachment. */
|
286 |
+
if ( 'attachment' == $post_type && wp_attachment_is_image( $args['post_id'] ) ) {
|
287 |
+
$attachment_id = $args['post_id'];
|
288 |
+
}
|
289 |
+
|
290 |
+
/* If the post is not an attachment, check if it has any image attachments. */
|
291 |
+
elseif ( 'attachment' !== $post_type ) {
|
292 |
+
|
293 |
+
/* Get attachments for the inputted $post_id. */
|
294 |
+
$attachments = get_children(
|
295 |
+
array(
|
296 |
+
'post_parent' => $args['post_id'],
|
297 |
+
'post_status' => 'inherit',
|
298 |
+
'post_type' => 'attachment',
|
299 |
+
'post_mime_type' => 'image',
|
300 |
+
'order' => 'ASC',
|
301 |
+
'orderby' => 'menu_order ID',
|
302 |
+
'suppress_filters' => true
|
303 |
+
)
|
304 |
+
);
|
305 |
+
|
306 |
+
/* Check if any attachments were found. */
|
307 |
+
if ( !empty( $attachments ) ) {
|
308 |
+
|
309 |
+
/* Set the default iterator to 0. */
|
310 |
+
$i = 0;
|
311 |
+
|
312 |
+
/* Loop through each attachment. */
|
313 |
+
foreach ( $attachments as $id => $attachment ) {
|
314 |
+
|
315 |
+
/* Set the attachment ID as the current ID in the loop. */
|
316 |
+
$attachment_id = $id;
|
317 |
+
|
318 |
+
/* Break if/when we hit 'order_of_image'. */
|
319 |
+
if ( ++$i == $args['order_of_image'] )
|
320 |
+
break;
|
321 |
+
}
|
322 |
}
|
323 |
}
|
324 |
|
325 |
+
/* Check if we have an attachment ID before proceeding. */
|
326 |
+
if ( !empty( $attachment_id ) ) {
|
|
|
327 |
|
328 |
+
/* Get the attachment image. */
|
329 |
+
$image = wp_get_attachment_image_src( $id, $args['size'] );
|
330 |
|
331 |
+
/* Get the attachment excerpt. */
|
332 |
+
$alt = trim( strip_tags( get_post_field( 'post_excerpt', $id ) ) );
|
333 |
+
|
334 |
+
/* Save the attachment as the 'featured image'. */
|
335 |
+
if ( true === $args['thumbnail_id_save'] )
|
336 |
+
set_post_thumbnail( $args['post_id'], $id );
|
337 |
+
|
338 |
+
/* Return the image URL. */
|
339 |
+
return array( 'src' => $image[0], 'alt' => $alt );
|
340 |
}
|
341 |
|
342 |
+
/* Return false for anything else. */
|
343 |
+
return false;
|
344 |
}
|
345 |
|
346 |
/**
|
348 |
* if using large images within posts, better to use the other options.
|
349 |
*
|
350 |
* @since 0.7.0
|
351 |
+
* @access private
|
352 |
* @param array $args Arguments for how to load and display the image.
|
353 |
* @return array|bool Array of image attributes. | False if no image is found.
|
354 |
*/
|
369 |
* Not used with get_the_image() by default.
|
370 |
*
|
371 |
* @since 0.7.0
|
372 |
+
* @access private
|
373 |
* @param array $args Arguments for how to load and display the image.
|
374 |
* @return array|bool Array of image attributes. | False if no image is found.
|
375 |
*/
|
382 |
* only be called if there is an image to display, but will handle it if not.
|
383 |
*
|
384 |
* @since 0.7.0
|
385 |
+
* @access private
|
386 |
* @param array $args Arguments for how to load and display the image.
|
387 |
* @param array $image Array of image attributes ($image, $classes, $alt, $caption).
|
388 |
* @return string $image Formatted image (w/link to post if the option is set).
|
406 |
/* Loop through the custom field keys and add them as classes. */
|
407 |
if ( is_array( $meta_key ) ) {
|
408 |
foreach ( $meta_key as $key )
|
409 |
+
$classes[] = sanitize_html_class( $key );
|
410 |
}
|
411 |
|
412 |
/* Add the $size and any user-added $image_class to the class. */
|
413 |
+
$classes[] = sanitize_html_class( $size );
|
414 |
+
$classes[] = sanitize_html_class( $image_class );
|
415 |
|
416 |
/* Join all the classes into a single string and make sure there are no duplicates. */
|
417 |
$class = join( ' ', array_unique( $classes ) );
|
418 |
|
|
|
|
|
|
|
|
|
419 |
/* Add the image attributes to the <img /> element. */
|
420 |
$html = '<img src="' . $image['src'] . '" alt="' . esc_attr( strip_tags( $image_alt ) ) . '" class="' . esc_attr( $class ) . '"' . $width . $height . ' />';
|
421 |
|
423 |
if ( $link_to_post )
|
424 |
$html = '<a href="' . get_permalink( $post_id ) . '" title="' . esc_attr( apply_filters( 'the_title', get_post_field( 'post_title', $post_id ) ) ) . '">' . $html . '</a>';
|
425 |
|
|
|
|
|
|
|
|
|
426 |
/* If there is a $post_thumbnail_id, apply the WP filters normally associated with get_the_post_thumbnail(). */
|
427 |
if ( !empty( $image['post_thumbnail_id'] ) )
|
428 |
$html = apply_filters( 'post_thumbnail_html', $html, $post_id, $image['post_thumbnail_id'], $size, '' );
|
436 |
* of expensive scans of the content when using the image scan feature.
|
437 |
*
|
438 |
* @since 0.6.0
|
439 |
+
* @access private
|
440 |
* @param array $args Arguments for how to load and display the image.
|
441 |
* @param array $image Array of image attributes ($image, $classes, $alt, $caption).
|
442 |
*/
|
462 |
* Deletes the image cache for the specific post when the 'save_post' hook is fired.
|
463 |
*
|
464 |
* @since 0.7.0
|
465 |
+
* @access private
|
466 |
+
* @param int $post_id The ID of the post to delete the cache for.
|
467 |
+
* @return void
|
468 |
*/
|
469 |
function get_the_image_delete_cache_by_post( $post_id ) {
|
470 |
wp_cache_delete( $post_id, 'get_the_image' );
|
475 |
* or 'updated_post_meta' hooks are called.
|
476 |
*
|
477 |
* @since 0.7.0
|
478 |
+
* @access private
|
479 |
+
* @param int $meta_id The ID of the metadata being updated.
|
480 |
+
* @param int $post_id The ID of the post to delete the cache for.
|
481 |
+
* @return void
|
482 |
*/
|
483 |
function get_the_image_delete_cache_by_meta( $meta_id, $post_id ) {
|
484 |
wp_cache_delete( $post_id, 'get_the_image' );
|
readme.html
CHANGED
@@ -50,10 +50,10 @@
|
|
50 |
<p>By simply making a function call to <code><?php get_the_image(); ?></code> within a template file, the script will default to this:</p>
|
51 |
|
52 |
<pre><code>$defaults = array(
|
53 |
-
'meta_key' => array( 'Thumbnail', 'thumbnail' ),
|
54 |
-
'post_id' =>
|
55 |
'attachment' => true,
|
56 |
-
'the_post_thumbnail' => true,
|
57 |
'size' => 'thumbnail',
|
58 |
'default_image' => false,
|
59 |
'order_of_image' => 1,
|
@@ -64,9 +64,14 @@
|
|
64 |
'height' => false,
|
65 |
'format' => 'img',
|
66 |
'meta_key_save' => false,
|
|
|
67 |
'callback' => null,
|
68 |
'cache' => true,
|
69 |
-
'
|
|
|
|
|
|
|
|
|
70 |
);</code></pre>
|
71 |
|
72 |
<dl>
|
@@ -98,10 +103,16 @@
|
|
98 |
<dd>What format to return the image in. If set to <code>array</code> the return value of the function will be an array of <code><img></code> attributes. All other values will return the <code><img></code> element.</dd>
|
99 |
<dt>meta_key_save</dt>
|
100 |
<dd>A meta key to save the image <acronym title="Uniform Resource Locator">URL</acronym> as. This is useful if you're not using custom fields but want to cut back on database queries by having the script automatically set the custom field for you. By default, this is set to <code>false</code>.</dd>
|
|
|
|
|
101 |
<dt>callback</dt>
|
102 |
<dd>A custom callback function that will be called if set. It's only called if no images are found by any other options of the plugin. However, it will be run before the <code>default_image</code> is set. The <code>$args</code> array is passed to the callback function as the only parameter.</dd>
|
103 |
<dt>cache</dt>
|
104 |
<dd>Whether to use the WordPress Cache <acronym title="Application Programming Interface">API</acronym> (integrates with caching plugins) to serve the post images. By default, this is set to <code>true</code>.</dd>
|
|
|
|
|
|
|
|
|
105 |
<dt>echo</dt>
|
106 |
<dd>If set to <code>true</code>, the image is shown on the page. If set to <code>false</code>, the image will be returned to use in your own function. (Set to <code>true</code> by default.)</dd>
|
107 |
</dl>
|
@@ -174,7 +185,7 @@
|
|
174 |
|
175 |
<p>Let's suppose you've used this code:</p>
|
176 |
|
177 |
-
<pre><code><?php get_the_image( array( '
|
178 |
|
179 |
<p>This will give you these CSS classes to work with:</p>
|
180 |
|
@@ -193,7 +204,7 @@ img.thumbnail { }</code></pre>
|
|
193 |
|
194 |
<h2>Plugin support</h2>
|
195 |
|
196 |
-
<p>I run a WordPress community called <a href="http://themehybrid.com" title="Theme Hybrid">Theme Hybrid</a>, which is where I fully support all of my WordPress projects, including plugins. You can sign up for an account to get plugin support for a small yearly fee ($
|
197 |
|
198 |
<p>I know. I know. You might not want to pay for support, but just consider it a donation to the project. To continue making cool, GPL-licensed plugins and having the time to support them, I must pay the bills.</p>
|
199 |
|
@@ -203,7 +214,7 @@ img.thumbnail { }</code></pre>
|
|
203 |
|
204 |
<p>This plugin is copyrighted to <a href="http://justintadlock.com" title="Justin Tadlock">Justin Tadlock</a>.</p>
|
205 |
|
206 |
-
<p>2008 – 
|
207 |
|
208 |
</body>
|
209 |
</html>
|
50 |
<p>By simply making a function call to <code><?php get_the_image(); ?></code> within a template file, the script will default to this:</p>
|
51 |
|
52 |
<pre><code>$defaults = array(
|
53 |
+
'meta_key' => array( 'Thumbnail', 'thumbnail' ), // array|string
|
54 |
+
'post_id' => get_the_ID(),
|
55 |
'attachment' => true,
|
56 |
+
'the_post_thumbnail' => true, // WP 2.9+ image function
|
57 |
'size' => 'thumbnail',
|
58 |
'default_image' => false,
|
59 |
'order_of_image' => 1,
|
64 |
'height' => false,
|
65 |
'format' => 'img',
|
66 |
'meta_key_save' => false,
|
67 |
+
'thumbnail_id_save' => false, // Set 'featured image'.
|
68 |
'callback' => null,
|
69 |
'cache' => true,
|
70 |
+
'before' => '',
|
71 |
+
'after' => '',
|
72 |
+
'echo' => true,
|
73 |
+
'custom_key' => null, // @deprecated 0.6. Use 'meta_key'.
|
74 |
+
'default_size' => null, // @deprecated 0.5. Use 'size'.
|
75 |
);</code></pre>
|
76 |
|
77 |
<dl>
|
103 |
<dd>What format to return the image in. If set to <code>array</code> the return value of the function will be an array of <code><img></code> attributes. All other values will return the <code><img></code> element.</dd>
|
104 |
<dt>meta_key_save</dt>
|
105 |
<dd>A meta key to save the image <acronym title="Uniform Resource Locator">URL</acronym> as. This is useful if you're not using custom fields but want to cut back on database queries by having the script automatically set the custom field for you. By default, this is set to <code>false</code>.</dd>
|
106 |
+
<dt>thumbnail_id_save</dt>
|
107 |
+
<dd>Whether to save the attachment ID as the post thumbnail (featured image) ID if no featured image is set for the post. By default, this is set to <code>false</code></dd>
|
108 |
<dt>callback</dt>
|
109 |
<dd>A custom callback function that will be called if set. It's only called if no images are found by any other options of the plugin. However, it will be run before the <code>default_image</code> is set. The <code>$args</code> array is passed to the callback function as the only parameter.</dd>
|
110 |
<dt>cache</dt>
|
111 |
<dd>Whether to use the WordPress Cache <acronym title="Application Programming Interface">API</acronym> (integrates with caching plugins) to serve the post images. By default, this is set to <code>true</code>.</dd>
|
112 |
+
<dt>before</dt>
|
113 |
+
<dd><abbr title="Hypertext Markup Language">HTML</abbr> to place before the output of the image.</dd>
|
114 |
+
<dt>after</dt>
|
115 |
+
<dd><abbr title="Hypertext Markup Language">HTML</abbr> to place after the output of the image.</dd>
|
116 |
<dt>echo</dt>
|
117 |
<dd>If set to <code>true</code>, the image is shown on the page. If set to <code>false</code>, the image will be returned to use in your own function. (Set to <code>true</code> by default.)</dd>
|
118 |
</dl>
|
185 |
|
186 |
<p>Let's suppose you've used this code:</p>
|
187 |
|
188 |
+
<pre><code><?php get_the_image( array( 'meta_key' => array( 'Donkey Kong', 'mario' ), 'size' => 'full' ) ); ?></code></pre>
|
189 |
|
190 |
<p>This will give you these CSS classes to work with:</p>
|
191 |
|
204 |
|
205 |
<h2>Plugin support</h2>
|
206 |
|
207 |
+
<p>I run a WordPress community called <a href="http://themehybrid.com" title="Theme Hybrid">Theme Hybrid</a>, which is where I fully support all of my WordPress projects, including plugins. You can sign up for an account to get plugin support for a small yearly fee ($29 <acronym title="United States Dollars">USD</acronym> at the time of writing).</p>
|
208 |
|
209 |
<p>I know. I know. You might not want to pay for support, but just consider it a donation to the project. To continue making cool, GPL-licensed plugins and having the time to support them, I must pay the bills.</p>
|
210 |
|
214 |
|
215 |
<p>This plugin is copyrighted to <a href="http://justintadlock.com" title="Justin Tadlock">Justin Tadlock</a>.</p>
|
216 |
|
217 |
+
<p>2008 – 2012 © Justin Tadlock. All rights reserved.</p>
|
218 |
|
219 |
</body>
|
220 |
</html>
|
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: 3.2
|
6 |
-
Tested up to: 3.
|
7 |
-
Stable tag: 0.
|
8 |
|
9 |
An easy-to-use image script for adding things such as thumbnails and feature images.
|
10 |
|
@@ -56,6 +56,19 @@ You can view this plugin in action on my <a href="http://justintadlock.com" titl
|
|
56 |
|
57 |
== Changelog ==
|
58 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
**Version 0.7**
|
60 |
|
61 |
* Deprecated and replaced functions that lacked the `get_the_image_` prefix.
|
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: 3.2
|
6 |
+
Tested up to: 3.4
|
7 |
+
Stable tag: 0.8
|
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.8**
|
60 |
+
|
61 |
+
* Inline docs updates.
|
62 |
+
* Added the `before` argument to output HTML before the image.
|
63 |
+
* Added the `after` argument to output HTML after the image.
|
64 |
+
* Added the `thumbnail_id_save` argument to allow the attached image to be saved as the thumbnail/featured image.
|
65 |
+
* Get the post ID via `get_the_ID()` rather than the global `$post` object.
|
66 |
+
* Fixed debug notice with `$image_html`.
|
67 |
+
* Moved the `*_fetch_post_thumbnail_html` hooks into the main function and only fire them if displaying to the screen.
|
68 |
+
* Simplified the `meta_key` logic.
|
69 |
+
* Completely rewrote the `attachment` logic.
|
70 |
+
* Sanitize classes with `sanitize_html_class()`.
|
71 |
+
|
72 |
**Version 0.7**
|
73 |
|
74 |
* Deprecated and replaced functions that lacked the `get_the_image_` prefix.
|