SHARE
TWEET

Untitled

a guest Jun 8th, 2012 173 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Add autmatically rel="prettyPhoto" attribut when you have one image in a post
  3.  * or rel="prettyPhoto[0]" attribut when you have more than one image in a post
  4.  */
  5. function sumtips_image_attribute($content) {
  6.         global $post;
  7.  
  8.         /* Search img tag or src attribut in the post content */
  9.         $regular_expression  = '/<a(.*)href="(.*)"(.*)><img(.*)src="(.*)><\/a>/';
  10.  
  11.         /* grab all the images from the post in an array $allpics using preg_match_all */
  12.         preg_match_all( $regular_expression, $post->post_content, $allpics );
  13.  
  14.         /* Count image in a post */
  15.         $nb = count($allpics[0]);
  16.  
  17.         /* Replace HTML attribut */
  18.         if ($nb == 1) {
  19.  
  20.                 /* If we have only one image in a post */
  21.                 $pattern ="/<a(.*?)href=('|\")(.*?).(bmp|gif|jpeg|jpg|png)('|\")(.*?)>/i";
  22.                 $replacement = '<a$1href=$2$3.$4$5 rel="prettyPhoto">';
  23.                 $content = preg_replace($pattern, $replacement, $content);
  24.                 return $content;
  25.  
  26.         } elseif ($nb > 2) {
  27.  
  28.                 /* If we have more than one image in a post */
  29.                 $pattern ="/<a(.*?)href=('|\")(.*?).(bmp|gif|jpeg|jpg|png)('|\")(.*?)>/i";
  30.                 $replacement = '<a$1href=$2$3.$4$5 rel="prettyPhoto[0]">';
  31.                 $content = preg_replace($pattern, $replacement, $content);
  32.                 return $content;
  33.         }
  34. }
  35. add_filter('the_content', 'sumtips_image_attribute');
RAW Paste Data
Top