Last active
May 1, 2024 12:46
-
-
Save thekendog/9086cf4afb22480ce39326186777886e to your computer and use it in GitHub Desktop.
Use Kadence lightbox for Blocksy post featured image
This file contains hidden or 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
/** | |
* Enqueue the Kadence lightbox styles and scripts | |
*/ | |
add_action('wp_enqueue_scripts', function() { | |
if ( !is_plugin_active( 'kadence-blocks/kadence-blocks.php' ) ) { | |
return; | |
} | |
if ( is_singular('post') ) { | |
wp_enqueue_style('kadence-glightbox', KADENCE_BLOCKS_URL . 'includes/assets/css/kb-glightbox.min.css', array(), KADENCE_BLOCKS_VERSION); | |
wp_enqueue_script('kadence-glightbox', KADENCE_BLOCKS_URL . 'includes/assets/js/glightbox.min.js', array(), KADENCE_BLOCKS_VERSION, true); | |
wp_add_inline_script('kadence-glightbox', ' | |
window.addEventListener("DOMContentLoaded",function () { | |
GLightbox({ | |
skin: "kadence-dark", | |
openEffect: "fade", | |
closeEffect: "fade" | |
}); | |
}); | |
'); | |
} | |
}); | |
/** | |
* Make single post featured images a clickable lightbox using Kadence lightbox | |
*/ | |
add_filter('post_thumbnail_html', function($html, $post_id, $post_thumbnail_id, $size, $attr) { | |
if ( !is_plugin_active( 'kadence-blocks/kadence-blocks.php' ) ) { | |
return; | |
} | |
if ( is_singular('post') ) { | |
$link = wp_get_attachment_image_url($post_thumbnail_id, 'full'); | |
$html = preg_replace('/(<img([^>]*)>)/i', '<a href="' . $link . '" class="glightbox" aria-label="Display this image in a lightbox">$1</a>', $html); | |
} | |
return $html; | |
}, 20, 5); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment