Version Description
Download this release
Release Info
Developer | greenshady |
Plugin | Get the Image |
Version | 1.0.0 |
Comparing to | |
See all releases |
Code changes from version 0.9.0 to 1.0.0
- get-the-image.php +734 -390
- readme.md +66 -32
- readme.txt +34 -4
get-the-image.php
CHANGED
@@ -1,11 +1,11 @@
|
|
1 |
<?php
|
2 |
/**
|
3 |
* Plugin Name: Get The Image
|
4 |
-
* Plugin URI:
|
5 |
* Description: This is a highly intuitive script that can grab an image by custom field, featured image, post attachment, or extracting it from the post's content.
|
6 |
-
* Version:
|
7 |
-
* Author:
|
8 |
-
* Author URI:
|
9 |
*/
|
10 |
|
11 |
/**
|
@@ -25,9 +25,9 @@
|
|
25 |
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
26 |
*
|
27 |
* @package GetTheImage
|
28 |
-
* @version 0.
|
29 |
* @author Justin Tadlock <justin@justintadlock.com>
|
30 |
-
* @copyright Copyright (c) 2008 -
|
31 |
* @link http://justintadlock.com/archives/2008/05/27/get-the-image-wordpress-plugin
|
32 |
* @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
33 |
*/
|
@@ -36,325 +36,586 @@
|
|
36 |
add_theme_support( 'post-thumbnails' );
|
37 |
|
38 |
/* Delete the cache when a post or post metadata is updated. */
|
39 |
-
add_action( 'save_post',
|
40 |
add_action( 'deleted_post_meta', 'get_the_image_delete_cache_by_meta', 10, 2 );
|
41 |
add_action( 'updated_post_meta', 'get_the_image_delete_cache_by_meta', 10, 2 );
|
42 |
-
add_action( 'added_post_meta',
|
43 |
|
44 |
/**
|
45 |
-
* The main image function for displaying an image.
|
46 |
-
*
|
47 |
*
|
48 |
-
*
|
49 |
-
* will no longer look for images. The check order is 'meta_key', 'the_post_thumbnail', 'attachment',
|
50 |
-
* 'image_scan', 'callback', and 'default_image'.
|
51 |
-
*
|
52 |
-
* @since 0.1.0
|
53 |
* @access public
|
54 |
-
* @
|
55 |
-
* @
|
56 |
-
* @return string|array The HTML for the image. | Image attributes in an array.
|
57 |
*/
|
58 |
function get_the_image( $args = array() ) {
|
59 |
-
global $_wp_additional_image_sizes;
|
60 |
-
|
61 |
-
/* Set the default arguments. */
|
62 |
-
$defaults = array(
|
63 |
-
|
64 |
-
/* Post the image is associated with. */
|
65 |
-
'post_id' => get_the_ID(),
|
66 |
|
67 |
-
|
68 |
-
'meta_key' => array( 'Thumbnail', 'thumbnail' ), // array|string
|
69 |
-
'the_post_thumbnail' => true,
|
70 |
-
'attachment' => true,
|
71 |
-
'image_scan' => false,
|
72 |
-
'callback' => null,
|
73 |
-
'default_image' => false,
|
74 |
|
75 |
-
|
76 |
-
|
77 |
-
'order_of_image' => 1,
|
78 |
|
79 |
-
/* Format/display of image. */
|
80 |
-
'link_to_post' => true,
|
81 |
-
'image_class' => false,
|
82 |
-
'width' => false,
|
83 |
-
'height' => false,
|
84 |
-
'before' => '',
|
85 |
-
'after' => '',
|
86 |
|
87 |
-
|
88 |
-
'caption' => false, // Default WP [caption] requires a width.
|
89 |
|
90 |
-
/* Saving the image. */
|
91 |
-
'meta_key_save' => false,
|
92 |
-
'thumbnail_id_save' => false, // Set 'featured image'.
|
93 |
-
'cache' => true,
|
94 |
|
95 |
-
|
96 |
-
|
97 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
98 |
|
99 |
-
/*
|
100 |
-
|
101 |
-
|
102 |
-
|
|
|
103 |
|
104 |
-
|
105 |
-
|
|
|
106 |
|
107 |
-
|
108 |
-
$args = wp_parse_args( $args, $defaults );
|
109 |
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
|
126 |
-
|
127 |
-
|
|
|
128 |
|
129 |
-
|
130 |
-
$key = md5( serialize( compact( array_keys( $args ) ) ) );
|
131 |
|
132 |
-
|
133 |
-
|
|
|
134 |
|
135 |
-
|
136 |
-
$
|
137 |
|
138 |
-
|
139 |
-
|
|
|
|
|
140 |
|
141 |
-
|
142 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
143 |
|
144 |
-
/*
|
145 |
-
|
146 |
-
$image = get_the_image_by_meta_key( $args );
|
147 |
|
148 |
-
/* If
|
149 |
-
if (
|
150 |
-
$image = get_the_image_by_post_thumbnail( $args );
|
151 |
|
152 |
-
|
153 |
-
|
154 |
-
$image = get_the_image_by_attachment( $args );
|
155 |
|
156 |
-
|
157 |
-
|
158 |
-
$image = get_the_image_by_scan( $args );
|
159 |
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
|
168 |
-
/*
|
169 |
-
|
|
|
|
|
170 |
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
|
175 |
-
|
176 |
-
|
177 |
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
}
|
182 |
}
|
183 |
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
|
|
|
|
|
|
|
|
|
|
188 |
|
189 |
-
|
190 |
-
|
191 |
|
192 |
-
|
193 |
-
|
194 |
|
195 |
-
|
196 |
-
|
197 |
|
198 |
-
/*
|
199 |
-
|
200 |
|
201 |
-
|
202 |
-
foreach ( $atts as $att )
|
203 |
-
$out[ $att['name'] ] = $att['value'];
|
204 |
|
205 |
-
|
206 |
-
|
207 |
|
208 |
-
|
209 |
-
|
210 |
-
}
|
211 |
|
212 |
-
|
213 |
-
|
214 |
-
return !empty( $image_html ) ? $args['before'] . $image_html . $args['after'] : $image_html;
|
215 |
-
}
|
216 |
|
217 |
-
|
218 |
-
|
219 |
-
do_action( 'begin_fetch_post_thumbnail_html', $post_id, $image['post_thumbnail_id'], $size );
|
220 |
|
221 |
-
|
222 |
-
|
223 |
|
224 |
-
|
225 |
-
|
226 |
-
do_action( 'end_fetch_post_thumbnail_html', $post_id, $image['post_thumbnail_id'], $size );
|
227 |
-
}
|
228 |
|
229 |
-
|
|
|
230 |
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
*
|
235 |
-
* @since 0.7.0
|
236 |
-
* @access private
|
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 |
-
*/
|
240 |
-
function get_the_image_by_meta_key( $args = array() ) {
|
241 |
|
242 |
-
|
243 |
-
|
244 |
-
|
|
|
|
|
|
|
245 |
|
246 |
-
|
247 |
-
|
|
|
248 |
|
249 |
-
|
250 |
-
|
|
|
|
|
|
|
251 |
|
252 |
-
/* If an image was
|
253 |
-
|
254 |
-
|
|
|
255 |
}
|
256 |
|
257 |
-
|
258 |
-
|
259 |
-
|
|
|
|
|
|
|
|
|
|
|
260 |
|
261 |
-
|
262 |
-
|
|
|
263 |
|
264 |
-
|
265 |
-
|
266 |
-
* If an image is found, return it and the $post_thumbnail_id. The WordPress function's other filters are
|
267 |
-
* later added in the display_the_image() function.
|
268 |
-
*
|
269 |
-
* @since 0.7.0
|
270 |
-
* @access private
|
271 |
-
* @param array $args Arguments for how to load and display the image.
|
272 |
-
* @return array|bool Array of image attributes. | False if no image is found.
|
273 |
-
*/
|
274 |
-
function get_the_image_by_post_thumbnail( $args = array() ) {
|
275 |
|
276 |
-
|
277 |
-
|
278 |
|
279 |
-
|
280 |
-
|
281 |
-
|
|
|
282 |
|
283 |
-
|
284 |
-
|
|
|
285 |
|
286 |
-
|
287 |
-
|
|
|
|
|
288 |
|
289 |
-
|
290 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
291 |
|
292 |
-
|
293 |
-
|
294 |
|
295 |
-
|
296 |
-
|
297 |
-
|
298 |
|
299 |
-
|
300 |
-
|
301 |
-
* attachments are found, loop through each. The loop only breaks once $order_of_image is reached.
|
302 |
-
*
|
303 |
-
* @since 0.7.0
|
304 |
-
* @access private
|
305 |
-
* @param array $args Arguments for how to load and display the image.
|
306 |
-
* @return array|bool Array of image attributes. | False if no image is found.
|
307 |
-
*/
|
308 |
-
function get_the_image_by_attachment( $args = array() ) {
|
309 |
|
310 |
-
|
311 |
-
|
312 |
|
313 |
-
|
314 |
-
|
315 |
-
$attachment_id = $args['post_id'];
|
316 |
}
|
317 |
|
318 |
-
|
319 |
-
|
320 |
-
|
321 |
-
|
322 |
-
|
323 |
-
|
324 |
-
|
325 |
-
|
326 |
-
|
327 |
-
|
328 |
-
|
329 |
-
|
330 |
-
|
331 |
-
|
332 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
333 |
|
334 |
-
|
335 |
-
|
|
|
336 |
|
337 |
-
|
338 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
339 |
|
340 |
-
|
341 |
-
|
342 |
|
343 |
-
|
344 |
-
|
345 |
|
346 |
-
|
347 |
-
|
348 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
349 |
}
|
350 |
}
|
|
|
|
|
|
|
|
|
351 |
}
|
352 |
|
353 |
-
|
354 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
355 |
|
356 |
/* Get the attachment image. */
|
357 |
-
$image = wp_get_attachment_image_src( $attachment_id, $args['size'] );
|
358 |
|
359 |
/* Get the attachment alt text. */
|
360 |
$alt = trim( strip_tags( get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ) ) );
|
@@ -363,151 +624,188 @@ function get_the_image_by_attachment( $args = array() ) {
|
|
363 |
$caption = get_post_field( 'post_excerpt', $attachment_id );
|
364 |
|
365 |
/* Save the attachment as the 'featured image'. */
|
366 |
-
if ( true === $args['thumbnail_id_save'] )
|
367 |
-
|
368 |
-
|
369 |
-
/*
|
370 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
371 |
}
|
372 |
|
373 |
-
|
374 |
-
|
375 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
376 |
|
377 |
-
|
378 |
-
|
379 |
-
|
380 |
-
*
|
381 |
-
* @since 0.7.0
|
382 |
-
* @access private
|
383 |
-
* @param array $args Arguments for how to load and display the image.
|
384 |
-
* @return array|bool Array of image attributes. | False if no image is found.
|
385 |
-
*/
|
386 |
-
function get_the_image_by_scan( $args = array() ) {
|
387 |
|
388 |
-
|
389 |
-
|
|
|
390 |
|
391 |
-
|
392 |
-
|
393 |
-
|
394 |
|
395 |
-
|
396 |
-
|
397 |
|
398 |
-
|
399 |
-
|
400 |
-
* Not used with get_the_image() by default.
|
401 |
-
*
|
402 |
-
* @since 0.7.0
|
403 |
-
* @access private
|
404 |
-
* @param array $args Arguments for how to load and display the image.
|
405 |
-
* @return array|bool Array of image attributes. | False if no image is found.
|
406 |
-
*/
|
407 |
-
function get_the_image_by_default( $args = array() ) {
|
408 |
-
return array( 'src' => $args['default_image'] );
|
409 |
-
}
|
410 |
|
411 |
-
|
412 |
-
|
413 |
-
* only be called if there is an image to display, but will handle it if not.
|
414 |
-
*
|
415 |
-
* @since 0.7.0
|
416 |
-
* @access private
|
417 |
-
* @param array $args Arguments for how to load and display the image.
|
418 |
-
* @param array $image Array of image attributes ($image, $classes, $alt, $caption).
|
419 |
-
* @return string $image Formatted image (w/link to post if the option is set).
|
420 |
-
*/
|
421 |
-
function get_the_image_format( $args = array(), $image = false ) {
|
422 |
|
423 |
-
|
424 |
-
|
425 |
-
return false;
|
426 |
|
427 |
-
|
428 |
-
|
|
|
|
|
|
|
|
|
429 |
|
430 |
-
|
431 |
-
|
|
|
432 |
|
433 |
-
|
434 |
-
|
435 |
-
|
436 |
|
437 |
-
|
438 |
-
|
439 |
-
foreach ( $meta_key as $key )
|
440 |
-
$classes[] = $key;
|
441 |
-
}
|
442 |
|
443 |
-
|
444 |
-
|
445 |
|
446 |
-
|
447 |
-
|
448 |
-
if ( !is_array( $image_class ) )
|
449 |
-
$image_class = preg_split( '#\s+#', $image_class );
|
450 |
-
$classes = array_merge( $classes, $image_class );
|
451 |
-
}
|
452 |
|
453 |
-
|
454 |
-
|
455 |
|
456 |
-
|
457 |
-
|
458 |
|
459 |
-
|
460 |
-
|
461 |
|
462 |
-
|
463 |
-
|
464 |
-
$html = '<a href="' . get_permalink( $post_id ) . '" title="' . esc_attr( get_post_field( 'post_title', $post_id ) ) . '">' . $html . '</a>';
|
465 |
|
466 |
-
|
467 |
-
|
468 |
-
|
469 |
|
470 |
-
|
471 |
-
|
472 |
-
|
473 |
|
474 |
-
|
475 |
-
|
|
|
476 |
|
477 |
-
|
478 |
-
|
479 |
-
|
480 |
-
|
481 |
-
|
482 |
-
|
483 |
-
|
484 |
-
|
485 |
-
|
486 |
-
|
487 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
488 |
|
489 |
-
|
490 |
-
|
491 |
-
|
|
|
|
|
492 |
|
493 |
-
|
494 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
495 |
|
496 |
-
|
497 |
-
if ( empty( $meta ) )
|
498 |
-
add_post_meta( $args['post_id'], $args['meta_key_save'], $image['src'] );
|
499 |
|
500 |
-
|
501 |
-
|
502 |
-
update_post_meta( $args['post_id'], $args['meta_key_save'], $image['src'], $meta );
|
503 |
}
|
504 |
|
505 |
/**
|
506 |
* Deletes the image cache for the specific post when the 'save_post' hook is fired.
|
507 |
*
|
508 |
-
* @since
|
509 |
* @access private
|
510 |
-
* @param
|
511 |
* @return void
|
512 |
*/
|
513 |
function get_the_image_delete_cache_by_post( $post_id ) {
|
@@ -518,78 +816,124 @@ function get_the_image_delete_cache_by_post( $post_id ) {
|
|
518 |
* Deletes the image cache for a specific post when the 'added_post_meta', 'deleted_post_meta',
|
519 |
* or 'updated_post_meta' hooks are called.
|
520 |
*
|
521 |
-
* @since
|
522 |
* @access private
|
523 |
-
* @param
|
524 |
-
* @param
|
525 |
* @return void
|
526 |
*/
|
527 |
function get_the_image_delete_cache_by_meta( $meta_id, $post_id ) {
|
528 |
wp_cache_delete( $post_id, 'get_the_image' );
|
529 |
}
|
530 |
|
|
|
|
|
|
|
|
|
531 |
/**
|
532 |
-
* @since
|
533 |
* @deprecated 0.3.0
|
|
|
534 |
*/
|
535 |
-
function get_the_image_link(
|
536 |
-
get_the_image
|
|
|
537 |
}
|
538 |
|
539 |
/**
|
540 |
-
* @since
|
541 |
* @deprecated 0.7.0
|
|
|
542 |
*/
|
543 |
-
function image_by_custom_field(
|
544 |
-
return get_the_image_by_meta_key( $args );
|
545 |
-
}
|
546 |
|
547 |
/**
|
548 |
-
* @since
|
549 |
* @deprecated 0.7.0
|
|
|
550 |
*/
|
551 |
-
function image_by_the_post_thumbnail(
|
552 |
-
return get_the_image_by_post_thumbnail( $args );
|
553 |
-
}
|
554 |
|
555 |
/**
|
556 |
-
* @since
|
557 |
* @deprecated 0.7.0
|
|
|
558 |
*/
|
559 |
-
function image_by_attachment(
|
560 |
-
return get_the_image_by_attachment( $args );
|
561 |
-
}
|
562 |
|
563 |
/**
|
564 |
-
* @since
|
565 |
* @deprecated 0.7.0
|
|
|
566 |
*/
|
567 |
-
function image_by_scan(
|
568 |
-
return get_the_image_by_scan( $args );
|
569 |
-
}
|
570 |
|
571 |
/**
|
572 |
-
* @since
|
573 |
* @deprecated 0.7.0
|
|
|
574 |
*/
|
575 |
-
function image_by_default(
|
576 |
-
return get_the_image_by_default( $args );
|
577 |
-
}
|
578 |
|
579 |
/**
|
580 |
-
* @since
|
581 |
* @deprecated 0.7.0
|
|
|
582 |
*/
|
583 |
-
function display_the_image(
|
584 |
-
return get_the_image_format( $args, $image );
|
585 |
-
}
|
586 |
|
587 |
/**
|
588 |
-
* @since
|
589 |
-
* @deprecated 0.7.0
|
|
|
590 |
*/
|
591 |
-
function get_the_image_delete_cache() {
|
592 |
-
|
593 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
594 |
|
595 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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, featured image, post attachment, or extracting it from the post's content.
|
6 |
+
* Version: 1.0.0
|
7 |
+
* Author: Justin Tadlock
|
8 |
+
* Author URI: http://justintadlock.com
|
9 |
*/
|
10 |
|
11 |
/**
|
25 |
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
26 |
*
|
27 |
* @package GetTheImage
|
28 |
+
* @version 1.0.0
|
29 |
* @author Justin Tadlock <justin@justintadlock.com>
|
30 |
+
* @copyright Copyright (c) 2008 - 2014, Justin Tadlock
|
31 |
* @link http://justintadlock.com/archives/2008/05/27/get-the-image-wordpress-plugin
|
32 |
* @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
33 |
*/
|
36 |
add_theme_support( 'post-thumbnails' );
|
37 |
|
38 |
/* Delete the cache when a post or post metadata is updated. */
|
39 |
+
add_action( 'save_post', 'get_the_image_delete_cache_by_post' );
|
40 |
add_action( 'deleted_post_meta', 'get_the_image_delete_cache_by_meta', 10, 2 );
|
41 |
add_action( 'updated_post_meta', 'get_the_image_delete_cache_by_meta', 10, 2 );
|
42 |
+
add_action( 'added_post_meta', 'get_the_image_delete_cache_by_meta', 10, 2 );
|
43 |
|
44 |
/**
|
45 |
+
* The main image function for displaying an image. This is a wrapper for the Get_The_Image class. Use this
|
46 |
+
* function in themes rather than the class.
|
47 |
*
|
48 |
+
* @since 0.1.0
|
|
|
|
|
|
|
|
|
49 |
* @access public
|
50 |
+
* @param array $args Arguments for how to load and display the image.
|
51 |
+
* @return string|array The HTML for the image. | Image attributes in an array.
|
|
|
52 |
*/
|
53 |
function get_the_image( $args = array() ) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
|
55 |
+
$image = new Get_The_Image( $args );
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
|
57 |
+
return $image->get_image();
|
58 |
+
}
|
|
|
59 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
|
61 |
+
/* === Internal Plugin Code: Don't use the below unless you know what you're doing. Expect breakage. === */
|
|
|
62 |
|
|
|
|
|
|
|
|
|
63 |
|
64 |
+
/**
|
65 |
+
* Class for getting images related to a post. Only use this class in your projects if you absolutely know
|
66 |
+
* what you're doing and expect your code to break in future versions. Use the the `get_the_image()`
|
67 |
+
* wrapper function instead. That's the reason it exists.
|
68 |
+
*
|
69 |
+
* @since 1.0.0
|
70 |
+
* @access private
|
71 |
+
*/
|
72 |
+
final class Get_The_Image {
|
73 |
+
|
74 |
+
/**
|
75 |
+
* Array of arguments passed in by the user and merged with the defaults.
|
76 |
+
*
|
77 |
+
* @since 1.0.0
|
78 |
+
* @access public
|
79 |
+
* @var array
|
80 |
+
*/
|
81 |
+
public $args = array();
|
82 |
+
|
83 |
+
/**
|
84 |
+
* Image arguments array filled by the class. This is used to store data about the image (src,
|
85 |
+
* width, height, etc.). In some scenarios, it may not be set, particularly when getting the
|
86 |
+
* raw image HTML.
|
87 |
+
*
|
88 |
+
* @since 1.0.0
|
89 |
+
* @access public
|
90 |
+
* @var array
|
91 |
+
*/
|
92 |
+
public $image_args = array();
|
93 |
+
|
94 |
+
/**
|
95 |
+
* The image HTML to output.
|
96 |
+
*
|
97 |
+
* @since 1.0.0
|
98 |
+
* @access public
|
99 |
+
* @var string
|
100 |
+
*/
|
101 |
+
public $image = '';
|
102 |
+
|
103 |
+
/**
|
104 |
+
* Original image HTML. This is set when splitting an image from the content. By default, this
|
105 |
+
* is only used when 'scan_raw' is set.
|
106 |
+
*
|
107 |
+
* @since 1.0.0
|
108 |
+
* @access public
|
109 |
+
* @var array
|
110 |
+
*/
|
111 |
+
public $original_image = '';
|
112 |
+
|
113 |
+
/**
|
114 |
+
* Constructor method. This sets up and runs the show.
|
115 |
+
*
|
116 |
+
* @since 1.0.0
|
117 |
+
* @access public
|
118 |
+
* @param array $args
|
119 |
+
* @return void
|
120 |
+
*/
|
121 |
+
public function __construct( $args = array() ) {
|
122 |
+
global $wp_embed;
|
123 |
+
|
124 |
+
/* Use WP's embed functionality to handle the [embed] shortcode and autoembeds. */
|
125 |
+
add_filter( 'get_the_image_post_content', array( $wp_embed, 'run_shortcode' ) );
|
126 |
+
add_filter( 'get_the_image_post_content', array( $wp_embed, 'autoembed' ) );
|
127 |
+
|
128 |
+
/* Set the default arguments. */
|
129 |
+
$defaults = array(
|
130 |
+
|
131 |
+
/* Post the image is associated with. */
|
132 |
+
'post_id' => get_the_ID(),
|
133 |
+
|
134 |
+
/* Method order (see methods below). */
|
135 |
+
'order' => array( 'meta_key', 'featured', 'attachment', 'scan', 'scan_raw', 'callback', 'default' ),
|
136 |
+
|
137 |
+
/* Methods of getting an image (in order). */
|
138 |
+
'meta_key' => array( 'Thumbnail', 'thumbnail' ), // array|string
|
139 |
+
'featured' => true,
|
140 |
+
'attachment' => true,
|
141 |
+
'scan' => false,
|
142 |
+
'scan_raw' => false, // Note: don't use the array format option with this.
|
143 |
+
'callback' => null,
|
144 |
+
'default' => false,
|
145 |
+
|
146 |
+
/* Split image from post content (by default, only used with the 'scan_raw' option). */
|
147 |
+
'split_content' => false,
|
148 |
+
|
149 |
+
/* Attachment-specific arguments. */
|
150 |
+
'size' => has_image_size( 'post-thumbnail' ) ? 'post-thumbnail' : 'thumbnail',
|
151 |
+
|
152 |
+
/* Format/display of image. */
|
153 |
+
'link_to_post' => true,
|
154 |
+
'image_class' => false,
|
155 |
+
'width' => false,
|
156 |
+
'height' => false,
|
157 |
+
'before' => '',
|
158 |
+
'after' => '',
|
159 |
+
|
160 |
+
/* Minimum allowed sizes. */
|
161 |
+
'min_width' => 0,
|
162 |
+
'min_height' => 0,
|
163 |
+
|
164 |
+
/* Captions. */
|
165 |
+
'caption' => false, // Default WP [caption] requires a width.
|
166 |
+
|
167 |
+
/* Saving the image. */
|
168 |
+
'meta_key_save' => false, // Save as metadata (string).
|
169 |
+
'thumbnail_id_save' => false, // Set 'featured image'.
|
170 |
+
'cache' => true, // Cache the image.
|
171 |
+
|
172 |
+
/* Return/echo image. */
|
173 |
+
'format' => 'img',
|
174 |
+
'echo' => true,
|
175 |
+
|
176 |
+
/* Deprecated arguments. */
|
177 |
+
'custom_key' => null, // @deprecated 0.6.0 Use 'meta_key'.
|
178 |
+
'default_size' => null, // @deprecated 0.5.0 Use 'size'.
|
179 |
+
'the_post_thumbnail' => null, // @deprecated 1.0.0 Use 'featured'.
|
180 |
+
'image_scan' => null, // @deprecated 1.0.0 Use 'scan' or 'scan_raw'.
|
181 |
+
'default_image' => null, // @deprecated 1.0.0 Use 'default'.
|
182 |
+
'order_of_image' => null, // @deprecated 1.0.0 No replacement.
|
183 |
+
);
|
184 |
|
185 |
+
/* Allow plugins/themes to filter the arguments. */
|
186 |
+
$this->args = apply_filters(
|
187 |
+
'get_the_image_args',
|
188 |
+
wp_parse_args( $args, $defaults )
|
189 |
+
);
|
190 |
|
191 |
+
/* If no post ID, return. */
|
192 |
+
if ( empty( $this->args['post_id'] ) )
|
193 |
+
return false;
|
194 |
|
195 |
+
/* === Handle deprecated arguments. === */
|
|
|
196 |
|
197 |
+
/* If $default_size is given, overwrite $size. */
|
198 |
+
if ( !is_null( $this->args['default_size'] ) )
|
199 |
+
$this->args['size'] = $this->args['default_size'];
|
200 |
|
201 |
+
/* If $custom_key is set, overwrite $meta_key. */
|
202 |
+
if ( !is_null( $this->args['custom_key'] ) )
|
203 |
+
$this->args['meta_key'] = $this->args['custom_key'];
|
204 |
|
205 |
+
/* If 'the_post_thumbnail' is set, overwrite 'featured'. */
|
206 |
+
if ( !is_null( $this->args['the_post_thumbnail'] ) )
|
207 |
+
$this->args['featured'] = $this->args['the_post_thumbnail'];
|
208 |
|
209 |
+
/* If 'image_scan' is set, overwrite 'scan'. */
|
210 |
+
if ( !is_null( $this->args['image_scan'] ) )
|
211 |
+
$this->args['scan'] = $this->args['image_scan'];
|
212 |
|
213 |
+
/* If 'default_image' is set, overwrite 'default'. */
|
214 |
+
if ( !is_null( $this->args['default_image'] ) )
|
215 |
+
$this->args['default'] = $this->args['default_image'];
|
216 |
|
217 |
+
/* === End deprecated arguments. === */
|
|
|
218 |
|
219 |
+
/* If $format is set to 'array', don't link to the post. */
|
220 |
+
if ( 'array' == $this->args['format'] )
|
221 |
+
$this->args['link_to_post'] = false;
|
222 |
|
223 |
+
/* Find images. */
|
224 |
+
$this->find();
|
225 |
|
226 |
+
/* Only used if $original_image is set. */
|
227 |
+
if ( true === $this->args['split_content'] && !empty( $this->original_image ) )
|
228 |
+
add_filter( 'the_content', array( $this, 'split_content' ), 15 );
|
229 |
+
}
|
230 |
|
231 |
+
/**
|
232 |
+
* Returns the image HTML or image array.
|
233 |
+
*
|
234 |
+
* @since 1.0.0
|
235 |
+
* @access public
|
236 |
+
* @return void
|
237 |
+
*/
|
238 |
+
public function get_image() {
|
239 |
|
240 |
+
/* Allow plugins/theme to override the final output. */
|
241 |
+
$image_html = apply_filters( 'get_the_image', $this->image );
|
|
|
242 |
|
243 |
+
/* If $format is set to 'array', return an array of image attributes. */
|
244 |
+
if ( 'array' === $this->args['format'] ) {
|
|
|
245 |
|
246 |
+
/* Set up a default empty array. */
|
247 |
+
$out = array();
|
|
|
248 |
|
249 |
+
/* Get the image attributes. */
|
250 |
+
$atts = wp_kses_hair( $image_html, array( 'http', 'https' ) );
|
|
|
251 |
|
252 |
+
/* Loop through the image attributes and add them in key/value pairs for the return array. */
|
253 |
+
foreach ( $atts as $att )
|
254 |
+
$out[ $att['name'] ] = $att['value'];
|
255 |
|
256 |
+
/* Return the array of attributes. */
|
257 |
+
return $out;
|
258 |
+
}
|
259 |
|
260 |
+
/* Or, if $echo is set to false, return the formatted image. */
|
261 |
+
elseif ( false === $this->args['echo'] ) {
|
262 |
+
return !empty( $image_html ) ? $this->args['before'] . $image_html . $this->args['after'] : $image_html;
|
263 |
+
}
|
264 |
|
265 |
+
/* If there is a $post_thumbnail_id, do the actions associated with get_the_post_thumbnail(). */
|
266 |
+
if ( isset( $this->image_args['post_thumbnail_id'] ) )
|
267 |
+
do_action( 'begin_fetch_post_thumbnail_html', $this->args['post_id'], $this->image_args['post_thumbnail_id'], $this->args['size'] );
|
268 |
|
269 |
+
/* Display the image if we get to this point. */
|
270 |
+
echo !empty( $image_html ) ? $this->args['before'] . $image_html . $this->args['after'] : $image_html;
|
271 |
|
272 |
+
/* If there is a $post_thumbnail_id, do the actions associated with get_the_post_thumbnail(). */
|
273 |
+
if ( isset( $this->image_args['post_thumbnail_id'] ) )
|
274 |
+
do_action( 'end_fetch_post_thumbnail_html', $this->args['post_id'], $this->image_args['post_thumbnail_id'], $this->args['size'] );
|
|
|
275 |
}
|
276 |
|
277 |
+
/**
|
278 |
+
* Figures out if we have an image related to the post. Runs through the various methods of getting
|
279 |
+
* an image. If there's a cached image, we'll just use that.
|
280 |
+
*
|
281 |
+
* @since 1.0.0
|
282 |
+
* @access public
|
283 |
+
* @return void
|
284 |
+
*/
|
285 |
+
public function find() {
|
286 |
|
287 |
+
/* Get cache key based on $this->args. */
|
288 |
+
$key = md5( serialize( compact( array_keys( $this->args ) ) ) );
|
289 |
|
290 |
+
/* Check for a cached image. */
|
291 |
+
$image_cache = wp_cache_get( $this->args['post_id'], 'get_the_image' );
|
292 |
|
293 |
+
if ( !is_array( $image_cache ) )
|
294 |
+
$image_cache = array();
|
295 |
|
296 |
+
/* If there is no cached image, let's see if one exists. */
|
297 |
+
if ( !isset( $image_cache[ $key ] ) || empty( $cache ) ) {
|
298 |
|
299 |
+
foreach ( $this->args['order'] as $method ) {
|
|
|
|
|
300 |
|
301 |
+
if ( !empty( $this->image ) || !empty( $this->image_args ) )
|
302 |
+
break;
|
303 |
|
304 |
+
if ( 'meta_key' === $method && !empty( $this->args['meta_key'] ) )
|
305 |
+
$this->get_meta_key_image();
|
|
|
306 |
|
307 |
+
elseif ( 'featured' === $method && true === $this->args['featured'] )
|
308 |
+
$this->get_featured_image();
|
|
|
|
|
309 |
|
310 |
+
elseif ( 'attachment' === $method && true === $this->args['attachment'] )
|
311 |
+
$this->get_attachment_image();
|
|
|
312 |
|
313 |
+
elseif ( 'scan' === $method && true === $this->args['scan'] )
|
314 |
+
$this->get_scan_image();
|
315 |
|
316 |
+
elseif ( 'scan_raw' === $method && true === $this->args['scan_raw'])
|
317 |
+
$this->get_scan_raw_image();
|
|
|
|
|
318 |
|
319 |
+
elseif ( 'callback' === $method && !is_null( $this->args['callback'] ) )
|
320 |
+
$this->get_callback_image();
|
321 |
|
322 |
+
elseif ( 'default' === $method && !empty( $this->args['default'] ) )
|
323 |
+
$this->get_default_image();
|
324 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
325 |
|
326 |
+
/* Format the image HTML. */
|
327 |
+
if ( empty( $this->image ) && !empty( $this->image_args ) )
|
328 |
+
$this->format_image();
|
329 |
+
|
330 |
+
/* If we have image HTML. */
|
331 |
+
if ( !empty( $this->image ) ) {
|
332 |
|
333 |
+
/* Save the image as metadata. */
|
334 |
+
if ( !empty( $this->args['meta_key_save'] ) )
|
335 |
+
$this->meta_key_save();
|
336 |
|
337 |
+
/* Set the image cache for the specific post. */
|
338 |
+
$image_cache[ $key ] = $this->image;
|
339 |
+
wp_cache_set( $this->args['post_id'], $image_cache, 'get_the_image' );
|
340 |
+
}
|
341 |
+
}
|
342 |
|
343 |
+
/* If an image was already cached for the post and arguments, use it. */
|
344 |
+
else {
|
345 |
+
$this->image = $image_cache[ $key ];
|
346 |
+
}
|
347 |
}
|
348 |
|
349 |
+
/**
|
350 |
+
* Gets a image by post meta key.
|
351 |
+
*
|
352 |
+
* @since 1.0.0
|
353 |
+
* @access public
|
354 |
+
* @return void
|
355 |
+
*/
|
356 |
+
public function get_meta_key_image() {
|
357 |
|
358 |
+
/* If $meta_key is not an array. */
|
359 |
+
if ( !is_array( $this->args['meta_key'] ) )
|
360 |
+
$this->args['meta_key'] = array( $this->args['meta_key'] );
|
361 |
|
362 |
+
/* Loop through each of the given meta keys. */
|
363 |
+
foreach ( $this->args['meta_key'] as $meta_key ) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
364 |
|
365 |
+
/* Get the image URL by the current meta key in the loop. */
|
366 |
+
$image = get_post_meta( $this->args['post_id'], $meta_key, true );
|
367 |
|
368 |
+
/* If an image was found, break out of the loop. */
|
369 |
+
if ( !empty( $image ) )
|
370 |
+
break;
|
371 |
+
}
|
372 |
|
373 |
+
/* If there's an image and it is numeric, assume it is an attachment ID. */
|
374 |
+
if ( !empty( $image ) && is_numeric( $image ) )
|
375 |
+
$this->_get_image_attachment( absint( $image ) );
|
376 |
|
377 |
+
/* Else, assume the image is a file URL. */
|
378 |
+
elseif ( !empty( $image ) )
|
379 |
+
$this->image_args = array( 'src' => $image );
|
380 |
+
}
|
381 |
|
382 |
+
/**
|
383 |
+
* Gets the featured image (i.e., WP's post thumbnail).
|
384 |
+
*
|
385 |
+
* @since 1.0.0
|
386 |
+
* @access public
|
387 |
+
* @return void
|
388 |
+
*/
|
389 |
+
public function get_featured_image() {
|
390 |
|
391 |
+
/* Check for a post image ID (set by WP as a custom field). */
|
392 |
+
$post_thumbnail_id = get_post_thumbnail_id( $this->args['post_id'] );
|
393 |
|
394 |
+
/* If no post image ID is found, return. */
|
395 |
+
if ( empty( $post_thumbnail_id ) )
|
396 |
+
return;
|
397 |
|
398 |
+
/* Apply filters on post_thumbnail_size because this is a default WP filter used with its image feature. */
|
399 |
+
$this->args['size'] = apply_filters( 'post_thumbnail_size', $this->args['size'] );
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
400 |
|
401 |
+
/* Set the image args. */
|
402 |
+
$this->_get_image_attachment( $post_thumbnail_id );
|
403 |
|
404 |
+
/* Add the post thumbnail ID. */
|
405 |
+
$this->image_args['post_thumbnail_id'] = $post_thumbnail_id;
|
|
|
406 |
}
|
407 |
|
408 |
+
/**
|
409 |
+
* Gets the first image attached to the post. If the post itself is an attachment image, that will
|
410 |
+
* be the image used. This method also works with sub-attachments (images for audio/video attachments
|
411 |
+
* are a good example).
|
412 |
+
*
|
413 |
+
* @since 1.0.0
|
414 |
+
* @access public
|
415 |
+
* @return void
|
416 |
+
*/
|
417 |
+
public function get_attachment_image() {
|
418 |
+
|
419 |
+
/* Check if the post itself is an image attachment. */
|
420 |
+
if ( wp_attachment_is_image( $this->args['post_id'] ) ) {
|
421 |
+
$attachment_id = $this->args['post_id'];
|
422 |
+
}
|
423 |
+
|
424 |
+
/* If the post is not an image attachment, check if it has any image attachments. */
|
425 |
+
else {
|
426 |
+
|
427 |
+
/* Get attachments for the inputted $post_id. */
|
428 |
+
$attachments = get_children(
|
429 |
+
array(
|
430 |
+
'numberposts' => 1,
|
431 |
+
'post_parent' => $this->args['post_id'],
|
432 |
+
'post_status' => 'inherit',
|
433 |
+
'post_type' => 'attachment',
|
434 |
+
'post_mime_type' => 'image',
|
435 |
+
'order' => 'ASC',
|
436 |
+
'orderby' => 'menu_order ID',
|
437 |
+
'fields' => 'ids'
|
438 |
+
)
|
439 |
+
);
|
440 |
+
|
441 |
+
/* Check if any attachments were found. */
|
442 |
+
if ( !empty( $attachments ) )
|
443 |
+
$attachment_id = array_shift( $attachments );
|
444 |
+
}
|
445 |
|
446 |
+
if ( !empty( $attachment_id ) )
|
447 |
+
$this->_get_image_attachment( $attachment_id );
|
448 |
+
}
|
449 |
|
450 |
+
/**
|
451 |
+
* Scans the post content for an image. It first scans and checks for an image with the
|
452 |
+
* "wp-image-xxx" ID. If that exists, it'll grab the actual image attachment. If not, it looks
|
453 |
+
* for the image source.
|
454 |
+
*
|
455 |
+
* @since 1.0.0
|
456 |
+
* @access public
|
457 |
+
* @return void
|
458 |
+
*/
|
459 |
+
public function get_scan_image() {
|
460 |
|
461 |
+
/* Get the post content. */
|
462 |
+
$post_content = get_post_field( 'post_content', $this->args['post_id'] );
|
463 |
|
464 |
+
/* Apply filters to content. */
|
465 |
+
$post_content = apply_filters( 'get_the_image_post_content', $post_content );
|
466 |
|
467 |
+
/* Check the content for `id="wp-image-%d"`. */
|
468 |
+
preg_match( '/id=[\'"]wp-image-([\d]*)[\'"]/i', $post_content, $image_ids );
|
469 |
+
|
470 |
+
/* Loop through any found image IDs. */
|
471 |
+
if ( is_array( $image_ids ) ) {
|
472 |
+
|
473 |
+
foreach ( $image_ids as $image_id ) {
|
474 |
+
$this->_get_image_attachment( $image_id );
|
475 |
+
|
476 |
+
if ( !empty( $this->image_args ) )
|
477 |
+
return;
|
478 |
+
}
|
479 |
+
}
|
480 |
+
|
481 |
+
/* Search the post's content for the <img /> tag and get its URL. */
|
482 |
+
preg_match_all( '|<img.*?src=[\'"](.*?)[\'"].*?>|i', $post_content, $matches );
|
483 |
+
|
484 |
+
/* If there is a match for the image, set the image args. */
|
485 |
+
if ( isset( $matches ) && !empty( $matches[1][0] ) )
|
486 |
+
$this->image_args = array( 'src' => $matches[1][0] );
|
487 |
+
}
|
488 |
+
|
489 |
+
/**
|
490 |
+
* Scans the post content for a complete image. This method will attempt to grab the complete
|
491 |
+
* HTML for an image. If an image is found, pretty much all arguments passed in may be ignored
|
492 |
+
* in favor of getting the actual image used in the post content. It works with both captions
|
493 |
+
* and linked images. However, it can't account for all possible HTML wrappers for images used
|
494 |
+
* in all setups.
|
495 |
+
*
|
496 |
+
* This method was created for use with the WordPress "image" post format where theme authors
|
497 |
+
* might want to pull the whole image from the content as the user added it. It's also meant
|
498 |
+
* to be used (not required) with the `split_content` option.
|
499 |
+
*
|
500 |
+
* Note: This option should not be used if returning the image as an array. If that's desired,
|
501 |
+
* use the `scan` option instead.
|
502 |
+
*
|
503 |
+
* @since 1.0.0
|
504 |
+
* @access public
|
505 |
+
* @return void
|
506 |
+
*/
|
507 |
+
public function get_scan_raw_image() {
|
508 |
+
|
509 |
+
/* Get the post content. */
|
510 |
+
$post_content = get_post_field( 'post_content', $this->args['post_id'] );
|
511 |
+
|
512 |
+
/* Apply filters to content. */
|
513 |
+
$post_content = apply_filters( 'get_the_image_post_content', $post_content );
|
514 |
+
|
515 |
+
/* Finds matches for shortcodes in the content. */
|
516 |
+
preg_match_all( '/' . get_shortcode_regex() . '/s', $post_content, $matches, PREG_SET_ORDER );
|
517 |
+
|
518 |
+
if ( !empty( $matches ) ) {
|
519 |
+
|
520 |
+
foreach ( $matches as $shortcode ) {
|
521 |
+
|
522 |
+
if ( in_array( $shortcode[2], array( 'caption', 'wp_caption' ) ) ) {
|
523 |
+
|
524 |
+
preg_match( '#id=[\'"]attachment_([\d]*)[\'"]|class=[\'"].*?wp-image-([\d]*).*?[\'"]#i', $shortcode[0], $matches );
|
525 |
+
|
526 |
+
if ( !empty( $matches ) && isset( $matches[1] ) || isset( $matches[2] ) ) {
|
527 |
+
|
528 |
+
$attachment_id = !empty( $matches[1] ) ? absint( $matches[1] ) : absint( $matches[2] );
|
529 |
+
|
530 |
+
$image_src = wp_get_attachment_image_src( $attachment_id, $this->args['size'] );
|
531 |
+
|
532 |
+
if ( !empty( $image_src ) ) {
|
533 |
+
|
534 |
+
/* old-style captions. */
|
535 |
+
if ( preg_match( '#.*?[\s]caption=[\'"](.+?)[\'"]#i', $shortcode[0], $caption_matches ) )
|
536 |
+
$image_caption = trim( $caption_matches[1] );
|
537 |
+
|
538 |
+
$caption_args = array(
|
539 |
+
'width' => $image_src[1],
|
540 |
+
'align' => 'center'
|
541 |
+
);
|
542 |
+
|
543 |
+
if ( !empty( $image_caption ) )
|
544 |
+
$caption_args['caption'] = $image_caption;
|
545 |
+
|
546 |
+
/* Set up the patterns for the 'src', 'width', and 'height' attributes. */
|
547 |
+
$patterns = array(
|
548 |
+
'/(src=[\'"]).+?([\'"])/i',
|
549 |
+
'/(width=[\'"]).+?([\'"])/i',
|
550 |
+
'/(height=[\'"]).+?([\'"])/i',
|
551 |
+
);
|
552 |
+
|
553 |
+
/* Set up the replacements for the 'src', 'width', and 'height' attributes. */
|
554 |
+
$replacements = array(
|
555 |
+
'${1}' . $image_src[0] . '${2}',
|
556 |
+
'${1}' . $image_src[1] . '${2}',
|
557 |
+
'${1}' . $image_src[2] . '${2}',
|
558 |
+
);
|
559 |
+
|
560 |
+
/* Filter the image attributes. */
|
561 |
+
$shortcode_content = preg_replace( $patterns, $replacements, $shortcode[5] );
|
562 |
+
|
563 |
+
$this->image = img_caption_shortcode( $caption_args, $shortcode_content );
|
564 |
+
$this->original_image = $shortcode[0];
|
565 |
+
return;
|
566 |
+
}
|
567 |
+
else {
|
568 |
+
$this->image = do_shortcode( $shortcode[0] );
|
569 |
+
$this->original_image = $shortcode[0];
|
570 |
+
return;
|
571 |
+
}
|
572 |
+
}
|
573 |
+
}
|
574 |
}
|
575 |
}
|
576 |
+
|
577 |
+
/* Pull a raw HTML image + link if it exists. */
|
578 |
+
if ( preg_match( '#((?:<a [^>]+>\s*)?<img [^>]+>(?:\s*</a>)?)#is', $post_content, $matches ) )
|
579 |
+
$this->image = $this->original_image = $matches[0];
|
580 |
}
|
581 |
|
582 |
+
/**
|
583 |
+
* Allows developers to create a custom callback function. If the `callback` argument is set, theme
|
584 |
+
* developers are expected to **always** return an array. Even if nothing is found, return an empty
|
585 |
+
* array.
|
586 |
+
*
|
587 |
+
* @since 1.0.0
|
588 |
+
* @access public
|
589 |
+
* @return void
|
590 |
+
*/
|
591 |
+
public function get_callback_image() {
|
592 |
+
$this->image_args = call_user_func( $this->args['callback'], $this->args );
|
593 |
+
}
|
594 |
+
|
595 |
+
/**
|
596 |
+
* Sets the default image.
|
597 |
+
*
|
598 |
+
* @since 1.0.0
|
599 |
+
* @access public
|
600 |
+
* @return void
|
601 |
+
*/
|
602 |
+
public function get_default_image() {
|
603 |
+
$this->image_args = array( 'src' => $this->args['default'] );
|
604 |
+
}
|
605 |
+
|
606 |
+
/**
|
607 |
+
* Handles an image attachment. Other methods rely on this method for getting the image data since
|
608 |
+
* most images are actually attachments.
|
609 |
+
*
|
610 |
+
* @since 1.0.0
|
611 |
+
* @access public
|
612 |
+
* @param int $attachment_id
|
613 |
+
* @return void
|
614 |
+
*/
|
615 |
+
public function _get_image_attachment( $attachment_id ) {
|
616 |
|
617 |
/* Get the attachment image. */
|
618 |
+
$image = wp_get_attachment_image_src( $attachment_id, $this->args['size'] );
|
619 |
|
620 |
/* Get the attachment alt text. */
|
621 |
$alt = trim( strip_tags( get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ) ) );
|
624 |
$caption = get_post_field( 'post_excerpt', $attachment_id );
|
625 |
|
626 |
/* Save the attachment as the 'featured image'. */
|
627 |
+
if ( true === $this->args['thumbnail_id_save'] )
|
628 |
+
$this->thumbnail_id_save( $attachment_id );
|
629 |
+
|
630 |
+
/* Set the image args. */
|
631 |
+
$this->image_args = array(
|
632 |
+
'src' => $image[0],
|
633 |
+
'width' => $image[1],
|
634 |
+
'height' => $image[2],
|
635 |
+
'alt' => $alt,
|
636 |
+
'caption' => $caption
|
637 |
+
);
|
638 |
}
|
639 |
|
640 |
+
/**
|
641 |
+
* Formats the image HTML. This method is only called if the `$image` property isn't set. It uses
|
642 |
+
* the `$image_args` property to set up the image.
|
643 |
+
*
|
644 |
+
* @since 1.0.0
|
645 |
+
* @access public
|
646 |
+
* @return void
|
647 |
+
*/
|
648 |
+
public function format_image() {
|
649 |
|
650 |
+
/* If there is no image URL, return false. */
|
651 |
+
if ( empty( $this->image_args['src'] ) )
|
652 |
+
return;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
653 |
|
654 |
+
/* Check against min. width. If the image width is too small return. */
|
655 |
+
if ( 0 < $this->args['min_width'] && isset( $this->image_args['width'] ) && $this->image_args['width'] < $this->args['min_width'] )
|
656 |
+
return;
|
657 |
|
658 |
+
/* Check against min. height. If the image height is too small return. */
|
659 |
+
if ( 0 < $this->args['min_height'] && isset( $this->image_args['height'] ) && $this->image_args['height'] < $this->args['min_height'] )
|
660 |
+
return;
|
661 |
|
662 |
+
/* Empty classes array. */
|
663 |
+
$classes = array();
|
664 |
|
665 |
+
/* If there is alt text, set it. Otherwise, default to the post title. */
|
666 |
+
$image_alt = !empty( $this->image_args['alt'] ) ? $this->image_args['alt'] : get_post_field( 'post_title', $this->args['post_id'] );
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
667 |
|
668 |
+
/* If there's a width/height for the image. */
|
669 |
+
if ( isset( $this->image_args['width'] ) && isset( $this->image_args['height'] ) ) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
670 |
|
671 |
+
/* Set a class based on the orientation. */
|
672 |
+
$classes[] = ( $this->image_args['height'] > $this->image_args['width'] ) ? 'portrait' : 'landscape';
|
|
|
673 |
|
674 |
+
/* If an explicit width/height is not set, use the info from the image. */
|
675 |
+
if ( empty( $this->args['width'] ) && empty( $this->args['height'] ) ) {
|
676 |
+
$this->args['width'] = $this->image_args['width'];
|
677 |
+
$this->args['height'] = $this->image_args['height'];
|
678 |
+
}
|
679 |
+
}
|
680 |
|
681 |
+
/* If there is a width or height, set them as HMTL-ready attributes. */
|
682 |
+
$width = $this->args['width'] ? ' width="' . esc_attr( $this->args['width'] ) . '"' : '';
|
683 |
+
$height = $this->args['height'] ? ' height="' . esc_attr( $this->args['height'] ) . '"' : '';
|
684 |
|
685 |
+
/* Add the meta key(s) to the classes array. */
|
686 |
+
if ( !empty( $this->args['meta_key'] ) )
|
687 |
+
$classes = array_merge( $classes, (array)$this->args['meta_key'] );
|
688 |
|
689 |
+
/* Add the $size to the class. */
|
690 |
+
$classes[] = $this->args['size'];
|
|
|
|
|
|
|
691 |
|
692 |
+
/* Get the custom image class. */
|
693 |
+
if ( !empty( $this->args['image_class'] ) ) {
|
694 |
|
695 |
+
if ( !is_array( $this->args['image_class'] ) )
|
696 |
+
$this->args['image_class'] = preg_split( '#\s+#', $this->args['image_class'] );
|
|
|
|
|
|
|
|
|
697 |
|
698 |
+
$classes = array_merge( $classes, $this->args['image_class'] );
|
699 |
+
}
|
700 |
|
701 |
+
/* Sanitize all the classes. */
|
702 |
+
$classes = $this->sanitize_class( $classes );
|
703 |
|
704 |
+
/* Join all the classes into a single string and make sure there are no duplicates. */
|
705 |
+
$class = join( ' ', $classes );
|
706 |
|
707 |
+
/* Add the image attributes to the <img /> element. */
|
708 |
+
$html = sprintf( '<img src="%s" alt="%s" class="%s"%s itemprop="image" />', esc_attr( $this->image_args['src'] ), esc_attr( strip_tags( $image_alt ) ), $class, $width . $height );
|
|
|
709 |
|
710 |
+
/* If $link_to_post is set to true, link the image to its post. */
|
711 |
+
if ( $this->args['link_to_post'] )
|
712 |
+
$html = '<a href="' . get_permalink( $this->args['post_id'] ) . '" title="' . esc_attr( get_post_field( 'post_title', $this->args['post_id'] ) ) . '">' . $html . '</a>';
|
713 |
|
714 |
+
/* If there is a $post_thumbnail_id, apply the WP filters normally associated with get_the_post_thumbnail(). */
|
715 |
+
if ( !empty( $this->image_args['post_thumbnail_id'] ) )
|
716 |
+
$html = apply_filters( 'post_thumbnail_html', $html, $this->args['post_id'], $this->image_args['post_thumbnail_id'], $this->args['size'], '' );
|
717 |
|
718 |
+
/* If we're showing a caption. */
|
719 |
+
if ( true === $this->args['caption'] && !empty( $this->image_args['caption'] ) )
|
720 |
+
$html = img_caption_shortcode( array( 'caption' => $this->image_args['caption'], 'width' => $this->args['width'] ), $html );
|
721 |
|
722 |
+
$this->image = $html;
|
723 |
+
}
|
724 |
+
|
725 |
+
/**
|
726 |
+
* Saves the image source as metadata. Saving the image as meta is actually quite a bit quicker
|
727 |
+
* if the user doesn't have a persistent caching plugin available. However, it doesn't play as
|
728 |
+
* nicely with custom image sizes used across multiple themes where one might want to resize images.
|
729 |
+
* This option should be reserved for advanced users only. Don't use in publicly-distributed
|
730 |
+
* themes.
|
731 |
+
*
|
732 |
+
* @since 1.0.0
|
733 |
+
* @access public
|
734 |
+
* @return void
|
735 |
+
*/
|
736 |
+
public function meta_key_save() {
|
737 |
+
|
738 |
+
/* If the $meta_key_save argument is empty or there is no image $url given, return. */
|
739 |
+
if ( empty( $this->args['meta_key_save'] ) || empty( $this->image_args['src'] ) )
|
740 |
+
return;
|
741 |
+
|
742 |
+
/* Get the current value of the meta key. */
|
743 |
+
$meta = get_post_meta( $this->args['post_id'], $this->args['meta_key_save'], true );
|
744 |
+
|
745 |
+
/* If there is no value for the meta key, set a new value with the image $url. */
|
746 |
+
if ( empty( $meta ) )
|
747 |
+
add_post_meta( $this->args['post_id'], $this->args['meta_key_save'], $this->image_args['src'] );
|
748 |
+
|
749 |
+
/* If the current value doesn't match the image $url, update it. */
|
750 |
+
elseif ( $meta !== $this->image_args['src'] )
|
751 |
+
update_post_meta( $this->args['post_id'], $this->args['meta_key_save'], $this->image_args['src'], $meta );
|
752 |
+
}
|
753 |
+
|
754 |
+
/**
|
755 |
+
* Saves the image attachment as the WordPress featured image. This is useful for setting the
|
756 |
+
* featured image for the post in the case that the user forgot to (win for client work!). It
|
757 |
+
* should not be used in publicly-distributed themes where you don't know how the user will be
|
758 |
+
* setting up their site.
|
759 |
+
*
|
760 |
+
* @since 1.0.0
|
761 |
+
* @access public
|
762 |
+
* @return void
|
763 |
+
*/
|
764 |
+
public function thumbnail_id_save( $attachment_id ) {
|
765 |
+
|
766 |
+
/* Save the attachment as the 'featured image'. */
|
767 |
+
if ( true === $this->args['thumbnail_id_save'] )
|
768 |
+
set_post_thumbnail( $this->args['post_id'], $attachment_id );
|
769 |
+
}
|
770 |
+
|
771 |
+
/**
|
772 |
+
* Sanitizes the image class.
|
773 |
+
*
|
774 |
+
* @since 1.0.0
|
775 |
+
* @access public
|
776 |
+
* @param array $classes
|
777 |
+
* @return array
|
778 |
+
*/
|
779 |
+
public function sanitize_class( $classes ) {
|
780 |
|
781 |
+
$classes = array_map( 'strtolower', $classes );
|
782 |
+
$classes = array_map( 'sanitize_html_class', $classes );
|
783 |
+
|
784 |
+
return array_unique( $classes );
|
785 |
+
}
|
786 |
|
787 |
+
/**
|
788 |
+
* Splits the original image HTML from the post content.
|
789 |
+
*
|
790 |
+
* @since 1.0.0
|
791 |
+
* @access public
|
792 |
+
* @param string $content
|
793 |
+
* @return string
|
794 |
+
*/
|
795 |
+
public function split_content( $content ) {
|
796 |
|
797 |
+
remove_filter( 'the_content', array( $this, 'split_content' ), 1 );
|
|
|
|
|
798 |
|
799 |
+
return str_replace( $this->original_image, '', $content );
|
800 |
+
}
|
|
|
801 |
}
|
802 |
|
803 |
/**
|
804 |
* Deletes the image cache for the specific post when the 'save_post' hook is fired.
|
805 |
*
|
806 |
+
* @since 0.7.0
|
807 |
* @access private
|
808 |
+
* @param int $post_id The ID of the post to delete the cache for.
|
809 |
* @return void
|
810 |
*/
|
811 |
function get_the_image_delete_cache_by_post( $post_id ) {
|
816 |
* Deletes the image cache for a specific post when the 'added_post_meta', 'deleted_post_meta',
|
817 |
* or 'updated_post_meta' hooks are called.
|
818 |
*
|
819 |
+
* @since 0.7.0
|
820 |
* @access private
|
821 |
+
* @param int $meta_id The ID of the metadata being updated.
|
822 |
+
* @param int $post_id The ID of the post to delete the cache for.
|
823 |
* @return void
|
824 |
*/
|
825 |
function get_the_image_delete_cache_by_meta( $meta_id, $post_id ) {
|
826 |
wp_cache_delete( $post_id, 'get_the_image' );
|
827 |
}
|
828 |
|
829 |
+
|
830 |
+
/* === Deprecated functions === */
|
831 |
+
|
832 |
+
|
833 |
/**
|
834 |
+
* @since 0.1.0
|
835 |
* @deprecated 0.3.0
|
836 |
+
* @access public
|
837 |
*/
|
838 |
+
function get_the_image_link() {
|
839 |
+
_deprecated_function( __FUNCTION__, '0.3.0', 'get_the_image' );
|
840 |
+
get_the_image( array( 'link_to_post' => true ) );
|
841 |
}
|
842 |
|
843 |
/**
|
844 |
+
* @since 0.3.0
|
845 |
* @deprecated 0.7.0
|
846 |
+
* @access private
|
847 |
*/
|
848 |
+
function image_by_custom_field() {}
|
|
|
|
|
849 |
|
850 |
/**
|
851 |
+
* @since 0.4.0
|
852 |
* @deprecated 0.7.0
|
853 |
+
* @access private
|
854 |
*/
|
855 |
+
function image_by_the_post_thumbnail() {}
|
|
|
|
|
856 |
|
857 |
/**
|
858 |
+
* @since 0.3.0
|
859 |
* @deprecated 0.7.0
|
860 |
+
* @access private
|
861 |
*/
|
862 |
+
function image_by_attachment() {}
|
|
|
|
|
863 |
|
864 |
/**
|
865 |
+
* @since 0.3.0
|
866 |
* @deprecated 0.7.0
|
867 |
+
* @access private
|
868 |
*/
|
869 |
+
function image_by_scan() {}
|
|
|
|
|
870 |
|
871 |
/**
|
872 |
+
* @since 0.3.0
|
873 |
* @deprecated 0.7.0
|
874 |
+
* @access private
|
875 |
*/
|
876 |
+
function image_by_default() {}
|
|
|
|
|
877 |
|
878 |
/**
|
879 |
+
* @since 0.1.0
|
880 |
* @deprecated 0.7.0
|
881 |
+
* @access private
|
882 |
*/
|
883 |
+
function display_the_image() {}
|
|
|
|
|
884 |
|
885 |
/**
|
886 |
+
* @since 0.5.0
|
887 |
+
* @deprecated 0.7.0
|
888 |
+
* @access private
|
889 |
*/
|
890 |
+
function get_the_image_delete_cache() {}
|
891 |
+
|
892 |
+
/**
|
893 |
+
* @since 0.7.0
|
894 |
+
* @deprecated 1.0.0
|
895 |
+
* @access private
|
896 |
+
*/
|
897 |
+
function get_the_image_by_meta_key() {}
|
898 |
+
|
899 |
+
/**
|
900 |
+
* @since 0.7.0
|
901 |
+
* @deprecated 1.0.0
|
902 |
+
* @access private
|
903 |
+
*/
|
904 |
+
function get_the_image_by_post_thumbnail() {}
|
905 |
+
|
906 |
+
/**
|
907 |
+
* @since 0.7.0
|
908 |
+
* @deprecated 1.0.0
|
909 |
+
* @access private
|
910 |
+
*/
|
911 |
+
function get_the_image_by_attachment() {}
|
912 |
+
|
913 |
+
/**
|
914 |
+
* @since 0.7.0
|
915 |
+
* @deprecated 1.0.0
|
916 |
+
* @access private
|
917 |
+
*/
|
918 |
+
function get_the_image_by_scan() {}
|
919 |
|
920 |
+
/**
|
921 |
+
* @since 0.7.0
|
922 |
+
* @deprecated 1.0.0
|
923 |
+
* @access private
|
924 |
+
*/
|
925 |
+
function get_the_image_by_default() {}
|
926 |
+
|
927 |
+
/**
|
928 |
+
* @since 0.7.0
|
929 |
+
* @deprecated 1.0.0
|
930 |
+
* @access private
|
931 |
+
*/
|
932 |
+
function get_the_image_format() {}
|
933 |
+
|
934 |
+
/**
|
935 |
+
* @since 0.6.0
|
936 |
+
* @deprecated 1.0.0
|
937 |
+
* @access private
|
938 |
+
*/
|
939 |
+
function get_the_image_meta_key_save() {}
|
readme.md
CHANGED
@@ -4,7 +4,7 @@ Get the Image is a plugin that grabs images for you. It was designed to make th
|
|
4 |
|
5 |
## What the plugin does ##
|
6 |
|
7 |
-
This plugin was made to easily get an image related to a post. This is the method order in which the plugin attempts to grab an image.
|
8 |
|
9 |
* Meta key (custom field).
|
10 |
* Post thumbnail (WP featured image).
|
@@ -33,17 +33,23 @@ The `get_the_image()` function accepts a single parameter of `$args`, which is a
|
|
33 |
/* Post the image is associated with. */
|
34 |
'post_id' => get_the_ID(),
|
35 |
|
|
|
|
|
|
|
36 |
/* Methods of getting an image (in order). */
|
37 |
'meta_key' => array( 'Thumbnail', 'thumbnail' ), // array|string
|
38 |
-
'
|
39 |
'attachment' => true,
|
40 |
-
'
|
|
|
41 |
'callback' => null,
|
42 |
-
'
|
|
|
|
|
|
|
43 |
|
44 |
/* Attachment-specific arguments. */
|
45 |
-
'size' =>
|
46 |
-
'order_of_image' => 1,
|
47 |
|
48 |
/* Format/display of image. */
|
49 |
'link_to_post' => true,
|
@@ -53,42 +59,55 @@ The `get_the_image()` function accepts a single parameter of `$args`, which is a
|
|
53 |
'before' => '',
|
54 |
'after' => '',
|
55 |
|
|
|
|
|
|
|
|
|
56 |
/* Captions. */
|
57 |
'caption' => false, // Default WP [caption] requires a width.
|
58 |
|
59 |
/* Saving the image. */
|
60 |
-
'meta_key_save' => false,
|
61 |
'thumbnail_id_save' => false, // Set 'featured image'.
|
62 |
-
'cache' => true,
|
63 |
|
64 |
/* Return/echo image. */
|
65 |
'format' => 'img',
|
66 |
'echo' => true,
|
67 |
|
68 |
/* Deprecated arguments. */
|
69 |
-
'custom_key' => null, // @deprecated 0.6. Use 'meta_key'.
|
70 |
-
'default_size' => null, // @deprecated 0.5.
|
|
|
|
|
|
|
|
|
71 |
);
|
72 |
|
73 |
* `post_id` - The ID of the post to get the image for. This defaults to the current post in the loop.
|
|
|
74 |
* `meta_key` - This parameter refers to post meta keys (custom fields) that you use. Remember, meta keys are case-sensitive (defaults are `Thumbnail` and `thumbnail`). By default, this is an array of meta keys, but it can also be a string for a single meta key.
|
|
|
75 |
* `attachment` - The script will look for images attached to the post (set to `true` by default).
|
76 |
-
* `
|
77 |
-
* `
|
78 |
-
* `
|
79 |
-
* `
|
|
|
|
|
80 |
* `link_to_post` - Whether the image shown should be linked to the post (set to `true` by default).
|
81 |
* `image_class` - You can give an additional class to the image for use in your CSS.
|
82 |
-
* `image_scan` - If set to `true`, the script will search within your post for an image that's been added.
|
83 |
* `width` - Set the width of the image on output.
|
84 |
* `height` - Set the height of the image on output.
|
85 |
-
* `
|
|
|
|
|
|
|
|
|
86 |
* `meta_key_save` - A meta key to save the image URL 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 `false`.
|
87 |
* `thumbnail_id_save` - 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 `false`
|
88 |
-
* `callback` - 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 `default_image` is set. The `$args` array is passed to the callback function as the only parameter.
|
89 |
* `cache` - Whether to use the WordPress Cache API (integrates with caching plugins) to serve the post images. By default, this is set to `true`.
|
90 |
-
* `
|
91 |
-
* `after` - HTML to place after the output of the image.
|
92 |
* `echo` - If set to `true`, the image is shown on the page. If set to `false`, the image will be returned to use in your own function. (Set to `true` by default.)
|
93 |
|
94 |
### Some usage examples ##
|
@@ -119,7 +138,7 @@ If you want to have a sort of fallback image, then you can set an image for the
|
|
119 |
|
120 |
You can even make the script scan for images that have been added to your post with this:
|
121 |
|
122 |
-
<?php get_the_image( array( '
|
123 |
|
124 |
#### Example 5 ####
|
125 |
|
@@ -186,15 +205,38 @@ You will still have the `size` and `meta_key` classes plus your additional class
|
|
186 |
|
187 |
## Changelog ##
|
188 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
189 |
### Version 0.9.0 ###
|
190 |
|
191 |
-
#### Enhancements:
|
192 |
|
193 |
* Caption support. FTW!
|
194 |
* Multiple image classes now allowed via the `image_class` argument.
|
195 |
* Use current theme's `post-thumbnail` as default image size if set via `set_post_thumbnail_size()`.
|
196 |
|
197 |
-
#### Bug fixes:
|
198 |
|
199 |
* Use the WordPress-saved attachment alt text for the image.
|
200 |
* Only add `$out['src']` if `$out['url']` is set when returning as an array.
|
@@ -284,7 +326,7 @@ You will still have the `size` and `meta_key` classes plus your additional class
|
|
284 |
|
285 |
## Support ##
|
286 |
|
287 |
-
I run a WordPress community called [Theme Hybrid](http://themehybrid.com), 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
|
288 |
|
289 |
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.
|
290 |
|
@@ -292,12 +334,4 @@ I know. I know. You might not want to pay for support, but just consider it a
|
|
292 |
|
293 |
Get the Image is licensed under the [GNU GPL](http://www.gnu.org/licenses/old-licenses/gpl-2.0.html), version 2 or later.
|
294 |
|
295 |
-
2008 – 
|
296 |
-
|
297 |
-
*[API]: Application Programming Interface
|
298 |
-
*[CSS]: Cascading Style Sheets
|
299 |
-
*[FTW]: For The Win
|
300 |
-
*[GPL]: General Public License
|
301 |
-
*[HTML]: Hypertext Markup Language
|
302 |
-
*[URL]: Uniform Resource Locator
|
303 |
-
*[USD]: United States Dollars
|
4 |
|
5 |
## What the plugin does ##
|
6 |
|
7 |
+
This plugin was made to easily get an image related to a post. This is the default method order in which the plugin attempts to grab an image.
|
8 |
|
9 |
* Meta key (custom field).
|
10 |
* Post thumbnail (WP featured image).
|
33 |
/* Post the image is associated with. */
|
34 |
'post_id' => get_the_ID(),
|
35 |
|
36 |
+
/* Method order (see methods below). */
|
37 |
+
'order' => array( 'meta_key', 'featured', 'attachment', 'scan', 'scan_raw', 'callback', 'default' ),
|
38 |
+
|
39 |
/* Methods of getting an image (in order). */
|
40 |
'meta_key' => array( 'Thumbnail', 'thumbnail' ), // array|string
|
41 |
+
'featured' => true,
|
42 |
'attachment' => true,
|
43 |
+
'scan' => false,
|
44 |
+
'scan_raw' => false, // Note: don't use the array format option with this.
|
45 |
'callback' => null,
|
46 |
+
'default' => false,
|
47 |
+
|
48 |
+
/* Split image from post content (by default, only used with the 'scan_raw' option). */
|
49 |
+
'split_content' => false,
|
50 |
|
51 |
/* Attachment-specific arguments. */
|
52 |
+
'size' => has_image_size( 'post-thumbnail' ) ? 'post-thumbnail' : 'thumbnail',
|
|
|
53 |
|
54 |
/* Format/display of image. */
|
55 |
'link_to_post' => true,
|
59 |
'before' => '',
|
60 |
'after' => '',
|
61 |
|
62 |
+
/* Minimum allowed sizes. */
|
63 |
+
'min_width' => 0,
|
64 |
+
'min_height' => 0,
|
65 |
+
|
66 |
/* Captions. */
|
67 |
'caption' => false, // Default WP [caption] requires a width.
|
68 |
|
69 |
/* Saving the image. */
|
70 |
+
'meta_key_save' => false, // Save as metadata (string).
|
71 |
'thumbnail_id_save' => false, // Set 'featured image'.
|
72 |
+
'cache' => true, // Cache the image.
|
73 |
|
74 |
/* Return/echo image. */
|
75 |
'format' => 'img',
|
76 |
'echo' => true,
|
77 |
|
78 |
/* Deprecated arguments. */
|
79 |
+
'custom_key' => null, // @deprecated 0.6.0 Use 'meta_key'.
|
80 |
+
'default_size' => null, // @deprecated 0.5.0 Use 'size'.
|
81 |
+
'the_post_thumbnail' => null, // @deprecated 1.0.0 Use 'featured'.
|
82 |
+
'image_scan' => null, // @deprecated 1.0.0 Use 'scan' or 'scan_raw'.
|
83 |
+
'default_image' => null, // @deprecated 1.0.0 Use 'default'.
|
84 |
+
'order_of_image' => null, // @deprecated 1.0.0 No replacement.
|
85 |
);
|
86 |
|
87 |
* `post_id` - The ID of the post to get the image for. This defaults to the current post in the loop.
|
88 |
+
* `order` - Order of methods used to grab images. Defaults to `array( 'meta_key', 'featured', 'attachment', 'scan', 'scan_raw', 'callback', 'default' )`.
|
89 |
* `meta_key` - This parameter refers to post meta keys (custom fields) that you use. Remember, meta keys are case-sensitive (defaults are `Thumbnail` and `thumbnail`). By default, this is an array of meta keys, but it can also be a string for a single meta key.
|
90 |
+
* `featured` - This refers to the `the_post_thumbnail()` WordPress function. By having this set to `true`, you may select an image from the featured image meta box while on the edit post screen.
|
91 |
* `attachment` - The script will look for images attached to the post (set to `true` by default).
|
92 |
+
* `scan` - If set to `true`, the script will search within your post for an image that's been added.
|
93 |
+
* `scan_raw` - If set to `true`, the script will search within your post for an image and pull the raw HTML for that image.
|
94 |
+
* `callback` - 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 `default` is set. The `$args` array is passed to the callback function as the only parameter.
|
95 |
+
* `default` - Will take the input of an image URL and use it if no other images are found (no default set).
|
96 |
+
* `split_content` - Whether to split the raw HTML of the found image from the post content. Default is `false`. This method is only used with the `scan_raw` method.
|
97 |
+
* `size` - This refers to the size of an attached image. You can choose between `thumbnail`, `medium`, `large`, `full`, or any custom image size you have available (the default is `thumbnail` or `post-thumbnail` if theme has set a thumbnail size).
|
98 |
* `link_to_post` - Whether the image shown should be linked to the post (set to `true` by default).
|
99 |
* `image_class` - You can give an additional class to the image for use in your CSS.
|
|
|
100 |
* `width` - Set the width of the image on output.
|
101 |
* `height` - Set the height of the image on output.
|
102 |
+
* `before` - HTML to place before the output of the image.
|
103 |
+
* `after` - HTML to place after the output of the image.
|
104 |
+
* `min_width` - Minimum width of the image to get. This won't work with the `scan*` methods. Defaults to `0`.
|
105 |
+
* `min_height` - Minimum height of the image to get. This won't work with the `scan*` methods. Defaults to `0`.
|
106 |
+
* `caption` - Whether to display the image caption if it exists. Defaults to `false`.
|
107 |
* `meta_key_save` - A meta key to save the image URL 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 `false`.
|
108 |
* `thumbnail_id_save` - 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 `false`
|
|
|
109 |
* `cache` - Whether to use the WordPress Cache API (integrates with caching plugins) to serve the post images. By default, this is set to `true`.
|
110 |
+
* `format` - What format to return the image in. If set to `array` the return value of the function will be an array of `<img>` attributes. All other values will return the `<img>` element.
|
|
|
111 |
* `echo` - If set to `true`, the image is shown on the page. If set to `false`, the image will be returned to use in your own function. (Set to `true` by default.)
|
112 |
|
113 |
### Some usage examples ##
|
138 |
|
139 |
You can even make the script scan for images that have been added to your post with this:
|
140 |
|
141 |
+
<?php get_the_image( array( 'scan' => true ) ); ?>
|
142 |
|
143 |
#### Example 5 ####
|
144 |
|
205 |
|
206 |
## Changelog ##
|
207 |
|
208 |
+
### Version 1.0.0 ###
|
209 |
+
|
210 |
+
#### General Changes: ####
|
211 |
+
|
212 |
+
* `the_post_thumbnail` argument deprecated in favor of `featured`.
|
213 |
+
* `image_scan` argument deprecated in favor of `scan` or `scan_raw`.
|
214 |
+
* `default_image` argument deprecated in favor of `default`.
|
215 |
+
* `order_of_image` argument removed with no replacement.
|
216 |
+
|
217 |
+
#### Enhancements: ####
|
218 |
+
|
219 |
+
* Re-coded how the image script works by encapsulating the functionality into a single class rather than multiple functions. This makes it much easier to reuse code and paves the way for more improvements in the future.
|
220 |
+
* New `scan_raw` argument for pulling an image (straight HTML) directly from the post content.
|
221 |
+
* New `split_content` argument for removing an image from the post content if one is found. Used only in conjunction with the `scan_raw` argument.
|
222 |
+
* New `order` argument for changing the order in which the script looks for images.
|
223 |
+
* Better support and handling for sub-attachments (e.g., featured images for audio/video attachments).
|
224 |
+
* Support for Schema.org microdata. `itemprop="image"` attribute added to image outputs where possible.
|
225 |
+
* New image orientation class if the width and height are available. Class can be `landscape` or `portrait`.
|
226 |
+
* Default image size is `post-thumbnail` if the theme has set this size. Otherwise, `thumbnail` is the default size.
|
227 |
+
* Supports the ability to get embedded images via WordPress' image embeds (Instagram, Flickr, etc.) via the `scan*` methods.
|
228 |
+
* New filter hook: `get_the_image_post_content`. Used when checking the post content.
|
229 |
+
* Added `min_width` and `min_height` arguments (doesn't work with `scan*` methods).
|
230 |
+
|
231 |
### Version 0.9.0 ###
|
232 |
|
233 |
+
#### Enhancements: ####
|
234 |
|
235 |
* Caption support. FTW!
|
236 |
* Multiple image classes now allowed via the `image_class` argument.
|
237 |
* Use current theme's `post-thumbnail` as default image size if set via `set_post_thumbnail_size()`.
|
238 |
|
239 |
+
#### Bug fixes: ####
|
240 |
|
241 |
* Use the WordPress-saved attachment alt text for the image.
|
242 |
* Only add `$out['src']` if `$out['url']` is set when returning as an array.
|
326 |
|
327 |
## Support ##
|
328 |
|
329 |
+
I run a WordPress community called [Theme Hybrid](http://themehybrid.com), 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.
|
330 |
|
331 |
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.
|
332 |
|
334 |
|
335 |
Get the Image is licensed under the [GNU GPL](http://www.gnu.org/licenses/old-licenses/gpl-2.0.html), version 2 or later.
|
336 |
|
337 |
+
2008 – 2014 © [Justin Tadlock](http://justintadlock.com).
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
readme.txt
CHANGED
@@ -3,9 +3,8 @@
|
|
3 |
Contributors: greenshady
|
4 |
Donate link: http://themehybrid.com/donate
|
5 |
Tags: image, images, thumbnail
|
6 |
-
Requires at least: 3.
|
7 |
-
|
8 |
-
Stable tag: 0.9.0
|
9 |
License: GPLv2 or later
|
10 |
License URI: http://www.gnu.org/licenses/gpl-2.0.html
|
11 |
|
@@ -35,6 +34,14 @@ Other image plugins have come and gone, but Get the Image has stood the test of
|
|
35 |
|
36 |
If you need professional plugin support from me, the plugin author, you can access the support forums at [Theme Hybrid](http://themehybrid.com/support), which is a professional WordPress help/support site where I handle support for all my plugins and themes for a community of 40,000+ users (and growing).
|
37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
== Installation ==
|
39 |
|
40 |
1. Upload `get-the-image` to the `/wp-content/plugins/` directory.
|
@@ -63,7 +70,7 @@ There are several methods, but in general, you would use this call within The Lo
|
|
63 |
|
64 |
<?php if ( function_exists( 'get_the_image' ) ) get_the_image(); ?>
|
65 |
|
66 |
-
To see all methods and options, refer to the `readme.md` file included with the plugin download.
|
67 |
|
68 |
== Screenshots ==
|
69 |
|
@@ -73,6 +80,29 @@ To see all methods and options, refer to the `readme.md` file included with the
|
|
73 |
|
74 |
== Changelog ==
|
75 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
### Version 0.9.0 ###
|
77 |
|
78 |
#### Enhancements: ####
|
3 |
Contributors: greenshady
|
4 |
Donate link: http://themehybrid.com/donate
|
5 |
Tags: image, images, thumbnail
|
6 |
+
Requires at least: 3.9
|
7 |
+
Stable tag: 1.0.0
|
|
|
8 |
License: GPLv2 or later
|
9 |
License URI: http://www.gnu.org/licenses/gpl-2.0.html
|
10 |
|
34 |
|
35 |
If you need professional plugin support from me, the plugin author, you can access the support forums at [Theme Hybrid](http://themehybrid.com/support), which is a professional WordPress help/support site where I handle support for all my plugins and themes for a community of 40,000+ users (and growing).
|
36 |
|
37 |
+
### Plugin Development ###
|
38 |
+
|
39 |
+
If you're a theme author, plugin author, or just a code hobbyist, you can follow the development of this plugin on its [GitHub repository](https://github.com/justintadlock/get-the-image).
|
40 |
+
|
41 |
+
### Donations ###
|
42 |
+
|
43 |
+
Yes, I do accept donations. If you want to buy me a beer or whatever, you can do so from my [donations page](http://themehybrid.com/donate). I appreciate all donations, no matter the size. Further development of this plugin is not contingent on donations, but they are always a nice incentive.
|
44 |
+
|
45 |
== Installation ==
|
46 |
|
47 |
1. Upload `get-the-image` to the `/wp-content/plugins/` directory.
|
70 |
|
71 |
<?php if ( function_exists( 'get_the_image' ) ) get_the_image(); ?>
|
72 |
|
73 |
+
To see all methods and options, refer to the `readme.md` file included with the plugin download or view it on the plugin's [GitHub page](https://github.com/justintadlock/get-the-image)
|
74 |
|
75 |
== Screenshots ==
|
76 |
|
80 |
|
81 |
== Changelog ==
|
82 |
|
83 |
+
### Version 1.0.0 ###
|
84 |
+
|
85 |
+
#### General Changes: ####
|
86 |
+
|
87 |
+
* `the_post_thumbnail` argument deprecated in favor of `featured`.
|
88 |
+
* `image_scan` argument deprecated in favor of `scan` or `scan_raw`.
|
89 |
+
* `default_image` argument deprecated in favor of `default`.
|
90 |
+
* `order_of_image` argument removed with no replacement.
|
91 |
+
|
92 |
+
#### Enhancements: ####
|
93 |
+
|
94 |
+
* Re-coded how the image script works by encapsulating the functionality into a single class rather than multiple functions. This makes it much easier to reuse code and paves the way for more improvements in the future.
|
95 |
+
* New `scan_raw` argument for pulling an image (straight HTML) directly from the post content.
|
96 |
+
* New `split_content` argument for removing an image from the post content if one is found. Used only in conjunction with the `scan_raw` argument.
|
97 |
+
* New `order` argument for changing the order in which the script looks for images.
|
98 |
+
* Better support and handling for sub-attachments (e.g., featured images for audio/video attachments).
|
99 |
+
* Support for Schema.org microdata. `itemprop="image"` attribute added to image outputs where possible.
|
100 |
+
* New image orientation class if the width and height are available. Class can be `landscape` or `portrait`.
|
101 |
+
* Default image size is `post-thumbnail` if the theme has set this size. Otherwise, `thumbnail` is the default size.
|
102 |
+
* Supports the ability to get embedded images via WordPress' image embeds (Instagram, Flickr, etc.) via the `scan*` methods.
|
103 |
+
* New filter hook: `get_the_image_post_content`. Used when checking the post content.
|
104 |
+
* Added `min_width` and `min_height` arguments (doesn't work with `scan*` methods).
|
105 |
+
|
106 |
### Version 0.9.0 ###
|
107 |
|
108 |
#### Enhancements: ####
|