admin管理员组文章数量:1432368
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 scriptProper use of wplocalizescript
版权声明:本文标题:wp enqueue script - Proper use of wp_localize_script? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745604387a2665752.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
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