Title: Lightbox and Custom Fields
Last modified: August 30, 2016

---

# Lightbox and Custom Fields

 *  Resolved [zhart](https://wordpress.org/support/users/zhart/)
 * (@zhart)
 * [10 years, 7 months ago](https://wordpress.org/support/topic/lightbox-and-custom-fields/)
 * Hello, and thank you for a wonderful plugin.
 * All my pictures and galleries are displayed via custom fields. (I use the ACF
   plugin to create custom fields.)
 * I really like this option in the Lightbox plugin settings:
    “Enable to load scripts
   and styles only on pages that have images or galleries in post content.” Unfortunately,
   this option does not work with custom fields. This option does not recognize 
   the images that are published in the custom fields. Is there a way to make this
   option to work with custom fields?
 * [https://wordpress.org/plugins/responsive-lightbox/](https://wordpress.org/plugins/responsive-lightbox/)

Viewing 10 replies - 1 through 10 (of 10 total)

 *  Plugin Author [dFactory](https://wordpress.org/support/users/dfactory/)
 * (@dfactory)
 * [10 years, 7 months ago](https://wordpress.org/support/topic/lightbox-and-custom-fields/#post-6685295)
 * Thx,
 * Unfortunatelly not – custom fields are not controllable via filter hooks so we
   can’t detect that. This feature is for post content only.
 *  Thread Starter [zhart](https://wordpress.org/support/users/zhart/)
 * (@zhart)
 * [10 years, 7 months ago](https://wordpress.org/support/topic/lightbox-and-custom-fields/#post-6685333)
 * It’s a pity.
    But I suppose that in that case I can go the other way. Custom 
   fields are processed in the template. So I can somehow change the code that outputs
   images and galleries. Can I add loading of necessary scripts directly in this
   code?
 * For example I have such a conditional statement in the code:
 *     ```
       <?php
       if( get_field('image_field') ) :
       $image_output = get_field('image_field');
           echo '<img src="' . $image_output['url'] . '" alt="' . $image_output['alt'] . '" />';
       endif;
       ?>
       ```
   
 * Can I add loading of necessary scripts directly in this conditional statement?
   
   In this case, the scripts will be loaded only on those pages where there are 
   images. If this is possible, how to do it most correctly?
 *  Plugin Author [dFactory](https://wordpress.org/support/users/dfactory/)
 * (@dfactory)
 * [10 years, 7 months ago](https://wordpress.org/support/topic/lightbox-and-custom-fields/#post-6685355)
 * Hmm… you could try that with a filter hook available in the plugin:
 *     ```
       function custom_rl_lightbox_conditional_loading( $load ) {
       	if ( function_exists( 'get_field' ) ) {
       		if ( get_field( 'image_field', get_the_ID() ) ) {
       			$load = true;
       		}
       	}
       	return $load;
       }
       add_filter( 'rl_lightbox_conditional_loading', 'custom_rl_lightbox_conditional_loading' );
       ```
   
 *  Thread Starter [zhart](https://wordpress.org/support/users/zhart/)
 * (@zhart)
 * [10 years, 7 months ago](https://wordpress.org/support/topic/lightbox-and-custom-fields/#post-6685398)
 * Thank you very much, it works perfectly.
    However, I use custom fields with a
   nested structure, so I modified the code slightly and add it to the file functions.
   php in my theme:
 *     ```
       function custom_rl_lightbox_conditional_loading( $load ) {
           if ( function_exists( 'have_rows' ) ) {
               if ( have_rows( 'flexible_content', get_the_ID() ) ) {
                   while ( have_rows('flexible_content') ) : the_row();
                   if( get_row_layout() == 'image' ) {
                   $load = true; }
                   elseif( get_row_layout() == 'gallery' ) {
                   $load = true; }
                   endwhile;
               }
           }
           return $load;
       }
       add_filter( 'rl_lightbox_conditional_loading', 'custom_rl_lightbox_conditional_loading' );
       ```
   
 * This code works for me correctly, but if it can be optimized, I will be grateful
   to you for tips.
 *  Plugin Author [dFactory](https://wordpress.org/support/users/dfactory/)
 * (@dfactory)
 * [10 years, 7 months ago](https://wordpress.org/support/topic/lightbox-and-custom-fields/#post-6685401)
 * Your code is ok – that’s what this filter is for – to adjust it to your specific
   needs.
 * Regards,
    Bartosz / dfactory team
 *  Thread Starter [zhart](https://wordpress.org/support/users/zhart/)
 * (@zhart)
 * [10 years, 7 months ago](https://wordpress.org/support/topic/lightbox-and-custom-fields/#post-6685405)
 * **Bartosz**, ok, many thanks again 🙂
 *  [reddo](https://wordpress.org/support/users/reddo/)
 * (@reddo)
 * [10 years, 6 months ago](https://wordpress.org/support/topic/lightbox-and-custom-fields/#post-6685561)
 * How about if the image is inserted in a wysiwyg editor from ACF?
 *  Plugin Author [dFactory](https://wordpress.org/support/users/dfactory/)
 * (@dfactory)
 * [10 years, 6 months ago](https://wordpress.org/support/topic/lightbox-and-custom-fields/#post-6685562)
 * Hmmm… pretty similar to the one I posted above with the difference that you have
   to check if the content from wysiwyg editor contains images or a gallery.
 * Not tested but should work:
 *     ```
       function custom_rl_lightbox_conditional_loading( $load ) {
       	if ( function_exists( 'get_field' ) ) {
   
       		// get custom field content, use your meta key here
       		$content = get_field( 'wysiwyg_field', get_the_ID() );
   
       		if ( ! empty( $content ) ) {
   
       			// content contains a gallery?
       			$has_gallery = has_shortcode( $content, 'gallery' );
   
       			// content contains any images?
       			preg_match_all( '/<a(.*?)href=(?:\'|")([^<]*?).(bmp|gif|jpeg|jpg|png)(?:\'|")(.*?)>/i', $content, $links );
   
       			$has_images = (bool) $links[0];
   
       			// load if needed
       			if ( $has_gallery === true || $has_images === true ) {
       				$load = true;
       			}
       		}
       	}
       	return $load;
       }
       add_filter( 'rl_lightbox_conditional_loading', 'custom_rl_lightbox_conditional_loading' );
       ```
   
 *  [reddo](https://wordpress.org/support/users/reddo/)
 * (@reddo)
 * [10 years, 6 months ago](https://wordpress.org/support/topic/lightbox-and-custom-fields/#post-6685563)
 * Thank you, I’m gonna give it a try tomorrow.
 *  [reddo](https://wordpress.org/support/users/reddo/)
 * (@reddo)
 * [10 years, 6 months ago](https://wordpress.org/support/topic/lightbox-and-custom-fields/#post-6685564)
 * Unfortunately something’s not working. I’ve added the filter you provided, but
   the images still open as if the script’s not active.

Viewing 10 replies - 1 through 10 (of 10 total)

The topic ‘Lightbox and Custom Fields’ is closed to new replies.

 * ![](https://ps.w.org/responsive-lightbox/assets/icon-256x256.png?rev=3460874)
 * [Responsive Lightbox & Gallery](https://wordpress.org/plugins/responsive-lightbox/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/responsive-lightbox/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/responsive-lightbox/)
 * [Active Topics](https://wordpress.org/support/plugin/responsive-lightbox/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/responsive-lightbox/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/responsive-lightbox/reviews/)

## Tags

 * [custom fields](https://wordpress.org/support/topic-tag/custom-fields/)

 * 10 replies
 * 3 participants
 * Last reply from: [reddo](https://wordpress.org/support/users/reddo/)
 * Last activity: [10 years, 6 months ago](https://wordpress.org/support/topic/lightbox-and-custom-fields/#post-6685564)
 * Status: resolved