Short version: I'm able to get a PHP variable into a JS file using wp_localize_script. On JS file i'm able to echo the variable using console.log, but I cannot get it echo into the jQuery plugin's options.
Long version:
The plugin shortcode.
public function slider_shortcode($atts) { $data = shortcode_atts ( array( 'slideshow' => '', 'responsive' => '', ), $atts ); $slideshow_att = $data['slideshow']; // DEBUG -> IT WORKS. I can see $responsive value on the page. print_r($data['responsive']); ob_start(); $my_slider( $slideshow=$slideshow_att ); $my_slider_content = ob_get_clean(); wp_enqueue_script( $this->plugin_slug . '-public-script', plugins_url( 'assets/js/public.js', __FILE__ ), array( 'jquery' ), $this->version, TRUE); wp_localize_script( $this->plugin_slug . '-public-script', 'myScriptObject', $data ); return $my_slider_content; }
The OUTPUT JS file.
(function ($) { "use strict"; $(window).load(function () { var $resp = myScriptObject.responsive; // DEBUG -> IT WORKS. I CAN SEE THE SHORTCODE VALUE. console.log($resp); $('.my-slider').SuperSlider({ 'fullWidth': true, 'responsive': $resp, // <<-- HERE IS PROBLEM. IT NEVER CHANGES. 'dimensions': '900,300', }); }); }(jQuery));
The shortcode
[myslider responsive=true]
The value true is printed out using
print_r($data['responsive'])
andconsole.log($resp)
. But for some reasonresponsive: $resp
do not change!
What am I missing here?
myScriptObject
localization data. all javascript on the page operates in a global space, so any javascript file on the page has access to that data without it having to be physically embedded within the file. – Milo Aug 2 '13 at 16:52@
, like: @Milo. . . . Note, that you can only address one person per comment. . . . If he doesn't post an Answer, feel free to add your own, so this Question doesn't remain Unanswered. – brasofilo Aug 20 '13 at 20:33