Assigning HTML “rel” attribute to all page images

Very briefly, in a CakePHP app, I want to apply the lightbox script on all the images that link from a thumbnail to a larger image. If there are more than two, I want them to appear as a lightbox group. I don’t want the user to mess with TinyMCE and HTML, therefore I automatically apply the rel attributed for every single image in a page that is a hyperlink. The following PHP bit is added in app/Plugin/Nodes/View/Nodes/view.ctp. With some small changes it can be used for any PHP compatible web site.

$body = $this->Nodes->body();
if (strpos($body, "img")) {
   $pattern ="/<a(.*?)href=('|\")(.*?).(jpeg|jpg|png)('|\")(.*?)>/i";
   $newLink = '<a$1href=$2$3.$4$5 rel="lightbox[group]".$6>';
   echo preg_replace($pattern, $newLink, $body);
} else {
   echo $body;
}

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s