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 5 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
Remember you have to wp_enqueue_scripts and insert in there the wp_localize_script logic. –  Francisco Corrales Morales Apr 22 at 16:54
Anyone know where this code should go? –  ronnyandre Jun 20 at 9:51

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.