I am using the following php function (in wordpress) to add a data attribute to all image links:
function ccd_fancybox_image_attribute( $content ) {
global $post;
$pattern = "/<a(.*?)href=('|\")(.*?).(bmp|gif|jpeg|jpg|png)('|\")(.*?)>/i";
$replace = '<a$1href=$2$3.$4$5 data-type="image" data-fancybox="image">';
$content = preg_replace( $pattern, $replace, $content );
return $content;
}
add_filter( 'the_content', 'ccd_fancybox_image_attribute' );
This correctly changes a link like this:
<a href="image.jpg"><img src="image.jpg"></a>
to this:
<a href="image.jpg" data-type="image" data-fancybox="image"><img src="image.jpg"></a>
But this code is also incorrectly affecting images that link to PDF files, oddly adding the data attributes to the image itself, not the link. For example, a PDF link like this:
<a href="file.pdf"><img src="image.jpg"></a>
turns into this:
<a href="file.pdf"><img src="image.jpg" data-type="image" data-fancybox="image"></a>
I only barely understand regex, so I'm not sure how to adjust it. How do I fix the code so that it only applies to links to image files, not links to PDFs.
Thank you!