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.
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…. 😉
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.
The above filter will basically take the existing colors stylesheet output, eg.
And convert it to..
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.
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.: