• Resolved mfal55

    (@mfal55)


    Can you tell me if it’s possible to use SLB with the Advanced Custom Fields plugin? I’ve created templates where I show content that’s outside of the content block and so the light box will not trigger. If it is possible, can you direct me to instructions on how to implement this? Thanks so much.

    https://wordpress.org/plugins/simple-lightbox/

Viewing 15 replies - 1 through 15 (of 15 total)
  • Plugin Author Archetyped

    (@archetyped)

    SLB automatically activates links in post/page/widget content. Additional content areas can be activated via add-ons. Manual activation is also possible using the slb_activate() template tag.

    See here for more information on using slb_activate().

    Thread Starter mfal55

    (@mfal55)

    Thanks Archetyped – I’m not sure how to manually activate it along side a call to my custom field. I think perhaps I would need to create a filter, but am unsure of how to do this? To display the content which lives in the custom field, I use this snippet of code:

    <?php the_field('my_custom_content'); ?>

    Where and how would I inject the slb_activate() tag to trigger the links to open up in a lightbox? There aren’t really any examples like this available on your site or that I’ve run across online.

    Thanks again for your help here.

    Plugin Author Archetyped

    (@archetyped)

    Simple!

    You pass any content containing links you want to activate by SLB to slb_activate(). How do you pass content (i.e. data) around in PHP?

    Variables.

    So, what you need is a way to *get* a *field’s* data from ACV into a variable…

    You got it, get_field()!

    get_field() let’s you retrieve ACV field data and save it to a variable. You pass variables containing links you want to activate to slb_activate().

    πŸ™‚

    Thread Starter mfal55

    (@mfal55)

    thanks so much Archetyped! So I’ve got it running successfully on the standard tag, but I need just a little more assistance regarding something a tad more complex. I’m missing something, that I can’t pinpoint. I have a custom field for a gallery, and I want to trigger the lightbox on it. I’m going to paste the original code that I use to display the gallery, which without any further customization merely opens up to an attachment page for now. The second set of code is me attempting to implement the lightbox. Is there anyway you can cue me in on what I’m doing wrong? I’m guessing it’s some basic php issue, but I just can’t see it.

    original code:

    <?php 
    
    $image_ids = get_field('program_photo_gallery', false, false);
    
    $shortcode = '
    [gallery ids="' . implode(',', $image_ids) . '"]
    ';
    
    echo do_shortcode( $shortcode );
    
    ?>

    edited code to include SLB:

    <?php
    	$content = $image_ids;
    	$image_ids = get_field('program_photo_gallery', false, false);
    	if ( function_exists('slb_activate') )
    	    $content = slb_activate($content);
    	    $shortcode = '
    		[gallery ids="' . implode(',', $image_ids) . '"]
    		';
    	echo do_shortcode( $shortcode );
    ?>

    It doesn’t break the page, but also doesn’t trigger the lightbox… any help you can give is greatly appreciated.

    Thanks!

    Plugin Author Archetyped

    (@archetyped)

    What kind of value are you passing to slb_activate() from get_field()? (Hint: slb_activate() activates links in text content)

    Thread Starter mfal55

    (@mfal55)

    I think I’m confused when you say it activates links in text content. I was able to activate the link of a thumbnail image in another field just fine. So for example, I was able to do this and it triggered the lightbox on the images in this custom content field.

    <?php
       	$content = get_field('sidebar_content');
    	if ( function_exists('slb_activate') )
    	$content = slb_activate($content);
    	echo $content;
     ?>

    But trying to inject the slb_activate() into my gallery code, I just can not get it to work and am at a loss as to how to figure it out.

    Thread Starter mfal55

    (@mfal55)

    Got it working from a thread I found on the ACF website. The solution is to add the apply_filters with the_content along with the shortcode. So incase anyone is trying to use SLB with and Advanced Custom Fields Gallery, this should work.

    <?php 
    
    $image_ids = get_field('my_custom_gallery_field', false, false);
    
    $shortcode = '
    [gallery ids="' . implode(',', $image_ids) . '"]
    ';
    
    echo apply_filters('the_content', $shortcode);
    
    ?>
    Plugin Author Archetyped

    (@archetyped)

    Hi, sorry for the confusion.

    When I say “text content”, I mean string.

    Your code:

    $image_ids = get_field('program_photo_gallery', false, false);
    

    This code appears to be returning an array, not a string. However, the code:

    $content = get_field('sidebar_content');
    

    This code returns a string (HTML for the image to be displayed), which is why you can pass it directly to slb_activate(). It is rendered as a thumbnail image in the browser, but it is HTML text (a string) when returned from get_field().

    Using apply_filters('the_content',...) is the old manual way of activating links with SLB. So instead of:

    echo apply_filters('the_content', $shortcode);
    

    You could simply do:

    echo slb_activate($shortcode);
    

    Why does slb_activate() work here? Let’ step through your code:

    1. Retrieve image IDs from ACF (result: array value).

    $image_ids = get_field('my_custom_gallery_field', false, false);
    

    SLB acts upon links in a string. All we currently have is an array.

    2. Build shortcode using image ids (result: string value)

    $shortcode = '[gallery ids="' . implode(',', $image_ids) . '"]';
    

    We now have a string value, but it only contains a shortcode, no links for SLB to act upon.

    3. Render shortcode into HTML for image gallery (result: string value)

    $gallery = do_shortcode( $shortcode );
    

    Now we have the HTML for the gallery including links. We now have links to activate with SLB.

    4. Activate links with SLB and output to browser

    echo slb_activate($gallery);
    

    Hopefully that makes sense now. The only reason I didn’t spell it out from the beginning is because you were doing such a great job with the code you already had and you were so close! You just needed a nudge in the right direction.

    To be clear: The best way to activate non-standard content going forward is using slb_activate(), not apply_filters(). slb_activate() is faster and more efficient.

    Thread Starter mfal55

    (@mfal55)

    Wow – thanks so much Archetype – I really appreciate you taking the time to explain the breakdown on that. I’d rather understand the reason as opposed to just applying the end result. So thank you very very much. I hope this post is helpful to others using your excellent lightbox and the Advanced Custom Fields plugin.

    The final output is this:

    <?php
    $image_ids = get_field('my_custom_gallery_field', false, false);
    $shortcode = '
    [gallery ids="' . implode(',', $image_ids) . '"]
    ';
    $gallery = do_shortcode( $shortcode );
    echo slb_activate($gallery);
    ?>

    Plugin Author Archetyped

    (@archetyped)

    Perfect!

    Hi Archetyped, I used the solution above, and it’s working perfectly but the single link launch trick described here doesn’t seem to work.
    All the pictures of the gallery are displayed.
    I tried to hide the images and displaying only the first image using img:firstchild in includes/media.php without success.
    Is there a fix for that ?

    Plugin Author Archetyped

    (@archetyped)

    @ol23: Please provide a link to a page that exhibits the issue you are experiencing and I would be glad to take a look.

    I thought I had found a fix, but actually it did not work.. Now working on localhost, I will upload it later. THanks for your help

    Thanks, Archetyped. Didn’t realize I needed the get_sub_field('sub_field_alias'); and now it’s working.

    For reference to others trying to get a Repeater sub_field to be parsed with slb_activate use:

    <?php $subfield_variable = get_sub_field('sub_field_alias');
    if ( function_exists('slb_activate') )
    $subfield_variable = slb_activate($subfield_variable);
    echo $subfield_variable; ?>

    Cheers

    Plugin Author Archetyped

    (@archetyped)

    Nice @rickahontas! Yes, you need to pass a value to slb_activate(), so get_sub_field() (returns a value) should be used as opposed to the_sub_field)() (prints a value to the screen like all of WordPress’ own the_...() template tags).

Viewing 15 replies - 1 through 15 (of 15 total)
  • The topic ‘Use with Advanced Custom Fields’ is closed to new replies.