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
Related Posts:
- WP_enqueue_script inside shortcode?
- How to enqueue script based on post category?
- Enqueue Scripts / Styles when shortcode is present
- Conditionally Loading JavaScript/CSS for Shortcodes
- How to add stylesheets only to pages with specific shortcode?
- Tinymce – How to hook before or after live shortcodes rendering?
- How execute shortcode with javascript
- Including javascript for a shortcode
- Shortcode to Gutenberg-block: additional text on front-end and conditional display
- Can’t get JS code to work with shortcode
- How resource intensive is wp_register_script()?
- How to handle shortcodes when using the JSON API
- Pass javascript array to shortcode
- shortcode causing the_content() to return blank?
- Add custom setting that uses radio button to WP Gallery
- Convert HTML Script to Shortcode [closed]
- Get Required Assets (JS, CSS etc) for post using REST API
- How to force that styles are enqueued in the header?
- Scripts for shortcode: concatenate or load separately? [closed]
- Adding javascript actions to words in post content
- Conditionally Loading JavaScript/CSS for Shortcodes
- Adding JS in header when using wp_enqueue_script in a shortcode
- Redirect to another page using contact form 7? [closed]
- Add JS in footer via shortcode?
- Deactivate JS Script in Plugin Shortcode
- Displaying shortcode output through Options Framework
- enqueue script with get_shortcode_regex
- Regenerating WordPress shortcodes based on value with AJAX
- WordPress Shortcode / Only works in classic editor, gutenberg error / invalid json response
- Using shortcodes in the Site Title
- wp_enqueue_scripts does not work on admin page
- Displaying an interactive web map on my wordpress site
- Multiple shortcodes with embedded JavaScript code in same page, how?
- Simulate shortcodes with javascript
- Clarify: Enqueueing scripts within shortcode only loads in footer?
- wp_register_script and wp_register_style when shortcode is used
- WordPress shortcode select option not working [closed]
- Why does my JavaScript work in the browser console but does not work in my shortcode?
- Trying to create a shortcode that displays taxonomy terms in a dropdown
- How to have shortcode autofilling the content if the attribution is specified?
- How do I define and register a shortcode function in a namespaced functions.php file?
- Add type to enqueued script inside plugin
- Shortcode argument multiple values
- Do I just put the html in a page when enqueueng or do I also have to reference js file from the html page [closed]
- Change user meta value with shortcode
- WordPress post pagination continuous [duplicate]
- custom field value as shortcode parameter
- How can I collect and output all attributes of all executed ShortCodes?
- Getting Page Content Using Shortcode
- How to reload the shortcode content when the database is changing
- Are Shortcode Attributes Always Passed As Strings?
- Best way to include reusable sections in page content
- Execute shortcodes in PHP
- How to make my shortcode load scripts and styles, from within the plugin?
- How to Change CSS Colors from Custom Plugin Settings Page
- shortcode with conditional results if logged in
- Show form per shortcode
- Can I pass parameters to the add_shortcode() function?
- How to edit blog page in wordpress? there is no place to put shortcode
- A shortcode nested inside a shortcode
- How to check if a post has any one of many shortcodes?
- Hijack WordPress Shortcode attribute
- How to transform WordPress user role code to WP shortcode?
- Shortcode Always Displays Default Attribute Value
- Use another action instead of the shortcode API to display the last time a user logged in
- How to use wordpress function wp_enqueue_script() in php?
- How to add shortcode to html widget
- Implementing a URL Shortener
- Do I need to escape number in this shortcode function?
- Shortcode don’t work in custom option field, any solution?
- how can i pass this attribute in this shortcode?
- Custom shortcode to display posts, first post is full
- WordPress User ID as Shortcode
- wp_editor not rendering the shortcode
- Slow page Loads When Using Dynamic Coding
- Turning variable into a value set in shortcode
- How to add a same div on every page
- Default WordPress shortcodes not working?
- Can I use shortcodes in mail body
- Short code inside the page not working in WordPress
- How to find specific shortcodes in a post or in a widget and pass them to a variable?
- Does short-code slow down my WordPress site?
- enqueing Javascript and CSS
- redirect if shortcode exists
- How can I list only custom shortcodes?
- Position shortcode off [duplicate]
- how to add shortcode in wordpress container [duplicate]
- Substite Category Slug in a Shortcode
- Theme JS is available but theme CSS isn’t
- Form shortcode with multiple options as attributes
- How to add data attributes to the wordpress audio shortcode
- Javascript asset not enqueuing with the rest
- Display a form partially with a shortcode
- How to make this shortcode work for post content wordpress [duplicate]
- Query to list all galleries under a parent post?
- Add crossorigin to SCRIPT tag
- Adding the username into a URL to redirect to a specific page
- Beginner’s question about shortcodes
- Shortcode Help Needed → attributes
- How To Render Shortcode In AJAX Response?