Add Fancybox attribute to image links
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Fancybox image links | |
* @link http://fancyapps.com/fancybox/3/docs/#usage | |
* | |
*/ | |
add_filter( 'the_content', 'ac_the_content_fancybox_image_links', 10 ); // Adjust priorities if necessary | |
add_filter( 'widget_text', 'ac_the_content_fancybox_image_links', 10 ); | |
function ac_the_content_fancybox_image_links( $content ) { | |
$attribute = 'data-fancybox'; | |
$group = '="group"'; // Omit if grouping undesired | |
$search = '/<a href="([^"]+)\.(jpe?g|png|gif)"/'; | |
$replace = '<a href="$1.$2" ' . esc_attr( $attribute . $group ); | |
$content = preg_replace( $search, $replace, $content ); | |
return $content; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment