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', '.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?
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/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?
Share Improve this question edited Jun 20, 2016 at 13:07 asked Jun 20, 2016 at 12:27 user97067user97067 6 | Show 1 more comment1 Answer
Reset to default 0How 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
.
wp_enqueue_scripts
should be the action name, I guesswp_enqueue_script
is the mistake you have done while asking a question. – bravokeyl Commented Jun 20, 2016 at 12:55googlechart
outside of thewp_enqueue_scripts
action hook? – bravokeyl Commented Jun 20, 2016 at 12:56wp_enqueue_script('mygaloochart_script', plugins_url('chart.js', __FILE__), array('googlechart'));
– user97067 Commented Jun 20, 2016 at 13:02chart.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