Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Previously, I used the get_option() function to get an option in PHP, like this:

$width = get_option('my_width');

This is inside a shortcode function.

Now, I want to have an option in JavaScript. Is that possible?

The JS is added with wp_enqueue_script, from a shortcode function.

share|improve this question

1 Answer 1

up vote 3 down vote accepted

Define an array of parameters to be injected into the script:

$script_params = array(
    'myWidth' => get_option('my_width')

Localize the script via wp_localize_script:

wp_localize_script( 'your-script-handle', 'scriptParams', $script_params );

scriptParams now is a js object you can access from within the script:

alert( scriptParams.myWidth ); // the value from the PHP get_option call in the js
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.