Guest User

bobthemonster

a guest
Feb 26th, 2009
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.51 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Get The Image
  4. Plugin URI: http://justintadlock.com/archives/2008/05/27/get-the-image-wordpress-plugin
  5. Description: This is a highly intuitive script that can grab an image by custom field input, post attachment, or extracting it from the post's content.
  6. Version: 0.3.1
  7. Author: Justin Tadlock
  8. Author URI: http://justintadlock.com
  9. License: GPL
  10. */
  11.  
  12. /**
  13. * This is a highly intuitive script file that gets images
  14. * It first calls for custom field keys
  15. * If no custom field key is set, check for images "attached" to post
  16. * Check for image order if looking for attached images
  17. * Scan the post for images if $image_scan = true
  18. * Check for default image if $default_image = true
  19. *
  20. * Entirely rewrote the system in 0.4
  21. *
  22. * @package Hybrid
  23. * @subpackage Media
  24. *
  25. * @since 0.1
  26. * @filter get_the_image_args
  27. */
  28. function get_the_image($args = array()) {
  29.  
  30.     $defaults = array(
  31.         'custom_key' => array('Thumbnail','thumbnail'),
  32.         'post_id' => false, // Build functionality in later
  33.         'attachment' => true,
  34.         'default_size' => 'thumbnail',
  35.         'default_image' => false,
  36.         'order_of_image' => 1,
  37.         'link_to_post' => true,
  38.         'image_class' => false,
  39.         'image_scan' => false,
  40.         'width' => false,
  41.         'height' => false,
  42.         'format' => 'img',
  43.         'echo' => true
  44.     );
  45.  
  46.     $args = apply_filters('get_the_image_args', $args);
  47.  
  48.     $args = wp_parse_args($args, $defaults);
  49.  
  50.     extract($args);
  51.  
  52.     if(!is_array($custom_key)) :
  53.         $custom_key = str_replace(' ', '', $custom_key);
  54.         $custom_key = str_replace(array('+'), ',', $custom_key);
  55.         $custom_key = explode(',', $custom_key);
  56.         $args['custom_key'] = $custom_key;
  57.     endif;
  58.  
  59.     if($custom_key && $custom_key !== 'false' && $custom_key !== '0') $image = image_by_custom_field($args);
  60.  
  61.     if(!$image && $attachment && $attachment !== 'false' && $attachment !== '0') $image = image_by_attachment($args);
  62.  
  63.     if(!$image && $image_scan) $image = image_by_scan($args);
  64.  
  65.     if(!$image && $default_image) $image = image_by_default($args);
  66.  
  67.     if($image)
  68.         $image = display_the_image($args, $image);
  69.  
  70.     else
  71.         $image = '<!-- No images were added to this post. -->';
  72.  
  73.     if($echo && $echo !== 'false' && $echo !== '0' && $format !== 'array')
  74.         echo $image;
  75.     else
  76.         return $image;
  77. }
  78.  
  79. /**
  80. * Calls images by custom field key
  81. * Allow looping through multiple custom fields
  82. *
  83. * @since 0.4
  84. * @param $args Not Optional
  85. * @return array $image, $classes, $alt
  86. */
  87. function image_by_custom_field($args = array()) {
  88.  
  89.     extract($args);
  90.  
  91.     if(!$post_id)
  92.         global $post;
  93.  
  94.     if(isset($custom_key)) :
  95.         foreach($custom_key as $custom) :
  96.             $image = get_post_meta($post->ID, $custom, true);
  97.             if($image) :
  98.                 break;
  99.             endif;
  100.         endforeach;
  101.         if(!$image) :
  102.             return false;
  103.         endif;
  104.     endif;
  105.  
  106.     return array('image' => $image);
  107. }
  108.  
  109. /**
  110. * Check for attachment images
  111. * Uses get_children() to check if the post has images attached
  112. *
  113. * @since 0.4
  114. * @param $args Not Optional
  115. * @return array $image, $classes, $alt, $caption
  116. */
  117. function image_by_attachment($args = array()) {
  118.  
  119.     extract($args);
  120.  
  121.     if(!$post_id)
  122.         global $post;
  123.  
  124.     /*
  125.     * Use a WP 2.6 function to check
  126.     */
  127.     if(function_exists('wp_enqueue_style')) :
  128.         $attachments = get_children(array('post_parent' => $post->ID, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID'));
  129.  
  130.     /*
  131.     * WP 2.5 compatibility
  132.     */
  133.     else :
  134.         $attachments = get_children("post_parent=$post->ID&post_type=attachment&post_mime_type=image&orderby=\"menu_order ASC, ID ASC\"");
  135.  
  136.     endif;
  137.  
  138.     if(empty($attachments)) :
  139.         return false;
  140.     else :
  141.         foreach($attachments as $id => $attachment) :
  142.             $i++;
  143.             if($i == $order_of_image) :
  144.                 $image = wp_get_attachment_image_src($id, $default_size);
  145.                 $image = $image[0];
  146.                 break;
  147.             endif;
  148.         endforeach;
  149.     endif;
  150.  
  151.     return array('image' => $image);
  152. }
  153.  
  154. /**
  155. * Scans the post for images within the content
  156. * Not called by default with get_the_image()
  157. * Shouldn't use if using large images within posts, better to use the other options
  158. *
  159. * @since 0.4
  160. * @param $args Not Optional
  161. * @return $image, $classes, $alt
  162. */
  163. function image_by_scan($args = array()) {
  164.  
  165.     if(!$post_id)
  166.         global $post;
  167.  
  168.     preg_match_all('|<img.*?src=[\'"](.*?)[\'"].*?>|i', $post->post_content, $matches);
  169.  
  170.     if(isset($matches)) $image = $matches[1][0];
  171.  
  172.     if($matches[1][0])
  173.         return array('image' => $image);
  174.     else
  175.         return false;
  176. }
  177.  
  178. /**
  179. * Used for setting a default image
  180. * Not used with get_the_image() by default
  181. *
  182. * @since 0.4
  183. * @param $args Not Optional
  184. * @return array $image, $classes, $alt
  185. */
  186. function image_by_default($args = array()) {
  187.  
  188.     extract($args);
  189.  
  190.     $image = $default_image;
  191.  
  192.     return array('image' => $image);
  193. }
  194.  
  195. /**
  196. * Formats an image with appropriate alt text and class
  197. * Adds a link to the post if argument is set
  198. * Should only be called if there is an image to display, but will handle it if not
  199. *
  200. * @since 0.1
  201. * @param $args Not Optional
  202. * @param $arr Array of image info ($image, $classes, $alt, $caption)
  203. * @return string Formatted image (w/link to post if the option is set)
  204. */
  205. function display_the_image($args = array(), $arr = false) {
  206.     global $post;
  207.  
  208.     extract($arr);
  209.  
  210.     if(!$image)
  211.         return;
  212.  
  213.     extract($args);
  214.  
  215.     if($width) $width = ' width="' . $width . '"';
  216.     if($height) $height = ' height="' . $height . '"';
  217.  
  218.     $img = $image;
  219.  
  220.     if(is_array($custom_key)) :
  221.         foreach($custom_key as $key) :
  222.             if($key !== 'false' && $key !== '0') :
  223.                 $classes[] = str_replace(' ', '-', strtolower($key));
  224.             endif;
  225.         endforeach;
  226.     endif;
  227.  
  228.     $classes[] = $default_size;
  229.     $classes[] = $image_class;
  230.  
  231.     $class = join(' ', $classes);
  232.  
  233.     $image = '';
  234.  
  235.     if($format == 'array') :
  236.         $image = array(
  237.             'url' => $img,
  238.             'alt' => the_title_attribute('echo=0'),
  239.             'class' => $class,
  240.             'link' => get_permalink($post->ID),
  241.         );
  242.         return $image;
  243.     endif;
  244.  
  245.     if($link_to_post)
  246.         $image .= '<a href="' . get_permalink($post->ID) . '" title="' . the_title_attribute('echo=0') . '">';
  247.  
  248.     $image .= '<img src="' . $img . '" alt="' . the_title_attribute('echo=0') . '" class="' . $class . '"' . $width . $height . ' />';
  249.  
  250.     if($link_to_post)
  251.         $image .= '</a>';
  252.  
  253.     return $image;
  254. }
  255.  
  256. /**
  257. * Deprecated function needs to be replaced with get_the_image()
  258. *
  259. * @since 0.1
  260. * @deprecated 0.4
  261. */
  262. function get_the_image_link($deprecated = false, $deprecated_2 = false, $deprecated_3 = false) {
  263.     _e('The function has been deprecated. You need to update your template file calls to <code>get_the_image()</code>.','get_the_image');
  264. }
  265. ?>
Advertisement
Add Comment
Please, Sign In to add comment