ID; /* Find internal image links */ // Check the page for link images direct to image (no trailing attributes) $regex_search = '/<\/a>/i'; preg_match_all($regex_search, $content, $matches, PREG_SET_ORDER); // Check which image is referenced foreach ($matches as $val) { $caption = ''; $image = get_post($val[5]); $caption = esc_attr( $image->post_content ); //Replace the instance with the lightbox and title(caption) references. Won't fail if caption is empty. $string = ''; $replace = ''; $content = str_replace($string, $replace, $content); } /* Find links created by [gallery] shortcode */ // Check the page for link images direct to image (no trailing attributes) $regex_search = '/<\/a>/i'; if (preg_match($regex_search, $content)) { $attachments = get_children( array('post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image') ); foreach ($attachments as $num => $attachment) { // Then re-arrange the link to include the caption and rel="lightbox" // Note quote usage: from [gallery] shortcode $caption = esc_attr( $attachment->post_content ); $pattern = "/<\/a>/i"; $replace = ''; $content = preg_replace($pattern, $replace, $content, 1); } } return $content; }