0

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:

  1. 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;
    }
    
  2. 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));
    
  3. The shortcode

    [myslider responsive=true]

  4. The value true is printed out using print_r($data['responsive']) and console.log($resp). But for some reason responsive: $resp do not change!

What am I missing here?

  • are you sure you've got a boolean value and not a string? – Milo Aug 2 '13 at 4:44
  • Whatever value I use there, it will not echo on JS file. – Daniel Aug 2 '13 at 4:47
  • 2
    you do need the file that initializes the slider, but you don't need to physically write to that file. the fact that you are able to log the variable to the console correctly from that file proves this. if you look at the html page source, you will see a script tag that WordPress has generated which contains your 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
  • 1
    Ha ha! I got it! Please put your last comment as answer and I will accept it. Thanks a lot! – Daniel Aug 2 '13 at 17:02
  • 1
    Daniel, when there are more than 2 parties in a Comment thread, there's no automatic notification, and you need to specifically address the person with an @, 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
0

Answered using insights from @Milo.

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.