Snippets: Automatically Add Lightbox To Images In Blog Posts

IMPORTANT: Snippet no longer required, a new option has been added to the customizer - http://wpexplorer-themes.com/total/docs/enable-auto-lightbox-blog-post-images/

// Add javascript for lightbox
function my_auto_blog_img_lightbox() { ?>

    <script>
        (function($) {
        'use strict';
            // Auto add lightbox to entry images
            $( '.single-blog-article .entry a:has(img)' ).each( function() {
                var $this   = $(this),
                    $img    = $this.find( 'img' ),
                    $src    = $img.attr( 'src' ),
                    $ref    = $this.attr( 'href' );
                if ( $src == $ref ) {
                    $this.iLightBox( {
                        skin        : wpexLocalize.lightboxSkin,
                    } );
                }
            } );
        } ) ( jQuery );
    </script>

<?php }
// Priority must be high to make sure it's added after wpexLocalize
add_filter( 'wp_footer', 'my_auto_blog_img_lightbox', 9999 );

// Load correct lightbox skin
function my_child_enqueue_iLightbox_skin() {

    if ( is_singular( 'post' ) ) {
        wpex_enqueue_ilightbox_skin();
    }
}
add_action( 'wp_footer', 'my_child_enqueue_iLightbox_skin', 10, 1 );

Snippet Type: PHP

Posted Under: Lightbox

All PHP snippets should be added via a child theme's functions.php file.