3

The function wp_localize_script( $handle, $name, $data ); does not work when trying to overwrite the same variable, that is when calling the function second time with the same $handle and $name but with other $data value.

How can this problem be solved?

4
  • Can you provide some context? Why do you need to overwrite the data?
    – s_ha_dum
    Commented Jun 18, 2015 at 4:26
  • For example in customizer. When theme settings are changed, it refreshes the preview. Theme settings are re-retrieved and their output in front-end needs to be overwriten. The order of actions without customizer. 1. on after_setup_theme action theme settings are retrieved using get_option() 2. on 'wp_head', scripts are enqueued using wp_enqueue_script and JS vars are initialized using wp_localize_script. On Customizer preview again wp_head filter is used. get_option() is again called and frontend is reinitialized. Now wp_localize_script have to be called again. Commented Jun 18, 2015 at 5:31
  • 2
    This answer my help wordpress.stackexchange.com/a/108364/74733 Commented Jun 18, 2015 at 6:06
  • Wow, thanks! I have to test this. Don't know if it works also with the new theme switcher in customizer. Commented Jun 18, 2015 at 6:34

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.