Calce

Remodelling in process

Make wp_localize_script() work properly

4

wp_localize_script($handle, $object_name, $l10n);
For example:
$myvars = array('value' => 'one');
wp_localize_script('my-handle', 'myVars', $myvars);

The above code won't do anything unless you enqueue a script with the same handle before it, like this:

wp_enqueue_script('my-handle', 'path-to-js-file.js');
$myvars = array('value' => 'one');
wp_localize_script('my-handle', 'myVars', $myvars);

This code will be inserted into your page:

/* <![CDATA[ */
var myVars = {
value: "one"
};
/* ]]> */

Then you can reference myVars in your script later on.

Comments (4)

rss
  • great article, it help me to solve my plugin problem. thanks a lot.

  • Glad it helped :)

  • Saroj

    many thank, problem solved

  • Saroj

    many thanks, problem solved

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

CommentLuv badge