[wp-trac] [WordPress Trac] #54529: Allow for `wp_register_script()` to be called after `wp_enqueue_script()`.
WordPress Trac
noreply at wordpress.org
Fri Dec 3 01:02:35 UTC 2021
#54529: Allow for `wp_register_script()` to be called after `wp_enqueue_script()`.
---------------------------------------------+---------------------
Reporter: dd32 | Owner: (none)
Type: defect (bug) | Status: new
Priority: normal | Milestone: 5.9
Component: Script Loader | Version: trunk
Severity: normal | Resolution:
Keywords: has-patch has-unit-tests commit | Focuses:
---------------------------------------------+---------------------
Comment (by dd32):
Replying to [comment:6 azaozz]:
> > This also doesn't deal with certain functions, like
wp_add_inline_style() that attach data to an existing registered item.
>
> Thinking that's okay.
As do I, it's not perfect, but it's not horrible.
This is an example of what wouldn't work though:
{{{
add_action( 'wp_enqueue_scripts', function() {
wp_register_script( 'example', 'https://example.com/example.js' );
} );
add_filter( 'the_content', function( $content, $post ) {
wp_enqueue_script( 'example' );
wp_localize_script( 'example', 'exampleData', [ 'postID' => $post->ID ]
);
return $content;
}, 10, 2 );
}}}
but like @azaozz said, that doesn't seem like a real-world example,
usually you'd have something more akin to this, which would still work, or
you'd not use `register` and just a straight `enqueue + url + localise` at
the same time.
{{{
add_action( 'wp_enqueue_scripts', function() {
wp_register_script( 'example', 'https://example.com/example.js' );
wp_localize_script( 'example', 'exampleData', [ 'url' =>
'https://example.com/' ] );
} );
}}}
One thing I'm unsure of - The PR causes the enqueue to happen before the
script register "in footer" call is made, but I'm not sure if that's
'''actually''' a problem, as I don't know how the 'group' part is
handled.. I ''think'' it's okay.
{{{
$registered = $wp_scripts->add( $handle, $src, $deps, $ver ); // it'll be
enqueued at this point
if ( $in_footer ) {
$wp_scripts->add_data( $handle, 'group', 1 ); // This might run
after the enqueue.
}
}}}
https://github.com/WordPress/wordpress-
develop/blob/2648a5f984b8abf06872151898e3a61d3458a628/src/wp-
includes/functions.wp-scripts.php#L178-L181
(note: Can someone else who is more familiar with scripts/styles please
test and commit, I'm way too rusty on these things)
--
Ticket URL: <https://core.trac.wordpress.org/ticket/54529#comment:7>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list