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 guesswp_enqueue_script
is the mistake you have done while asking a question.googlechart
outside of thewp_enqueue_scripts
action hook?wp_enqueue_script('mygaloochart_script', plugins_url('chart.js', __FILE__), array('googlechart'));
chart.js
is not being enqueued. On the other hand$dataToBePassed
is outside the scope of the function , it gives you null.