{"id":187416,"date":"2014-10-31T07:48:35","date_gmt":"2014-10-31T14:48:35","guid":{"rendered":"http:\/\/css-tricks.com\/?p=187416"},"modified":"2016-03-04T16:59:08","modified_gmt":"2016-03-04T23:59:08","slug":"taking-control-cssjs-wordpress-plugins-load","status":"publish","type":"post","link":"https:\/\/css-tricks.com\/taking-control-cssjs-wordpress-plugins-load\/","title":{"rendered":"Taking Control of the CSS\/JS that WordPress Plugins Load"},"content":{"rendered":"
The other day on ShopTalk a question we got led us down a short tangent on WordPress plugins, the resources they load, and what that would look like in a perfect world. I ended up writing up some thoughts<\/a> on it and getting some feedback. I think it came to a somewhat satisfying conclusion so I figured I write it up.<\/p>\n <\/p>\n Let’s take a real-world example. The WP-Polls plugin<\/a>. I think it’s a nice plugin. Does a good job with polls. I use it here on CSS-Tricks.<\/p>\n To put those polls on your site, it needs some JavaScript to handle the functionality (i.e. voting and seeing results without a page refresh) and CSS to style the poll widget (the results bars and whatnot).<\/p>\n The way that it adds that CSS is by injecting a stylesheet via the The way it adds that JavaScript is by injecting a script via the That makes sense. That’s what they need to do to make this plugin work. Plugin authors need to make sure their plugins are easy to use and work<\/em>.<\/p>\n Now we have these two single-purpose resources being loaded on the page. When you look into web performance and how to make websites faster, some of the first advice you hear is to concatenate resources. Very good rule of thumb: the less resources a website needs to load, the faster it will be.<\/p>\n Concatenating resources isn’t some obscure thing only the most elite websites need to worry about. It’s an every website problem. People have tackled this problem in a million ways. CSS sprites were all about concatenating image resources. Icon fonts and SVG sprites are solving that same problem. CSS preprocessors help you concatenate your stylesheets. The Rails Asset Pipeline helps you concatenate resources. There are Grunt and Gulp plugins galore that are designed for this. <\/p>\n But we’re in WordPress land here, so we solutions for that.<\/p>\n The first idea I thought of was that plugins should allow us to dequeue anything they enqueue from UI they add to the plugin itself. Even go so far as<\/a> to provide the resources they would have loaded for you in a textarea for easy copy-and-pasting into your own system. <\/p>\n I now think that was a little misguided. It would take a major movement in the WordPress plugins world to get this going. Even if that happened, that’s a lot of duplicate effort, and this issue can be handled more efficiently. <\/p>\nHow Some Plugins Work Now<\/h3>\n
wp_head()<\/code> hook, putting this in the
<head><\/code>:<\/p>\n
<link rel='stylesheet' id='wp-polls-css' href='http:\/\/example.com\/wp-content\/plugins\/wp-polls\/polls-css.css?ver=2.67' type='text\/css' media='all' \/><\/code><\/pre>\n
wp_footer()<\/code> hook, putting this near the bottom of the document:<\/p>\n
<script type='text\/javascript' src='\/\/example.com\/wp-content\/plugins\/wp-polls\/polls-js.js?ver=2.67'><\/script><\/code><\/pre>\n
As a performance-concerned developer, I want to concatenate scripts and styles.<\/h3>\n
Should plugins themselves help?<\/h3>\n
Solution #1: Use a Plugin<\/h3>\n