Fix for auto-lightboxing links that contain rel attributes already
-
In wp-jquery-lightbox.php, update the following function as so–
function jqlb_do_regexp($content, $id){ $id = esc_attr($id); $a_tag_img_regex = "/(<a[^>]+href=['\"][^>]+\\.(?:bmp|gif|jpg|jpeg|png)[^>]+)>/i"; if (preg_match_all($a_tag_img_regex, $content, $a_tag_matches, PREG_SET_ORDER)) { foreach ($a_tag_matches as $a_tag) { $new_a_tag = $a_tag[0]; $rel_regex = "/(rel=['\"])(?![^>]*?(?:lightbox|nolb|nobox))([^'\"]+)(['\"])/i"; $new_a_tag = preg_replace($rel_regex, '$1lightbox['.$id.'] $2$3', $new_a_tag); $no_rel_regex = "/(<a(?![^>]*?rel=['\"].+)[^>]+href=['\"][^>]+\\.(?:bmp|gif|jpg|jpeg|png)[^>]+)>/i"; $new_a_tag = preg_replace($no_rel_regex, '$1 rel="lightbox['.$id.']">', $new_a_tag); if ($new_a_tag != $a_tag[0]) $content = str_replace($a_tag[0], $new_a_tag, $content); } } return $content; }
As it currently stands, the previous code would add a second rel tag even if one already existed, which would cause the lightboxing to fail.
My updated code will add a rel=lightbox tag if there is no rel attribute at all. If there is a rel attribute, it will add lightbox[##] to it only if there is no “lightbox” text within the attribute value.
Couple restrictions I noticed along the way–
1) Your jquery selector (
a[rel^="lightbox"]
) forces the lightbox text to be the first value in the rel tag’s values. e.g. rel=”something lightbox[123]” does not work but rel=”lightbox[123] something” does. The selector would need to be changed to probably some sort of regex to properly select everything.2) More importantly, your “grouping” of lightboxed images only works if the rel tags are exactly the same. In
function start(imageLink)
you haveif(!this.href || (this.rel != imageLink.rel)) {
. This should be expanded upon to search for the same “lightbox[##]” text within each link. I didn’t have the motivation to do it myself right now as I don’t have a need for it at this moment.edit: added support for checking for “nobox” or “nolb” in the rel tag to override automatic lightboxing. “nolightbox” or anything with “lightbox” in the word will also override it.
- The topic ‘Fix for auto-lightboxing links that contain rel attributes already’ is closed to new replies.