Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Add autmatically rel="prettyPhoto" attribut when you have one image in a post
- * or rel="prettyPhoto[0]" attribut when you have more than one image in a post
- */
- function sumtips_image_attribute($content) {
- global $post;
- /* Search img tag or src attribut in the post content */
- $regular_expression = '/<a(.*)href="(.*)"(.*)><img(.*)src="(.*)><\/a>/';
- /* grab all the images from the post in an array $allpics using preg_match_all */
- preg_match_all( $regular_expression, $post->post_content, $allpics );
- /* Count image in a post */
- $nb = count($allpics[0]);
- /* Replace HTML attribut */
- if ($nb == 1) {
- /* If we have only one image in a post */
- $pattern ="/<a(.*?)href=('|\")(.*?).(bmp|gif|jpeg|jpg|png)('|\")(.*?)>/i";
- $replacement = '<a$1href=$2$3.$4$5 rel="prettyPhoto">';
- $content = preg_replace($pattern, $replacement, $content);
- return $content;
- } elseif ($nb > 2) {
- /* If we have more than one image in a post */
- $pattern ="/<a(.*?)href=('|\")(.*?).(bmp|gif|jpeg|jpg|png)('|\")(.*?)>/i";
- $replacement = '<a$1href=$2$3.$4$5 rel="prettyPhoto[0]">';
- $content = preg_replace($pattern, $replacement, $content);
- return $content;
- }
- }
- add_filter('the_content', 'sumtips_image_attribute');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement