I want to pass an array of data to my script mygaloochart_script located in the chart.js file.

Here's what I tried:

//I'm no using $atts directly because of reasons
$dataToBePassed = array (
                'chart' => $atts['chart'],
                'element' => $atts['element'],
                'elementtype' => $atts['elementtype'],
                'title' => $atts['title']

function pw_load_scripts() {
    wp_enqueue_script('googlechart', 'https://www.gstatic.com/charts/loader.js');
    wp_enqueue_script('mygaloochart_script', plugins_url('chart.js', __FILE__), array('googlechart'));
    wp_localize_script('mygaloochart_script', 'php_vars', $datatoBePassed);

 add_action('wp_enqueue_scripts', 'pw_load_scripts');

The first line of chart.js is console.log(php_vars.chart);, however nothing appears in the console.

What am I doing wrong?

  • wp_enqueue_scripts should be the action name, I guess wp_enqueue_script is the mistake you have done while asking a question.
    – bravokeyl
    Commented Jun 20, 2016 at 12:55
  • Why are you enqueue-ing googlechart outside of the wp_enqueue_scripts action hook?
    – bravokeyl
    Commented Jun 20, 2016 at 12:56
  • Edited with your remarks, but still the same issue. @bravokeyl I think t's being enqueued in the wp_enqueue_script('mygaloochart_script', plugins_url('chart.js', __FILE__), array('googlechart'));
    – user97067
    Commented Jun 20, 2016 at 13:02
  • my guess is chart.js is not being enqueued. On the other hand $dataToBePassed is outside the scope of the function , it gives you null.
    – bravokeyl
    Commented Jun 20, 2016 at 13:21
  • What do you mean it's not enqueued? How should I enqueue it?
    – user97067
    Commented Jun 20, 2016 at 13:22

1 Answer 1


How does your function pw_load_scripts() know about the existence of $dataToBePassed? Is the variable global? Looking at the code you've got, I'm guessing that, as far as wp_localize_script() is concerned, $dataToBePassed is a local variable, and it's probably null.

  • 2
    I solved it. The issue was... $datatoBePassed should be $dataToBePassed
    – user97067
    Commented Jun 20, 2016 at 14:59

Your Answer

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