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
(@zhart)
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?
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
(@zhart)
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.
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
(@zhart)
Bartosz, ok, many thanks again π
How about if the image is inserted in a wysiwyg editor from ACF?
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' );
Thank you, I’m gonna give it a try tomorrow.
Unfortunately something’s not working. I’ve added the filter you provided, but the images still open as if the script’s not active.