[wp-trac] [WordPress Trac] #54529: Allow for `wp_register_script()` to be called after `wp_enqueue_script()`.
WordPress Trac
noreply at wordpress.org
Mon Nov 29 05:50:10 UTC 2021
#54529: Allow for `wp_register_script()` to be called after `wp_enqueue_script()`.
---------------------------+-----------------------------
Reporter: dd32 | Owner: (none)
Type: enhancement | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Script Loader | Version:
Severity: normal | Keywords:
Focuses: |
---------------------------+-----------------------------
This is a partially thought out PR, in response to fixing issues with a
plugin and FSE.
When a plugin registers styles/scripts on `wp_enqueue_scripts` (as we
encourage plugins to do), and conditionally enqueues their script/style on
`the_content` filter, things "just work".
Unfortunately with FSE, things do not just work.
In FSE themes, `the_content` is run prior to the header being processed,
which results in the above scenario failing.
The attached PR makes a `wp_enqueue_script( 'example' );
wp_register_script( 'example', 'https://.....' );` work, where as
currently the enqueue silently fails (no doing it wrong, etc) and the
following register has no impact.
Example plugin:
{{{
// Classic Theme: This will be called first
// FSE: This will be called second.
add_action( 'wp_enqueue_scripts', function() {
wp_register_script( 'example', 'https://example.com/example.js' );
} );
// Classic Theme: This function will be called second, and enqueue the
above script
// FSE: This function will be called first, and fail to enqueue
add_filter( 'the_content', function( $content ) {
wp_enqueue_script( 'example' );
return $content;
} );
}}}
(note: not tested, just written in trac)
I'm not convinced this is a perfect option to take, and FSE might be able
to adjust how it's template loading is done, but it's done in the current
way '''specifically''' so that plugins can enqueue scripts/styles into the
header.
This also doesn't deal with certain functions, like
`wp_add_inline_style()` that attach data to an existing registered item,
only dealing with the enqueue happening before a register (or 'add' in
WP_Dependancy terms).
--
Ticket URL: <https://core.trac.wordpress.org/ticket/54529>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list