最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

wp enqueue script - Proper use of wp_localize_script?

matteradmin4PV0评论

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
  • 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
 |  Show 1 more comment

1 Answer 1

Reset to default 0

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.

Post a comment

comment list (0)

  1. No comments so far