Created
January 18, 2019 16:58
-
-
Save adamcapriola/8528676aeb54c74bb304fb8c60473ae9 to your computer and use it in GitHub Desktop.
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