If you’re outputting the script in the footer, you can call wp_localize_script
at any point after your script is enqueued but before the script is output on wp_footer
priority 20.
function wp_enqueue_woocommerce_style(){
wp_enqueue_script(
'chroma_js',
get_template_directory_uri() . '/js/chroma.js',
'',
'1.1',
true
);
}
add_action( 'wp_enqueue_scripts', 'wp_enqueue_woocommerce_style' );
function wp_localize_woocommerce_style(){
$array = array(
'var1' => $var1,
'var2' => $var2
);
wp_localize_script( 'chroma_js', 'php_vars', $array );
}
add_action( 'wp_footer', 'wp_localize_woocommerce_style', 0 );
Related Posts:
- When should I use wp_register_script() with wp_enqueue_script() vs just wp_enqueue_script()?
- Check if a script/style was enqueued/registered
- How to properly dequeue scripts and styles in child theme?
- Cannot deregister a script using wp_deregister_script
- How to dequeue / deregister any theme styles and scripts
- Hyphens vs. periods in the script slug in wp_register_script?
- enqueue and localize script in footer
- How to Add a Custom Script to Customize.php
- wp_enqueue_script vs. wp_register_script
- Child Theme – what is the scope of overwriting files?
- Best way to enqueue extremely popular scripts like bootstrap and font awesome
- Best spot for wp_register_script() and wp_register_style()
- wp_enqueue_script with dependencies doesn’t work
- Register script/style: Is it possible to customize the version query string via plugin?
- Help with enqueing scripts in footer after init action
- Modernizr check first, then move on to wp_register_script()/wp_enqueue_script
- How to cancel `wp_print_scripts`?
- Including style.css in Child Theme
- getting a js file for one page
- Can’t see why my scripts aren’t loading when I register and enqueue them
- Including files in Child Themes
- WordPress script file version numbers changing in live environment
- Change script type and src of plugins in theme
- How to enqueue JavaScript file that depends on CSS stylesheet
- Adding multiple wp_register_scripts for templates in WordPres?
- Localizing script to pass a value to a javascript function
- Enqueuing script is adding extra text for google maps [closed]
- Deregister multiple scripts using a function?
- wp_enqueue_scripts does not work
- Modular CSS and JS in Child Themes
- If a Script has been enqueued but not registered can I still removed it?
- include jquery plugin file not working
- How to use wp_register_script / wp_enqueue_script for multiple queries
- How to register script with null value for version?
- Stop a plugins js and css from loading on all pages and show up only where it is called
- Enqueue registered dependencies only when necessary
- How to get, in WP page’s script, a wp enqueued script (in Functions.php)?
- wp_enqueue doesn’t load dependencies
- Why do WordPress adds the id=”handle-{js|css}” attribute to scripts and stylesheet?
- Dequeue/deregister scripts for everybody but the administrator
- How can I get a list of all enqueued scripts and styles?
- Load CSS/Javascript in frontend conditionally if block is used
- How can I de-register ALL styles all at once? And same with Javascript?
- Enqueue script only when shortcode is used, with WP Plugin Boilerplate
- Using wp_enqueue_script on shortcode function handler
- Conditionally dequeue dependency of scripts
- Adding dependencies to script enqueing
- How to dequeue / deregister parent theme style
- Why is it wrong to use admin_print_scripts-{hook} to enqueue a script (.js) file?
- Enqueueing Scripts on a Custom Top-level Menu Page
- Can’t get JS code to work with shortcode
- How to switch css files according to devices and button click?
- Bootstrap js refuses to load
- Stop WordPress editor embedding links
- Enque Typekit Fonts – Not Found
- How to load script-related styles automatically?
- Why isn’t jQuery 2.1 loading in the header?
- Notice: wp_enqueue_script was called incorrectly
- WP .js script file not loading
- Do we have to register JQuery if we want to use it in wp_enqueue_script()?
- wp_head() – list hooked actions with priorities?
- Exclude external uri for css and js version
- Do I need to register my own created .js and .css files
- Not sure if enqueuing js scripts properly
- How can I properly enqueue tags in WordPress to use with Web Components?
- Jquery function working in Dev Console but not otherwise [duplicate]
- Custom script file enqueue has “?ver=4.5.1” when loading and doesn’t update
- get_stylesheet_directory() in child theme breaks parent scripts
- Scripts not loading through function Method in WordPress Theme
- Not all my scripts are enqueueing
- JavaScript is not enqueuing
- wp_enqueue_style referencing parent theme
- How to register and enqueue JavaScript files without breaking plugin dependencies?
- how to enqueue javascript to manipulate acf input field in admin?
- Add crossorigin to SCRIPT tag
- load a different stylesheet in a category post
- Fatal error: Call to undefined function wp_enqueue_media()
- javascript file is not enqueing
- Calling wp_localize_script in a different function
- Check for Twitter Bootstrap Loaded
- thickbox never gets called (weird behavior)
- Unqueue css file from specific page
- Modifying an enqueued script URL
- When to use wp_register_script() function?
- wp_enqueue_scripts action from a plugin overrides theme wp_enqueue_scripts no matter what the priority setting
- True parameter but jquery register in header and not in the footer with wp_register_script
- Trouble with wp_enqueue_script
- Can’t get jquery script to work
- Proper use of wp_localize_script?
- Prevent caching when using wp_enqueue_script?
- Should I be enqueueing styles/scripts once and then dequeueing them?
- List all non-admin enqueued files
- How to load default wordpress scripts
- Generating embed code for users to share
- How do I display the handle for all front-end enqueued scripts in plugin options page?
- css3-mediaqueries-js failing with child theme
- Registering / Enqueing Scripts Not Working
- Enqueueing Script to footer puts it at the very bottom
- Trying to use wp_register_script/style and enqueu them from an array – getting an error [duplicate]
- wp_script_is(‘enqueued’) true, but file not requested in HTML