Join Carrie Dils for an in-depth discussion in this video Applying filters for loading stylesheets, part of Advanced WordPress: Action and Filter Hooks.
- [Instructor] Earlier, we came and hooked onto this wp_enqueue_scripts around line 18 in the plugin and we used that to load up a custom stylesheet for our plugin. And that's a nice thing to do because, therefore you're adding some basic sort of starter styles for whatever content you're making visible to users; however, you can sort of go overboard. I've seen some plugins where developers get highly opinionated about applying styles which makes it difficult down the road for, say, a theme developer to come overwrite those styles.
So, if you do use custom styles with your plugin, you want to be judicious in how you apply those styles so that another developer could easily override the styles in the future. Now, if you wanted to be really nice, you could easily provide a way for a developer to come in and disable your styles all together. And I'm going to show you how to do that. So I'm going to come back here to this function spcp_login_stylesheet and I'm going to add a conditional statement, an if, and here I'm going to say apply_filters and I'll give my filter hook a name, I'll call it spcp_load_styles.
And the value to filter there is just going to be true; put the brackets around that. Okay, so if we go ahead and save it and come back and look, we'll refresh, and this hasn't done anything yet. It didn't affect the output of our styles and that's because when this is being executed, it's coming through and it's seeing this apply filters here but no one has hooked into this filter to declare true or not true so it just ignores that and comes on and executes this wp_enqueue_style.
So now, another plugin developer or a theme developer could add a little bit of code like this, they could call that filter, so spcp, easy for me to say, _load_styles. And I could write a callback function here that returned a value of false or I could simply say I want to return false. Now if I save that and go back and refresh, you can see that my widget is still here but it removed the styles. And just to show you what's going on here in the source code, let me comment out this filter really quickly.
So if we go back and load, here we can see the stylesheet that's coming from my plugin here and if we go back and uncomment out that filter, reload the page, we can see that now the stylesheet never even got loaded in the first place. Pretty cool. I want this plugin to actually load our styles, so I'm going to make this a comment and we'll just say, (mumbles) leave it here, and leave a comment so that another developer could note that they could use this bit of code to remove our style sheet.
And really, it would be a better practice to put this in the instructions for using the plugin, maybe a read me for the plugin, but I'm going to leave it inline for now. And as I write this, I notice that I've got a little typo there on stylesheet; I'm going to fix that. And we'll go ahead and save it. So there, that's how you could use apply_filters in code that you're writing to let somebody else down the line come in and modify what your code's doing without actually editing the code base itself.
- Actions and filters explained
- Identifying available hooks and filters
- Looking at load order
- Understanding callback functions
- Creating custom hooks
- Using third-party hooks
- Building a new WordPress plugin with filters and actions