Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 551 lines (450 sloc) 20.397 kb
4c2ae75d »
2010-07-17 Importing the files and folders for the Get the Image plugin.
1 <?php
2 /**
3 * Plugin Name: Get The Image
8d6c873a »
2012-06-12 Bringing plugin up to date with version 0.8 of the Get the Image script.
4 * Plugin URI: http://themehybrid.com/plugins/get-the-image
4c2ae75d »
2010-07-17 Importing the files and folders for the Get the Image plugin.
5 * Description: This is a highly intuitive script that can grab an image by custom field input, post attachment, or extracting it from the post's content.
a55fa22f »
2012-12-18 Version bump to 0.8.1.
6 * Version: 0.8.1
4c2ae75d »
2010-07-17 Importing the files and folders for the Get the Image plugin.
7 * Author: Justin Tadlock
8 * Author URI: http://justintadlock.com
9 *
7d490160 »
2010-10-23 Bringing this up to 0.6.1 standards.
10 * Get the Image - An advanced post image script for WordPress.
4c2ae75d »
2010-07-17 Importing the files and folders for the Get the Image plugin.
11 *
7d490160 »
2010-10-23 Bringing this up to 0.6.1 standards.
12 * Get the Image was created to be a highly-intuitive image script that displays post-specific images (an
13 * image-based representation of a post). The script handles old-style post images via custom fields for
14 * backwards compatibility. It also supports WordPress' built-in featured image functionality. On top of
15 * those things, it can automatically set attachment images as the post image or scan the post content for
16 * the first image element used. It can also fall back to a given default image.
17 *
18 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU
be3f6a38 »
2012-12-18 Bring in the changes from the 0.8.1 version of the Get the Image exte…
19 * General Public License as published by the Free Software Foundation; either version 2 of the License,
20 * or (at your option) any later version.
4c2ae75d »
2010-07-17 Importing the files and folders for the Get the Image plugin.
21 *
7d490160 »
2010-10-23 Bringing this up to 0.6.1 standards.
22 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
23 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
4c2ae75d »
2010-07-17 Importing the files and folders for the Get the Image plugin.
24 *
be3f6a38 »
2012-12-18 Bring in the changes from the 0.8.1 version of the Get the Image exte…
25 * @package GetTheImage
26 * @version 0.8.1
27 * @author Justin Tadlock <justin@justintadlock.com>
8d6c873a »
2012-06-12 Bringing plugin up to date with version 0.8 of the Get the Image script.
28 * @copyright Copyright (c) 2008 - 2012, Justin Tadlock
be3f6a38 »
2012-12-18 Bring in the changes from the 0.8.1 version of the Get the Image exte…
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
4c2ae75d »
2010-07-17 Importing the files and folders for the Get the Image plugin.
31 */
32
9933b0bd »
2011-09-19 Updating plugin to use version 0.7 of the script.
33 /* Adds theme support for WordPress 'featured images'. */
4c2ae75d »
2010-07-17 Importing the files and folders for the Get the Image plugin.
34 add_theme_support( 'post-thumbnails' );
35
ca5ff9c5 »
2010-07-17 Bringing code up to version 0.5.
36 /* Delete the cache when a post or post metadata is updated. */
9933b0bd »
2011-09-19 Updating plugin to use version 0.7 of the script.
37 add_action( 'save_post', 'get_the_image_delete_cache_by_post' );
38 add_action( 'deleted_post_meta', 'get_the_image_delete_cache_by_meta', 10, 2 );
39 add_action( 'updated_post_meta', 'get_the_image_delete_cache_by_meta', 10, 2 );
40 add_action( 'added_post_meta', 'get_the_image_delete_cache_by_meta', 10, 2 );
ca5ff9c5 »
2010-07-17 Bringing code up to version 0.5.
41
4c2ae75d »
2010-07-17 Importing the files and folders for the Get the Image plugin.
42 /**
7d490160 »
2010-10-23 Bringing this up to 0.6.1 standards.
43 * The main image function for displaying an image. It supports several arguments that allow developers to
44 * customize how the script outputs the image.
45 *
46 * The image check order is important to note here. If an image is found by any specific check, the script
47 * will no longer look for images. The check order is 'meta_key', 'the_post_thumbnail', 'attachment',
48 * 'image_scan', 'callback', and 'default_image'.
4c2ae75d »
2010-07-17 Importing the files and folders for the Get the Image plugin.
49 *
7d490160 »
2010-10-23 Bringing this up to 0.6.1 standards.
50 * @since 0.1.0
8d6c873a »
2012-06-12 Bringing plugin up to date with version 0.8 of the Get the Image script.
51 * @access public
9933b0bd »
2011-09-19 Updating plugin to use version 0.7 of the script.
52 * @global $post The current post's database object.
7d490160 »
2010-10-23 Bringing this up to 0.6.1 standards.
53 * @param array $args Arguments for how to load and display the image.
4c2ae75d »
2010-07-17 Importing the files and folders for the Get the Image plugin.
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(
be3f6a38 »
2012-12-18 Bring in the changes from the 0.8.1 version of the Get the Image exte…
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'.
4c2ae75d »
2010-07-17 Importing the files and folders for the Get the Image plugin.
82 );
83
84 /* Allow plugins/themes to filter the arguments. */
85 $args = apply_filters( 'get_the_image_args', $args );
86
87 /* Merge the input arguments and the defaults. */
88 $args = wp_parse_args( $args, $defaults );
89
ca5ff9c5 »
2010-07-17 Bringing code up to version 0.5.
90 /* If $default_size is given, overwrite $size. */
7d490160 »
2010-10-23 Bringing this up to 0.6.1 standards.
91 if ( !is_null( $args['default_size'] ) )
ecc26c2b »
2010-10-23 Deprecated custom_key in favor of meta_key.
92 $args['size'] = $args['default_size']; // Deprecated 0.5 in favor of $size
93
94 /* If $custom_key is set, overwrite $meta_key. */
7d490160 »
2010-10-23 Bringing this up to 0.6.1 standards.
95 if ( !is_null( $args['custom_key'] ) )
ecc26c2b »
2010-10-23 Deprecated custom_key in favor of meta_key.
96 $args['meta_key'] = $args['custom_key']; // Deprecated 0.6 in favor of $meta_key
ca5ff9c5 »
2010-07-17 Bringing code up to version 0.5.
97
98 /* If $format is set to 'array', don't link to the post. */
99 if ( 'array' == $args['format'] )
100 $args['link_to_post'] = false;
101
4c2ae75d »
2010-07-17 Importing the files and folders for the Get the Image plugin.
102 /* Extract the array to allow easy use of variables. */
103 extract( $args );
104
bd4cc246 »
2011-04-15 Bringing plugin up to version 0.6.2.
105 /* Get cache key based on $args. */
106 $key = md5( serialize( compact( array_keys( $args ) ) ) );
107
ca5ff9c5 »
2010-07-17 Bringing code up to version 0.5.
108 /* Check for a cached image. */
bd4cc246 »
2011-04-15 Bringing plugin up to version 0.6.2.
109 $image_cache = wp_cache_get( $post_id, 'get_the_image' );
4c2ae75d »
2010-07-17 Importing the files and folders for the Get the Image plugin.
110
ecc26c2b »
2010-10-23 Deprecated custom_key in favor of meta_key.
111 if ( !is_array( $image_cache ) )
112 $image_cache = array();
4c2ae75d »
2010-07-17 Importing the files and folders for the Get the Image plugin.
113
8d6c873a »
2012-06-12 Bringing plugin up to date with version 0.8 of the Get the Image script.
114 /* Set up a default, empty $image_html variable. */
115 $image_html = '';
116
ca5ff9c5 »
2010-07-17 Bringing code up to version 0.5.
117 /* If there is no cached image, let's see if one exists. */
be3f6a38 »
2012-12-18 Bring in the changes from the 0.8.1 version of the Get the Image exte…
118 if ( !isset( $image_cache[ $key ] ) || empty( $cache ) ) {
4c2ae75d »
2010-07-17 Importing the files and folders for the Get the Image plugin.
119
ca5ff9c5 »
2010-07-17 Bringing code up to version 0.5.
120 /* If a custom field key (array) is defined, check for images by custom field. */
ecc26c2b »
2010-10-23 Deprecated custom_key in favor of meta_key.
121 if ( !empty( $meta_key ) )
9933b0bd »
2011-09-19 Updating plugin to use version 0.7 of the script.
122 $image = get_the_image_by_meta_key( $args );
4c2ae75d »
2010-07-17 Importing the files and folders for the Get the Image plugin.
123
ca5ff9c5 »
2010-07-17 Bringing code up to version 0.5.
124 /* If no image found and $the_post_thumbnail is set to true, check for a post image (WP feature). */
ecc26c2b »
2010-10-23 Deprecated custom_key in favor of meta_key.
125 if ( empty( $image ) && !empty( $the_post_thumbnail ) )
9933b0bd »
2011-09-19 Updating plugin to use version 0.7 of the script.
126 $image = get_the_image_by_post_thumbnail( $args );
4c2ae75d »
2010-07-17 Importing the files and folders for the Get the Image plugin.
127
ca5ff9c5 »
2010-07-17 Bringing code up to version 0.5.
128 /* If no image found and $attachment is set to true, check for an image by attachment. */
ecc26c2b »
2010-10-23 Deprecated custom_key in favor of meta_key.
129 if ( empty( $image ) && !empty( $attachment ) )
9933b0bd »
2011-09-19 Updating plugin to use version 0.7 of the script.
130 $image = get_the_image_by_attachment( $args );
ca5ff9c5 »
2010-07-17 Bringing code up to version 0.5.
131
132 /* If no image found and $image_scan is set to true, scan the post for images. */
ecc26c2b »
2010-10-23 Deprecated custom_key in favor of meta_key.
133 if ( empty( $image ) && !empty( $image_scan ) )
9933b0bd »
2011-09-19 Updating plugin to use version 0.7 of the script.
134 $image = get_the_image_by_scan( $args );
ca5ff9c5 »
2010-07-17 Bringing code up to version 0.5.
135
9933b0bd »
2011-09-19 Updating plugin to use version 0.7 of the script.
136 /* If no image found and a callback function was given. Callback function must pass back array of <img> attributes. */
ecc26c2b »
2010-10-23 Deprecated custom_key in favor of meta_key.
137 if ( empty( $image ) && !is_null( $callback ) && function_exists( $callback ) )
138 $image = call_user_func( $callback, $args );
139
ca5ff9c5 »
2010-07-17 Bringing code up to version 0.5.
140 /* If no image found and a $default_image is set, get the default image. */
ecc26c2b »
2010-10-23 Deprecated custom_key in favor of meta_key.
141 if ( empty( $image ) && !empty( $default_image ) )
9933b0bd »
2011-09-19 Updating plugin to use version 0.7 of the script.
142 $image = get_the_image_by_default( $args );
ca5ff9c5 »
2010-07-17 Bringing code up to version 0.5.
143
bd4cc246 »
2011-04-15 Bringing plugin up to version 0.6.2.
144 /* If an image was found. */
145 if ( !empty( $image ) ) {
146
147 /* If $meta_key_save was set, save the image to a custom field. */
148 if ( !empty( $meta_key_save ) )
be3f6a38 »
2012-12-18 Bring in the changes from the 0.8.1 version of the Get the Image exte…
149 get_the_image_meta_key_save( $args, $image );
bd4cc246 »
2011-04-15 Bringing plugin up to version 0.6.2.
150
9933b0bd »
2011-09-19 Updating plugin to use version 0.7 of the script.
151 /* Format the image HTML. */
8d6c873a »
2012-06-12 Bringing plugin up to date with version 0.8 of the Get the Image script.
152 $image_html = get_the_image_format( $args, $image );
ca5ff9c5 »
2010-07-17 Bringing code up to version 0.5.
153
9933b0bd »
2011-09-19 Updating plugin to use version 0.7 of the script.
154 /* Set the image cache for the specific post. */
be3f6a38 »
2012-12-18 Bring in the changes from the 0.8.1 version of the Get the Image exte…
155 $image_cache[ $key ] = $image_html;
9933b0bd »
2011-09-19 Updating plugin to use version 0.7 of the script.
156 wp_cache_set( $post_id, $image_cache, 'get_the_image' );
157 }
ca5ff9c5 »
2010-07-17 Bringing code up to version 0.5.
158 }
9933b0bd »
2011-09-19 Updating plugin to use version 0.7 of the script.
159
160 /* If an image was already cached for the post and arguments, use it. */
ca5ff9c5 »
2010-07-17 Bringing code up to version 0.5.
161 else {
8d6c873a »
2012-06-12 Bringing plugin up to date with version 0.8 of the Get the Image script.
162 $image_html = $image_cache[$key];
ca5ff9c5 »
2010-07-17 Bringing code up to version 0.5.
163 }
4c2ae75d »
2010-07-17 Importing the files and folders for the Get the Image plugin.
164
165 /* Allow plugins/theme to override the final output. */
8d6c873a »
2012-06-12 Bringing plugin up to date with version 0.8 of the Get the Image script.
166 $image_html = apply_filters( 'get_the_image', $image_html );
4c2ae75d »
2010-07-17 Importing the files and folders for the Get the Image plugin.
167
bd4cc246 »
2011-04-15 Bringing plugin up to version 0.6.2.
168 /* If $format is set to 'array', return an array of image attributes. */
ca5ff9c5 »
2010-07-17 Bringing code up to version 0.5.
169 if ( 'array' == $format ) {
9933b0bd »
2011-09-19 Updating plugin to use version 0.7 of the script.
170
171 /* Set up a default empty array. */
172 $out = array();
173
174 /* Get the image attributes. */
8d6c873a »
2012-06-12 Bringing plugin up to date with version 0.8 of the Get the Image script.
175 $atts = wp_kses_hair( $image_html, array( 'http' ) );
ca5ff9c5 »
2010-07-17 Bringing code up to version 0.5.
176
9933b0bd »
2011-09-19 Updating plugin to use version 0.7 of the script.
177 /* Loop through the image attributes and add them in key/value pairs for the return array. */
ca5ff9c5 »
2010-07-17 Bringing code up to version 0.5.
178 foreach ( $atts as $att )
be3f6a38 »
2012-12-18 Bring in the changes from the 0.8.1 version of the Get the Image exte…
179 $out[ $att['name'] ] = $att['value'];
ca5ff9c5 »
2010-07-17 Bringing code up to version 0.5.
180
181 $out['url'] = $out['src']; // @deprecated 0.5 Use 'src' instead of 'url'.
9933b0bd »
2011-09-19 Updating plugin to use version 0.7 of the script.
182
183 /* Return the array of attributes. */
ca5ff9c5 »
2010-07-17 Bringing code up to version 0.5.
184 return $out;
185 }
bd4cc246 »
2011-04-15 Bringing plugin up to version 0.6.2.
186
187 /* Or, if $echo is set to false, return the formatted image. */
9933b0bd »
2011-09-19 Updating plugin to use version 0.7 of the script.
188 elseif ( false === $echo ) {
be3f6a38 »
2012-12-18 Bring in the changes from the 0.8.1 version of the Get the Image exte…
189 return !empty( $image_html ) ? $args['before'] . $image_html . $args['after'] : $image_html;
bd4cc246 »
2011-04-15 Bringing plugin up to version 0.6.2.
190 }
191
8d6c873a »
2012-06-12 Bringing plugin up to date with version 0.8 of the Get the Image script.
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
bd4cc246 »
2011-04-15 Bringing plugin up to version 0.6.2.
196 /* Display the image if we get to this point. */
be3f6a38 »
2012-12-18 Bring in the changes from the 0.8.1 version of the Get the Image exte…
197 echo !empty( $image_html ) ? $args['before'] . $image_html . $args['after'] : $image_html;
8d6c873a »
2012-06-12 Bringing plugin up to date with version 0.8 of the Get the Image script.
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 );
4c2ae75d »
2010-07-17 Importing the files and folders for the Get the Image plugin.
202 }
203
204 /* Internal Functions */
205
206 /**
be3f6a38 »
2012-12-18 Bring in the changes from the 0.8.1 version of the Get the Image exte…
207 * Calls images by custom field key. Script loops through multiple custom field keys. If that particular
208 * key is found, $image is set and the loop breaks. If an image is found, it is returned.
4c2ae75d »
2010-07-17 Importing the files and folders for the Get the Image plugin.
209 *
9933b0bd »
2011-09-19 Updating plugin to use version 0.7 of the script.
210 * @since 0.7.0
8d6c873a »
2012-06-12 Bringing plugin up to date with version 0.8 of the Get the Image script.
211 * @access private
9933b0bd »
2011-09-19 Updating plugin to use version 0.7 of the script.
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.
4c2ae75d »
2010-07-17 Importing the files and folders for the Get the Image plugin.
214 */
9933b0bd »
2011-09-19 Updating plugin to use version 0.7 of the script.
215 function get_the_image_by_meta_key( $args = array() ) {
4c2ae75d »
2010-07-17 Importing the files and folders for the Get the Image plugin.
216
7d490160 »
2010-10-23 Bringing this up to 0.6.1 standards.
217 /* If $meta_key is not an array. */
8d6c873a »
2012-06-12 Bringing plugin up to date with version 0.8 of the Get the Image script.
218 if ( !is_array( $args['meta_key'] ) )
219 $args['meta_key'] = array( $args['meta_key'] );
7d490160 »
2010-10-23 Bringing this up to 0.6.1 standards.
220
8d6c873a »
2012-06-12 Bringing plugin up to date with version 0.8 of the Get the Image script.
221 /* Loop through each of the given meta keys. */
222 foreach ( $args['meta_key'] as $meta_key ) {
7d490160 »
2010-10-23 Bringing this up to 0.6.1 standards.
223
8d6c873a »
2012-06-12 Bringing plugin up to date with version 0.8 of the Get the Image script.
224 /* Get the image URL by the current meta key in the loop. */
225 $image = get_post_meta( $args['post_id'], $meta_key, true );
7d490160 »
2010-10-23 Bringing this up to 0.6.1 standards.
226
8d6c873a »
2012-06-12 Bringing plugin up to date with version 0.8 of the Get the Image script.
227 /* If an image was found, break out of the loop. */
228 if ( !empty( $image ) )
229 break;
4c2ae75d »
2010-07-17 Importing the files and folders for the Get the Image plugin.
230 }
231
232 /* If a custom key value has been given for one of the keys, return the image URL. */
7d490160 »
2010-10-23 Bringing this up to 0.6.1 standards.
233 if ( !empty( $image ) )
9933b0bd »
2011-09-19 Updating plugin to use version 0.7 of the script.
234 return array( 'src' => $image );
4c2ae75d »
2010-07-17 Importing the files and folders for the Get the Image plugin.
235
236 return false;
237 }
238
239 /**
7d490160 »
2010-10-23 Bringing this up to 0.6.1 standards.
240 * Checks for images using a custom version of the WordPress 2.9+ get_the_post_thumbnail() function.
241 * If an image is found, return it and the $post_thumbnail_id. The WordPress function's other filters are
242 * later added in the display_the_image() function.
4c2ae75d »
2010-07-17 Importing the files and folders for the Get the Image plugin.
243 *
9933b0bd »
2011-09-19 Updating plugin to use version 0.7 of the script.
244 * @since 0.7.0
8d6c873a »
2012-06-12 Bringing plugin up to date with version 0.8 of the Get the Image script.
245 * @access private
9933b0bd »
2011-09-19 Updating plugin to use version 0.7 of the script.
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.
4c2ae75d »
2010-07-17 Importing the files and folders for the Get the Image plugin.
248 */
9933b0bd »
2011-09-19 Updating plugin to use version 0.7 of the script.
249 function get_the_image_by_post_thumbnail( $args = array() ) {
4c2ae75d »
2010-07-17 Importing the files and folders for the Get the Image plugin.
250
251 /* Check for a post image ID (set by WP as a custom field). */
252 $post_thumbnail_id = get_post_thumbnail_id( $args['post_id'] );
253
254 /* If no post image ID is found, return false. */
255 if ( empty( $post_thumbnail_id ) )
256 return false;
257
258 /* Apply filters on post_thumbnail_size because this is a default WP filter used with its image feature. */
ca5ff9c5 »
2010-07-17 Bringing code up to version 0.5.
259 $size = apply_filters( 'post_thumbnail_size', $args['size'] );
4c2ae75d »
2010-07-17 Importing the files and folders for the Get the Image plugin.
260
261 /* Get the attachment image source. This should return an array. */
262 $image = wp_get_attachment_image_src( $post_thumbnail_id, $size );
263
ca5ff9c5 »
2010-07-17 Bringing code up to version 0.5.
264 /* Get the attachment excerpt to use as alt text. */
265 $alt = trim( strip_tags( get_post_field( 'post_excerpt', $post_thumbnail_id ) ) );
266
4c2ae75d »
2010-07-17 Importing the files and folders for the Get the Image plugin.
267 /* Return both the image URL and the post thumbnail ID. */
9933b0bd »
2011-09-19 Updating plugin to use version 0.7 of the script.
268 return array( 'src' => $image[0], 'post_thumbnail_id' => $post_thumbnail_id, 'alt' => $alt );
4c2ae75d »
2010-07-17 Importing the files and folders for the Get the Image plugin.
269 }
270
271 /**
7d490160 »
2010-10-23 Bringing this up to 0.6.1 standards.
272 * Check for attachment images. Uses get_children() to check if the post has images attached. If image
273 * attachments are found, loop through each. The loop only breaks once $order_of_image is reached.
4c2ae75d »
2010-07-17 Importing the files and folders for the Get the Image plugin.
274 *
9933b0bd »
2011-09-19 Updating plugin to use version 0.7 of the script.
275 * @since 0.7.0
8d6c873a »
2012-06-12 Bringing plugin up to date with version 0.8 of the Get the Image script.
276 * @access private
9933b0bd »
2011-09-19 Updating plugin to use version 0.7 of the script.
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.
4c2ae75d »
2010-07-17 Importing the files and folders for the Get the Image plugin.
279 */
9933b0bd »
2011-09-19 Updating plugin to use version 0.7 of the script.
280 function get_the_image_by_attachment( $args = array() ) {
4c2ae75d »
2010-07-17 Importing the files and folders for the Get the Image plugin.
281
8d6c873a »
2012-06-12 Bringing plugin up to date with version 0.8 of the Get the Image script.
282 /* Get the post type of the current post. */
283 $post_type = get_post_type( $args['post_id'] );
4c2ae75d »
2010-07-17 Importing the files and folders for the Get the Image plugin.
284
8d6c873a »
2012-06-12 Bringing plugin up to date with version 0.8 of the Get the Image script.
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(
be3f6a38 »
2012-12-18 Bring in the changes from the 0.8.1 version of the Get the Image exte…
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',
8d6c873a »
2012-06-12 Bringing plugin up to date with version 0.8 of the Get the Image script.
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 }
ca5ff9c5 »
2010-07-17 Bringing code up to version 0.5.
322 }
323 }
324
8d6c873a »
2012-06-12 Bringing plugin up to date with version 0.8 of the Get the Image script.
325 /* Check if we have an attachment ID before proceeding. */
326 if ( !empty( $attachment_id ) ) {
4c2ae75d »
2010-07-17 Importing the files and folders for the Get the Image plugin.
327
8d6c873a »
2012-06-12 Bringing plugin up to date with version 0.8 of the Get the Image script.
328 /* Get the attachment image. */
be3f6a38 »
2012-12-18 Bring in the changes from the 0.8.1 version of the Get the Image exte…
329 $image = wp_get_attachment_image_src( $attachment_id, $args['size'] );
ecc26c2b »
2010-10-23 Deprecated custom_key in favor of meta_key.
330
8d6c873a »
2012-06-12 Bringing plugin up to date with version 0.8 of the Get the Image script.
331 /* Get the attachment excerpt. */
be3f6a38 »
2012-12-18 Bring in the changes from the 0.8.1 version of the Get the Image exte…
332 $alt = trim( strip_tags( get_post_field( 'post_excerpt', $attachment_id ) ) );
8d6c873a »
2012-06-12 Bringing plugin up to date with version 0.8 of the Get the Image script.
333
334 /* Save the attachment as the 'featured image'. */
335 if ( true === $args['thumbnail_id_save'] )
be3f6a38 »
2012-12-18 Bring in the changes from the 0.8.1 version of the Get the Image exte…
336 set_post_thumbnail( $args['post_id'], $attachment_id );
8d6c873a »
2012-06-12 Bringing plugin up to date with version 0.8 of the Get the Image script.
337
338 /* Return the image URL. */
339 return array( 'src' => $image[0], 'alt' => $alt );
4c2ae75d »
2010-07-17 Importing the files and folders for the Get the Image plugin.
340 }
341
8d6c873a »
2012-06-12 Bringing plugin up to date with version 0.8 of the Get the Image script.
342 /* Return false for anything else. */
343 return false;
4c2ae75d »
2010-07-17 Importing the files and folders for the Get the Image plugin.
344 }
345
346 /**
7d490160 »
2010-10-23 Bringing this up to 0.6.1 standards.
347 * Scans the post for images within the content. Not called by default with get_the_image(). Shouldn't use
348 * if using large images within posts, better to use the other options.
4c2ae75d »
2010-07-17 Importing the files and folders for the Get the Image plugin.
349 *
9933b0bd »
2011-09-19 Updating plugin to use version 0.7 of the script.
350 * @since 0.7.0
8d6c873a »
2012-06-12 Bringing plugin up to date with version 0.8 of the Get the Image script.
351 * @access private
9933b0bd »
2011-09-19 Updating plugin to use version 0.7 of the script.
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.
4c2ae75d »
2010-07-17 Importing the files and folders for the Get the Image plugin.
354 */
9933b0bd »
2011-09-19 Updating plugin to use version 0.7 of the script.
355 function get_the_image_by_scan( $args = array() ) {
4c2ae75d »
2010-07-17 Importing the files and folders for the Get the Image plugin.
356
357 /* Search the post's content for the <img /> tag and get its URL. */
358 preg_match_all( '|<img.*?src=[\'"](.*?)[\'"].*?>|i', get_post_field( 'post_content', $args['post_id'] ), $matches );
359
360 /* If there is a match for the image, return its URL. */
9933b0bd »
2011-09-19 Updating plugin to use version 0.7 of the script.
361 if ( isset( $matches ) && !empty( $matches[1][0] ) )
362 return array( 'src' => $matches[1][0] );
4c2ae75d »
2010-07-17 Importing the files and folders for the Get the Image plugin.
363
364 return false;
365 }
366
367 /**
7d490160 »
2010-10-23 Bringing this up to 0.6.1 standards.
368 * Used for setting a default image. The function simply returns the image URL it was given in an array.
369 * Not used with get_the_image() by default.
4c2ae75d »
2010-07-17 Importing the files and folders for the Get the Image plugin.
370 *
9933b0bd »
2011-09-19 Updating plugin to use version 0.7 of the script.
371 * @since 0.7.0
8d6c873a »
2012-06-12 Bringing plugin up to date with version 0.8 of the Get the Image script.
372 * @access private
9933b0bd »
2011-09-19 Updating plugin to use version 0.7 of the script.
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.
4c2ae75d »
2010-07-17 Importing the files and folders for the Get the Image plugin.
375 */
9933b0bd »
2011-09-19 Updating plugin to use version 0.7 of the script.
376 function get_the_image_by_default( $args = array() ) {
377 return array( 'src' => $args['default_image'] );
4c2ae75d »
2010-07-17 Importing the files and folders for the Get the Image plugin.
378 }
379
380 /**
7d490160 »
2010-10-23 Bringing this up to 0.6.1 standards.
381 * Formats an image with appropriate alt text and class. Adds a link to the post if argument is set. Should
382 * only be called if there is an image to display, but will handle it if not.
4c2ae75d »
2010-07-17 Importing the files and folders for the Get the Image plugin.
383 *
9933b0bd »
2011-09-19 Updating plugin to use version 0.7 of the script.
384 * @since 0.7.0
8d6c873a »
2012-06-12 Bringing plugin up to date with version 0.8 of the Get the Image script.
385 * @access private
9933b0bd »
2011-09-19 Updating plugin to use version 0.7 of the script.
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).
4c2ae75d »
2010-07-17 Importing the files and folders for the Get the Image plugin.
388 * @return string $image Formatted image (w/link to post if the option is set).
389 */
9933b0bd »
2011-09-19 Updating plugin to use version 0.7 of the script.
390 function get_the_image_format( $args = array(), $image = false ) {
4c2ae75d »
2010-07-17 Importing the files and folders for the Get the Image plugin.
391
392 /* If there is no image URL, return false. */
bd4cc246 »
2011-04-15 Bringing plugin up to version 0.6.2.
393 if ( empty( $image['src'] ) )
4c2ae75d »
2010-07-17 Importing the files and folders for the Get the Image plugin.
394 return false;
395
396 /* Extract the arguments for easy-to-use variables. */
397 extract( $args );
398
ca5ff9c5 »
2010-07-17 Bringing code up to version 0.5.
399 /* If there is alt text, set it. Otherwise, default to the post title. */
ecc26c2b »
2010-10-23 Deprecated custom_key in favor of meta_key.
400 $image_alt = ( ( !empty( $image['alt'] ) ) ? $image['alt'] : apply_filters( 'the_title', get_post_field( 'post_title', $post_id ) ) );
ca5ff9c5 »
2010-07-17 Bringing code up to version 0.5.
401
4c2ae75d »
2010-07-17 Importing the files and folders for the Get the Image plugin.
402 /* If there is a width or height, set them as HMTL-ready attributes. */
ca5ff9c5 »
2010-07-17 Bringing code up to version 0.5.
403 $width = ( ( $width ) ? ' width="' . esc_attr( $width ) . '"' : '' );
404 $height = ( ( $height ) ? ' height="' . esc_attr( $height ) . '"' : '' );
4c2ae75d »
2010-07-17 Importing the files and folders for the Get the Image plugin.
405
406 /* Loop through the custom field keys and add them as classes. */
ecc26c2b »
2010-10-23 Deprecated custom_key in favor of meta_key.
407 if ( is_array( $meta_key ) ) {
408 foreach ( $meta_key as $key )
8d6c873a »
2012-06-12 Bringing plugin up to date with version 0.8 of the Get the Image script.
409 $classes[] = sanitize_html_class( $key );
4c2ae75d »
2010-07-17 Importing the files and folders for the Get the Image plugin.
410 }
411
ca5ff9c5 »
2010-07-17 Bringing code up to version 0.5.
412 /* Add the $size and any user-added $image_class to the class. */
8d6c873a »
2012-06-12 Bringing plugin up to date with version 0.8 of the Get the Image script.
413 $classes[] = sanitize_html_class( $size );
414 $classes[] = sanitize_html_class( $image_class );
4c2ae75d »
2010-07-17 Importing the files and folders for the Get the Image plugin.
415
ca5ff9c5 »
2010-07-17 Bringing code up to version 0.5.
416 /* Join all the classes into a single string and make sure there are no duplicates. */
417 $class = join( ' ', array_unique( $classes ) );
4c2ae75d »
2010-07-17 Importing the files and folders for the Get the Image plugin.
418
419 /* Add the image attributes to the <img /> element. */
bd4cc246 »
2011-04-15 Bringing plugin up to version 0.6.2.
420 $html = '<img src="' . $image['src'] . '" alt="' . esc_attr( strip_tags( $image_alt ) ) . '" class="' . esc_attr( $class ) . '"' . $width . $height . ' />';
4c2ae75d »
2010-07-17 Importing the files and folders for the Get the Image plugin.
421
422 /* If $link_to_post is set to true, link the image to its post. */
423 if ( $link_to_post )
ca5ff9c5 »
2010-07-17 Bringing code up to version 0.5.
424 $html = '<a href="' . get_permalink( $post_id ) . '" title="' . esc_attr( apply_filters( 'the_title', get_post_field( 'post_title', $post_id ) ) ) . '">' . $html . '</a>';
4c2ae75d »
2010-07-17 Importing the files and folders for the Get the Image plugin.
425
426 /* If there is a $post_thumbnail_id, apply the WP filters normally associated with get_the_post_thumbnail(). */
ecc26c2b »
2010-10-23 Deprecated custom_key in favor of meta_key.
427 if ( !empty( $image['post_thumbnail_id'] ) )
ca5ff9c5 »
2010-07-17 Bringing code up to version 0.5.
428 $html = apply_filters( 'post_thumbnail_html', $html, $post_id, $image['post_thumbnail_id'], $size, '' );
4c2ae75d »
2010-07-17 Importing the files and folders for the Get the Image plugin.
429
430 return $html;
431 }
432
433 /**
7d490160 »
2010-10-23 Bringing this up to 0.6.1 standards.
434 * Saves the image URL as the value of the meta key provided. This allows users to set a custom meta key
435 * for their image. By doing this, users can trim off database queries when grabbing attachments or get rid
436 * of expensive scans of the content when using the image scan feature.
ecc26c2b »
2010-10-23 Deprecated custom_key in favor of meta_key.
437 *
7d490160 »
2010-10-23 Bringing this up to 0.6.1 standards.
438 * @since 0.6.0
8d6c873a »
2012-06-12 Bringing plugin up to date with version 0.8 of the Get the Image script.
439 * @access private
9933b0bd »
2011-09-19 Updating plugin to use version 0.7 of the script.
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).
ecc26c2b »
2010-10-23 Deprecated custom_key in favor of meta_key.
442 */
443 function get_the_image_meta_key_save( $args = array(), $image = array() ) {
444
445 /* If the $meta_key_save argument is empty or there is no image $url given, return. */
bd4cc246 »
2011-04-15 Bringing plugin up to version 0.6.2.
446 if ( empty( $args['meta_key_save'] ) || empty( $image['src'] ) )
ecc26c2b »
2010-10-23 Deprecated custom_key in favor of meta_key.
447 return;
448
449 /* Get the current value of the meta key. */
450 $meta = get_post_meta( $args['post_id'], $args['meta_key_save'], true );
451
452 /* If there is no value for the meta key, set a new value with the image $url. */
453 if ( empty( $meta ) )
bd4cc246 »
2011-04-15 Bringing plugin up to version 0.6.2.
454 add_post_meta( $args['post_id'], $args['meta_key_save'], $image['src'] );
ecc26c2b »
2010-10-23 Deprecated custom_key in favor of meta_key.
455
456 /* If the current value doesn't match the image $url, update it. */
bd4cc246 »
2011-04-15 Bringing plugin up to version 0.6.2.
457 elseif ( $meta !== $image['src'] )
458 update_post_meta( $args['post_id'], $args['meta_key_save'], $image['src'], $meta );
ecc26c2b »
2010-10-23 Deprecated custom_key in favor of meta_key.
459 }
460
461 /**
9933b0bd »
2011-09-19 Updating plugin to use version 0.7 of the script.
462 * Deletes the image cache for the specific post when the 'save_post' hook is fired.
ca5ff9c5 »
2010-07-17 Bringing code up to version 0.5.
463 *
9933b0bd »
2011-09-19 Updating plugin to use version 0.7 of the script.
464 * @since 0.7.0
8d6c873a »
2012-06-12 Bringing plugin up to date with version 0.8 of the Get the Image script.
465 * @access private
466 * @param int $post_id The ID of the post to delete the cache for.
467 * @return void
ca5ff9c5 »
2010-07-17 Bringing code up to version 0.5.
468 */
9933b0bd »
2011-09-19 Updating plugin to use version 0.7 of the script.
469 function get_the_image_delete_cache_by_post( $post_id ) {
470 wp_cache_delete( $post_id, 'get_the_image' );
ca5ff9c5 »
2010-07-17 Bringing code up to version 0.5.
471 }
472
473 /**
9933b0bd »
2011-09-19 Updating plugin to use version 0.7 of the script.
474 * Deletes the image cache for a specific post when the 'added_post_meta', 'deleted_post_meta',
475 * or 'updated_post_meta' hooks are called.
4c2ae75d »
2010-07-17 Importing the files and folders for the Get the Image plugin.
476 *
9933b0bd »
2011-09-19 Updating plugin to use version 0.7 of the script.
477 * @since 0.7.0
8d6c873a »
2012-06-12 Bringing plugin up to date with version 0.8 of the Get the Image script.
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
9933b0bd »
2011-09-19 Updating plugin to use version 0.7 of the script.
482 */
483 function get_the_image_delete_cache_by_meta( $meta_id, $post_id ) {
484 wp_cache_delete( $post_id, 'get_the_image' );
485 }
486
487 /**
7d490160 »
2010-10-23 Bringing this up to 0.6.1 standards.
488 * @since 0.1.0
489 * @deprecated 0.3.0
4c2ae75d »
2010-07-17 Importing the files and folders for the Get the Image plugin.
490 */
491 function get_the_image_link( $deprecated = '', $deprecated_2 = '', $deprecated_3 = '' ) {
492 get_the_image();
493 }
494
9933b0bd »
2011-09-19 Updating plugin to use version 0.7 of the script.
495 /**
496 * @since 0.3.0
497 * @deprecated 0.7.0
498 */
499 function image_by_custom_field( $args = array() ) {
500 return get_the_image_by_meta_key( $args );
501 }
502
503 /**
504 * @since 0.4.0
505 * @deprecated 0.7.0
506 */
507 function image_by_the_post_thumbnail( $args = array() ) {
508 return get_the_image_by_post_thumbnail( $args );
509 }
510
511 /**
512 * @since 0.3.0
513 * @deprecated 0.7.0
514 */
515 function image_by_attachment( $args = array() ) {
516 return get_the_image_by_attachment( $args );
517 }
518
519 /**
520 * @since 0.3.0
521 * @deprecated 0.7.0
522 */
523 function image_by_scan( $args = array() ) {
524 return get_the_image_by_scan( $args );
525 }
526
527 /**
528 * @since 0.3.0
529 * @deprecated 0.7.0
530 */
531 function image_by_default( $args = array() ) {
532 return get_the_image_by_default( $args );
533 }
534
535 /**
536 * @since 0.1.0
537 * @deprecated 0.7.0
538 */
539 function display_the_image( $args = array(), $image = false ) {
540 return get_the_image_format( $args, $image );
541 }
542
543 /**
544 * @since 0.5.0
545 * @deprecated 0.7.0 Replaced by cache delete functions specifically for the post ID.
546 */
547 function get_the_image_delete_cache() {
548 return;
549 }
550
4c2ae75d »
2010-07-17 Importing the files and folders for the Get the Image plugin.
551 ?>
Something went wrong with that request. Please try again.