Make WordPress Core

Changes between Version 1 and Version 2 of Ticket #54958, comment 8


Ignore:
Timestamp:
07/21/2022 01:18:21 PM (21 months ago)
Author:
luigipulcini
Comment:

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #54958, comment 8

    v1 v2  
    99In general, using `wp_localize_script` and `wp_add_inline_script` should always be done at the same time with the registration. Therefore, if you are registering the script inside `wp_enqueue_scripts` and, at the same time, adding the inline script you need, then you are free to enqueue the script during the shortcode callback by using `wp_enqueue_script` with the handle only. In fact, WordPress has a mechanism to automatically re-enqueue a script that was prematurely enqueued before the registration whenever the script gets finally registered. So, from that point of view, the change in block themes doesn't create any harm. But `wp_localize_script` and `wp_add_inline_script` **''must''** always be executed after the script is registered.
    1010
    11 In those situations where a block theme is used and the javascript parameters are based on values calculated inside the shortcode callback, there is no solution other than **''registering''** (not just **''enqueuing''**) the script and add the inline script **''inside''** the shortcode callback itself, even if that happens before `wp_enqueue_scripts`. Since WordPress cannot really register a script twice, this second approach is perfectly viable and effective.
     11In those situations where a block theme is used and the javascript parameters are based on values calculated inside the shortcode callback, there is no solution other than **''registering''** (not just **''enqueuing''**) the script and add the inline script **''inside''** the shortcode callback itself, even if that happens before `wp_enqueue_scripts`. Since WordPress cannot really register a script twice, this second approach is perfectly viable and effective for both regular and block themes.
     12
     13Of course, this is not desirable as it creates an inconsistency that will force developers to register scripts in a different way, based on which type of theme is being used.