Topic: WP plugin: embed juicebox in template using do_shortcode()

I need to display a juicebox gallery from a shortcode in a WordPress custom field. I uploaded my files through this custom field and that went fine and even displays properly if I put the shortcode in the default editor.

However, whenever I try and parse the contents of the custom field to display the gallery, I get the message "WP-Juicebox Gallery Id cannot be found."

Here's some example code that's not working:

<?php
// Get the slideshow's Juicebox shortcode
$juicebox = get_post_meta( $post->ID, 'slideshow_embed', true);
                                                
if ( $juicebox ) {
    // Render Juicebox shortcode inside div
    echo '<div class="slideshow-container">';
    echo do_shortcode( $juicebox );
    echo '</div>';
}
                                                
?>

I've also tried

echo apply_filters('the_content', get_post_meta($post->ID, 'slideshow_embed', true));

to no avail.

Any ideas?

2 (edited by montchr 2012-07-13 17:34:24)

Re: WP plugin: embed juicebox in template using do_shortcode()

Doesn't work. :-(

[Edit: OK, looks like you deleted your post...]

Re: WP plugin: embed juicebox in template using do_shortcode()

It would help greatly if I were able to see and use the code/plugin which contains the custom field you refer to.
Would you be able to zip the plugin (if it is a plugin) and upload it somewhere so that I could install it in my WordPress installation and try a few things in order to find a solution? Thanks.

[Edit: OK, looks like you deleted your post...]

Edit: Yes. I tried my suggestion myself and found that it produces PHP errors in debug mode, so I deleted it but you replied before I could post this message.

Re: WP plugin: embed juicebox in template using do_shortcode()

Sure. I used a premium plugin to generate the field. Is there any way I could send you the archive in a PM or email?

Re: WP plugin: embed juicebox in template using do_shortcode()

Nevermind, here: [Link deleted.]

Re: WP plugin: embed juicebox in template using do_shortcode()

File received, thank you. I've deleted your link above.
I'll take a look and get back to you in due course.

Re: WP plugin: embed juicebox in template using do_shortcode()

Thanks for your help!

Any chance the Juicebox plugin will use template tags in the future? It's a fantastic plugin but that would make it more fantastic.

Re: WP plugin: embed juicebox in template using do_shortcode()

It looks like the plugin you use is rather complex. Could you give me a quick rundown as to how you are using it and how I can replicate the problem? Thank you.
In the meantime, it might help you to know that the function that handles the shortcode within WP-Juicebox is called 'shortcode_handler' and it is within the class Juicebox defined in 'wp-juicebox.php'.
Perhaps if you called this function instead of 'do_shortcode', it might help.

Re: WP plugin: embed juicebox in template using do_shortcode()

Basically I'm using that plugin to create custom post types with archives, custom permalinks etc. It also lets you create custom fields and metaboxes. I'm not totally sure how it works but I'm referencing those custom fields other places in my theme and there's no problem. So I'd imagine they behave just like custom fields created in a theme.

I tried your suggestion but that's not working either. It gives the same response.

Re: WP plugin: embed juicebox in template using do_shortcode()

For now, I'm using a workaround by setting up the gallery in the main post editor and writing the_content() in my template where I want the gallery to appear.

However, the one big problem with this is that because the size of the gallery is set based on the original place it is displayed — I.E. the single post template — the same gallery can't be displayed in a smaller div container elsewhere in the template without its height extending beyond the bounds of the div. It tries to maintain the original size. Setting it to 100% and defining the height of the second div container does nothing because it still tries to respect the 100% of the original div on the single post. Which isn't exactly responsive.

Re: WP plugin: embed juicebox in template using do_shortcode()

Are you trying to embed a Juicebox gallery in a theme's header?
If so (and you know the gallery id you wish to display), you should be able to use the following code in your theme's 'header.php' file.

<?php
    $JB = new Juicebox();
    echo $JB -> shortcode_handler( array( 'gallery_id '=> '2' ) );
?>

... changing the '2' above to the gallery id of your choice.
Alternatively, the following should also work:

<?php
    echo do_shortcode('[juicebox gallery_id="2"]');
?>

As you have discovered, the gallery's dimensions are tied to the gallery (being stored in the gallery's XML file) and a single gallery cannot be displayed twice with different dimensions each time. In fact, the same gallery should not be displayed twice on the same page because both galleries will create divs with identical names to be embedded into.
I would recommend creating a separate gallery for your theme and, if necessary, duplicating the gallery (with different dimensions) for use elsewhere.

The problem with your original code seems to be the extraction of the gallery id from your custom field as entering the code above (with the hardcoded gallery ids) seems to work fine. However, I would need to know more about the custom field you are using to help with the extraction process.

What is output if you echo $juicebox after defining it on this line:

$juicebox = get_post_meta( $post->ID, 'slideshow_embed', true);

For your subsequent code to work, the output would have to be something like: [juicebox gallery_id="2"]