WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I got a theme that has lightbox built in, but only for galleries and featured images, not single images.

It just needs to have class="lightbox" added to the images source. I'd like a simple function that will include this automatically.

Not sure why the author left this out.

share|improve this question
up vote 2 down vote accepted

One way you can accomplish it is this way:


add_action( 'wp_enqueue_scripts', 'your_lightbox_callback' );
function your_lightbox_callback(){

    if( is_single() /* or whatever here */ ) {
        wp_register_script( 'your-lightbox', get_bloginfo( 'template_url' ) . '/lightbox-class.js' );
        wp_enqueue_script( 'your-lightbox' );



var $j = jQuery.noConflict();
$j( document ).ready( function() {

    $j( '#whereveryourpostsare img' ).addClass( 'lightbox' );

} );

Let me add that this hasn't been tested, just typed here on the fly. If you try it do tell me if you have any issues, thanks!

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.