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;
            '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');
  • 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 Oct 10 '13 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. – user2587741 Oct 10 '13 at 15:46

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

  function() {
    return array(1,2,3);

function print_partners_scripts() {
  $args = apply_filters('my_filter',null);
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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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