Fancybox Data Attributes to Images & Galleries
WordPress Snippet
<?php | |
/** | |
* Add data attributes for Fancybox | |
*/ | |
// Gallery images | |
function ccd_fancybox_gallery_attribute( $content, $id ) { | |
// Restore title attribute | |
$title = get_the_title( $id ); | |
return str_replace('<a', '<a data-type="image" data-fancybox="gallery" title="' . esc_attr( $title ) . '" ', $content); | |
} | |
add_filter( 'wp_get_attachment_link', 'ccd_fancybox_gallery_attribute', 10, 4 ); | |
// Single images | |
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' ); | |
?> |
Thanks! Just filled my needs.
This got me to where I needed to go too. Thank you!
Thanks a lot man.
hi
im using this function and its very very very useful for installing fancybox in wp themes .
very thanks my friend.