Posted By

FXDB on 11/29/11


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite


Add rel=”lightbox” to all images embedded in a post

 / Published in: PHP

Adding this snippet to the functions.php of your wordpress theme will add a rel=”lightbox” attribute to all images embedded in your post. Also note that on line 05 you can change the rel=”lightbox” to whatever you need as some image viewer scripts will use rel=”thumbnail” for example. This snippet will also add the post title as the title attribute of the images anchor tag.

  1. add_filter('the_content', 'my_addlightboxrel');
  2. function my_addlightboxrel($content) {
  3. global $post;
  4. $pattern ="/<a(.*?)href=('|\")(.*?).(bmp|gif|jpeg|jpg|png)('|\")(.*?)>/i";
  5. $replacement = '<a$1href=$2$3.$4$5 rel="lightbox" title="'.$post->post_title.'"$6>';
  6. $content = preg_replace($pattern, $replacement, $content);
  7. return $content;
  8. }

Report this snippet  

You need to login to post a comment.