0

I have here a shortcode which generate a slider. Everything works just fine until i try to add another "partners" slider with different options. I can't specific settings for each slider because the print_partners_scripts() function is generated only once. I need to initialize print_partners_scripts() function for every slider with specified in shortcode options. I hope you understand what i mean ;)

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";
}

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

    add_action( 'wp_footer', 'print_partners_scripts' );
    $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');
2
  • your title is a bit confusing, not sure what it has to do with your actual question. that said, this isn't really a WordPress issue. you're targeting each slider instance with a general class selector- ".partners-slider". give each of your sliders a unique ID and target that instead.
    – Milo
    Commented Oct 10, 2013 at 15:37
  • It's generating a unique ID now but not working as well. The print_partners_scripts() function print js code only once. Commented Oct 10, 2013 at 15:46

1 Answer 1

0

You can pass values by adding your own filter. Proof of concept:

add_filter(
  'my_filter',
  function() {
    return array(1,2,3);
  }
);

function print_partners_scripts() {
  $args = apply_filters('my_filter',null);
  var_dump($args);
}
add_action( 'wp_footer', 'print_partners_scripts' );

Where you to register and enqueue your script instead of echoing it directly to the page, you could use wp_localize-script to pass information to it.

3
  • That sounds good ;) Gonna check this soon, Big Thanks ! Any alternative will be really appreciated, just for my knowledge ;) Commented Oct 10, 2013 at 16:06
  • I want to set options in shortcode. Is this gonna work with add_filter? Commented Oct 10, 2013 at 16:53
  • It should. Your shortcode should pocesss before the footer.
    – s_ha_dum
    Commented Oct 10, 2013 at 17:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.