function wpcp_hard_lightbox_fix() {
?>
<script>
(function() {
function openWPCPLightbox(el) {
const gallery = el.getAttribute('data-lightbox-gallery');
const items = Array.from(
document.querySelectorAll('[data-lightbox-gallery="' + gallery + '"]')
).map(function(a) {
return {
src: a.getAttribute('href'),
caption: a.getAttribute('data-caption') || ''
};
});
const index = Array.from(
document.querySelectorAll('[data-lightbox-gallery="' + gallery + '"]')
).indexOf(el);
// Try Fancybox v4
if (window.Fancybox) {
Fancybox.show(items, {
startIndex: index,
loop: true
});
return;
}
// Try jQuery Fancybox
if (window.jQuery && jQuery.fancybox) {
jQuery.fancybox.open(items, {
loop: true
}, index);
return;
}
// fallback
window.open(el.href, '_blank');
}
// CAPTURE PHASE listener (KEY FIX)
document.addEventListener('click', function(e) {
const el = e.target.closest('a.wcp-light-box');
if (!el) return;
e.preventDefault();
e.stopPropagation();
e.stopImmediatePropagation();
openWPCPLightbox(el);
return false;
}, true); // 👈 IMPORTANT: capture phase = true
})();
</script>
<?php
}
add_action('wp_footer', 'wpcp_hard_lightbox_fix', 99999);
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter