Opened 15 months ago
Last modified 11 months ago
#59132 new enhancement
Create two filters in wp_scripts() and wp_styles().
Reported by: | salvio | Owned by: | |
---|---|---|---|
Milestone: | Awaiting Review | Priority: | normal |
Severity: | normal | Version: | 4.2 |
Component: | Script Loader | Keywords: | reporter-feedback |
Focuses: | javascript, css | Cc: |
Description
I'd like to know if it's possibile to change this:
<?php function wp_scripts() { global $wp_scripts; if ( ! ( $wp_scripts instanceof WP_Scripts ) ) { $wp_scripts = new WP_Scripts(); } return $wp_scripts; }
into:
<?php function wp_scripts() { global $wp_scripts; if ( ! ( $wp_scripts instanceof WP_Scripts ) ) { $wp_scripts = apply_filters( 'wp_scripts_instance', new WP_Scripts() ); } return $wp_scripts; }
To extend WP_Scripts and WP_Styles, for example:
<?php add_filter( 'wp_scripts_instance', function ($wp_scripts) { if( !class_exists('ScriptEnqueuer') ) { return $wp_scripts; } return new ScriptEnqueuer(); });
Is this possible?
Change History (4)
This ticket was mentioned in Slack in #slackhelp by salvio. View the logs.
15 months ago
#3
@
15 months ago
That's how Automattic's concatenation library works: https://github.com/Automattic/nginx-http-concat/blob/73dbe08af00a92b91977d5eb9fb0d92b5f5c0909/cssconcat.php#L188-L195
Note: See
TracTickets for help on using
tickets.
Hello @salvio,
Welcome to WordPress Core's Trac!
I wonder .. could you instead replace the global
$wp_scripts
at eitherinit
or when your plugin loads? The global is available for use, including being overwritten. If your custom class extendsWP_Scripts
class, then whenwp_scripts()
is called, it will use the custom instance in the global.What do you think?