[wp-hackers] Reacting to options (theme options) in Javascript

Otto otto at ottodestruct.com
Wed Aug 11 20:20:22 UTC 2010


Almost forgot to explain the object part...

The "MySettings" is the name of the JS object you want to get the
settings. This allows you to keep your settings separated between
different scripts.

In the example below, the settings would be accessible to scripts via
MySettings.settings or MySettings.foo .

-Otto



On Wed, Aug 11, 2010 at 3:18 PM, Otto <otto at ottodestruct.com> wrote:
> Best way to do that is using wp_localize_script.
>
> Example:
>
> wp_enqueue_script('my_script', /path/to/my/script.js', array('jquery'), etc...);
>
> wp_localize_script( 'my_script', 'MySettings', array(
>                'setting' => 123,
>                'foo' => "bar"
>                ));
>
> Basically, that array is the parameters I want to pass to the script.
> You can form it any way you like, it's just a PHP array. What will
> happen before the script gets added to the head is that an inline
> script containing those settings will be added first. So that the
> script will have access to your settings.
>
> -Otto


More information about the wp-hackers mailing list