Is it possible to change the attributes of a registered style or script before it fires?

I’d like to modify the properties of a registered style(or script – same applies) before it’s been loaded.

Right now i’m trying to modify a registered style just before it gets enqueued, so i can point it at another file(ie. change the src property, maybe others to). Looking for some ideas specifically about where to hook on and modify the $wp_styles object so that the enqueue fire as normal, but with my changes to the registered style’s properties.

Read More

I’m making an admin color scheme and thought it would be awesome if i could rewrite/tweak the existing enqueue, instead of adding an additional request to every page(ie. colors-fresh.css + my stylesheet – i’d rather have one request, why include the original stylesheet only to redefine every style in another)…

Unregister the style and register my own? – I’d then have to unregister both color schemes(classic/fresh) when i’d prefer to simply hook on and tweak the styles object before the enqueue fires.

I’m pretty sure it’s possible, but simply lacking the brain power to wrap my head around it right now.. (been wired in too long)..

Any suggestions welcome…. 😉

Related posts

Leave a Reply

2 comments

  1. Modify a registered style’s path

    I wanted to tweak the path to one of the WordPress admin stylesheets so i could keep requests down, and because it makes little sense to include two stylesheets, when the one i’m calling redefines all the styling in the stylesheet enqueued by WordPress.

    The idea is basically to re-point the existing style at a different stylesheet, two hooks are appropriate

    • style_loader_src
    • style_loader_tag.

    The former provides just the stylesheet URL(or path) and the handle(that’s the name the style is registered with), the latter provides the complete HTML string(and handle) for the stylesheet being included.

    I decided to use style_loader_src to switch the colors stylesheet path(src) because that’s literally all i need, to adjust the path and know the current handle, so suits my needs perfectly.

    Example filter that changes the path to the colors stylesheet.

    Checks if the handle is the colors stylesheet, and updates the path when it is.

    function switch_stylesheet_src( $src, $handle ) {
        if( 'colors' == $handle )
            $src = plugins_url( 'my-colors.css', __FILE__ );
        return $src;
    }
    add_filter( 'style_loader_src', 'switch_stylesheet_src', 10, 2 );
    

    The above filter will basically take the existing colors stylesheet output, eg.

    <link rel='stylesheet' id='colors-css' href='http://example.com/wp-admin/css/colors-fresh.css?ver=20100610' type='text/css' media='all' />
    

    And convert it to..

    <link rel='stylesheet' id='colors-css' href='http://example.com/wp-content/plugins/my-plugin/my-colors.css' type='text/css' media='all' />
    

    I find this method preferable to enqueuing an additional stylesheet, it’s one less requests, and there’s far less CSS overrides required in whatever styling you’re doing, because you’re essentially hijacking that request to load your own stylesheet instead.

  2. You can use the filter wp_admin_css_uri in the function of the same name which returns the uri of the admin css files. E.g.:

    function custom_modify_styles( $_file, $file ) {
        if( 'style' == $file ) // $file = css filename without extension
            $_file = 'my/new/path/to/style.css';
        return $_file;
    }
    add_action( 'wp_admin_css_uri', 'custom_modify_styles', 10, 2 );