Make WordPress Core

Opened 8 months ago

Last modified 5 months ago

#59132 new enhancement

Create two filters in wp_scripts() and wp_styles().

Reported by: salvio's profile 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.


8 months ago

#2 @hellofromTonya
8 months ago

  • Keywords reporter-feedback added

Hello @salvio,

Welcome to WordPress Core's Trac!

I wonder .. could you instead replace the global $wp_scripts at either init or when your plugin loads? The global is available for use, including being overwritten. If your custom class extends WP_Scripts class, then when wp_scripts() is called, it will use the custom instance in the global.

What do you think?

#4 @hellofromTonya
5 months ago

  • Version changed from 6.4 to 4.2

Modifying the Version as the code for this ticket was introduced in WordPress 4.2.0.

Note: See TracTickets for help on using tickets.