Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying to do a shortcode which will add additional jQuery script to wp_footer.

So this gonna be an example shortcode:

function partners($atts ) {
    global $extra_options, $pslider;
    extract(shortcode_atts(array(  
            'ids' => null,
            'extra_options' => null
        ), $atts));  

    $pslider = 'partners-slider'.rand(100,999);
    $ids = explode( ',', $ids );
    $output = '<div class="section row-one"><div class="col ps-gradient"><div class="ps-wrap"><div class="'.$pslider.'">';
    foreach($ids as $id) {    
    $img_attr = wp_get_attachment_image_src( $id, 'full' );
    $alt_text = get_post_meta( $id, '_wp_attachment_image_alt', true);
    $output .= '<div class="pslide"><a href="' . $alt_text . '"><img src="' . $img_attr[0] . '" /></a></div>';    
    }
    $output .= '</div></div></div></div>';
    return $output;  
}  

add_shortcode('partners', 'partners');

From the above function I need to get variables and place them in a jQuery script:

function print_partners_scripts() {
    global $extra_options, $pslider;
    echo '<script type="text/javascript">' . "\n";
    echo 'jQuery(document).ready( function() {'. "\n";
    echo '  $(".'.$pslider.'").bxSlider({
        slideWidth: 924,
        auto: 1,
        autoStart: 1,
        moveSlides: 1,
        minSlides: 7,
        maxSlides: 8,
        pager: false,
        controls: false,
        slideMargin: 5,
        autoHover: true
        ' . $extra_options . '
        });' . "\n";
    echo '});' . "\n";
    echo '</script>' . "\n";
}

add_action( 'wp_footer', 'print_partners_scripts' );

Ok, at the moment I have a shortcode which can return working slider.

But, what if I try to use this shortcode again on the same page? I've checked, the jQuery script will append only once.

I would like to generate this jQuery script for every [partners] shortcode.

For example:

[partners extra_options="something: true"]
[partners extra_options="something: false"]

First one should add this script to wp_footer:

<script type="text/javascript">
jQuery(document).ready( function() {
    $(".pslider342").bxSlider({
        slideWidth: 924,
        auto: 1,
        autoStart: 1,
        moveSlides: 1,
        minSlides: 7,
        maxSlides: 8,
        pager: false,
        controls: false,
        slideMargin: 5,
        autoHover: true,
        extra_options: true
    });
});
</script>

It worked.
The second one should add the same script with different variables:

<script type="text/javascript">
jQuery(document).ready( function() {
        $(".pslider456").bxSlider({
        slideWidth: 924,
        auto: 1,
        autoStart: 1,
        moveSlides: 1,
        minSlides: 7,
        maxSlides: 8,
        pager: false,
        controls: false,
        slideMargin: 5,
        autoHover: true,
        extra_options: false
    });
});
</script>

Unfortunately this one was not added.

Really need your help!

share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

The issue is that each time a shortcode is processed, you're overwriting the previous value with the new value, so you'll always only get the data for the last instance of your shortcode.

The simplest way to work within what you've already got is to put the data into an array.

global $pslider;
// generate a unique identifier
$unique_id = uniqid();
// save your data under that unique key
$pslider[$unique_id] = 'some data';
// use that unique ID in your markup
$output = '<div id="partners-slider-' . $unique_id . '">';


// then later in your footer funtion,
// loop over each instance and output your js
global $pslider;
foreach( $pslider as $id => $value ):
    echo 'unique ID: ' . $id;
    echo 'value: ' . $value;
endforeach;

Personally, I avoid global vars like the plague. I would wrap this whole thing in a class and use a class var to store your data.

share|improve this answer
    
You're awesome. Thank you ;) –  user2537093 Oct 17 '13 at 1:33
add comment

Your Answer

 
discard

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.