0

I am building a custom image slider.

I have a custom post type called 'slide' with an associated custom taxonomy called 'slideshows'.

When you create a new 'slideshow' it add to a custom table in the DB called wp_slideshowsmeta with columns for "meta_id, slideshows_id, meta_key, and meta_value".

Each slideshow has its own settings page using 'slideshows_add_form_fields' and 'slideshows_edit_form_fields'.

To call a slideshow I created a function ( 'gps_slider()' ) that is placed in the template file where you want the slideshow to appear. It has one parameter which is the slideshow name.

Everything works great... it loads the appropriate slideshow and functions. BUT, all the slideshows use the first one's settings.

I need to pass the slideshow name to my function that uses 'wp_localize_script' to pass the settings to the javascript.

I'm guessing it's likely a foreach loop, but can't seem to see how to construct it.

Any help is greatly appreciated.

Bryce

2
  • show us your code as it is right now.
    – Milo
    Commented Jan 13, 2014 at 21:36
  • Yes, of course... here is a pastebin pastebin.com/hFp8vxJx
    – jedifunk
    Commented Jan 13, 2014 at 21:49

2 Answers 2

0

The structure of this is kind of weird. But your main issue is that the wp_enqueue_scripts action runs before any calls to gps_slider() can possibly happen. Hook your script localization to the wp_footer action instead, so the slideshow data in the page already exists and has been output. Additionally you'll have to make your slideshow data an array of arrays so you can pass the settings for each individually.

$data = array();
$data[$name] = array(
    'gpsslidermode' => $slider_mode,
    'gpssliderspeed' => $slider_speed
);
wp_localize_script('gps-slider_script', 'gpsslidersettings', $data);

Also, why not just use the options table for your slideshow data, keyed to the slideshow ID? You don't seem to be doing anything with your custom table that couldn't be replicated with a built in core table, except creating more work for yourself. At the very least just save all of your slideshow meta in a single row in a serialized array, and cut 8 database queries from each slideshow load.

EDIT-

function gps_slider( $slideshow = '' ) {
    include('lib/gps-slider-output.php');
    global $slideshows;
    $slideshows[$slideshow] = array(
        // fetch and insert your settings data here
        'gpsslidermode' => $slider_mode,
        'gpssliderspeed' => $slider_speed,
        // etc..
    );
}

function gps_slider_localize(){
    global $slideshows;
    wp_localize_script('gps-slider_script', 'gpsslidersettings', $slideshows);
}
add_action( 'wp_footer', 'gps_slider_localize' );
3
  • Milo - Thanks, this helps for sure. However, now if I have 2 slideshows on the same page it only pulls the data for the last loaded slideshow. I've been messing with loops to try to get it to output both slideshows data, but don't seem to be getting it right. Concurrently, putting the data into an array of arrays breaks the localization, so the JS isn't getting the settings the way it suspects now.
    – jedifunk
    Commented Jan 14, 2014 at 17:20
  • I imagine it's because you're still just setting a single var, which also needs to be an array. See the edit above for a simplified version. Keep all of the data specific to each slider within the function that outputs it, then pass that array to localization. You'll also need to modify your javascript to then iterate over the javascript settings array to create each instance of the slider.
    – Milo
    Commented Jan 14, 2014 at 17:54
  • That did it... now I've just got to understand the JS enough to iterate for each slider.
    – jedifunk
    Commented Jan 14, 2014 at 21:18
0

I fear it may be because on line 8 of your pastebin, inside the get_term_by function you have hard coded 'slider95' instead of using the variable $name that you pass over into your function.

All its ever going to do is get the details for just slider95.

2
  • This is correct... this is hard coded at the moment... but placing $name in as a variable doesn't make it "go". That makes all the values in wp_localize_script return NULL
    – jedifunk
    Commented Jan 14, 2014 at 15:01
  • And they return NULL because $name doesn't seem to be at the time that the localize function runs.
    – jedifunk
    Commented Jan 14, 2014 at 15:07

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.