Support for wp_localize_script(). Inject PHP data in JS
-
Hi,
I would like to know what is the script handle of the autoptimize_.js script.
I need to add PHP data to JS and the way to do it is with wp_localize_script
<?php wp_localize_script( $handle, $name, $data ); ?>$handle
(string) (required) The registered script handle you are attaching the data for.
Default: NoneBecause Autoptimize join all my scripts I can’t use their handle name.
To avoid this issue now I am adding an empty script (excluded in Autoptimize settings), just because I know the name.
This is not ideal as it does a unnecessary request.// This empty script is needed because wp_localize_script need a script handle name. // Because the scripts will be concat by Autoptimeze we can't know that name wp_enqueue_script('global-script', get_template_directory_uri() . '/js/global.js', array(), null, true); $phpData = array( 'contactForm' => array( 'key' => get_field('contact_form_mandrill_key', 'option'), // ... ) ); wp_localize_script('global-script', 'phpData', $phpData);
Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
The topic ‘Support for wp_localize_script(). Inject PHP data in JS’ is closed to new replies.