These plugins take the easy way out and load their scripts and styles on every page just in case they’re needed, instead of intelligently loading them as needed.
The usual way to conditionally load scripts and styles in WordPress is to call wp_enqueue_script and wp_enqueue_style when you need the script or style and to not call them if you don’t need it. This works well for plugins that use [shortcodes] or otherwise print their content during the normal page building process.
Our map code is are usually built with this normal page rendering process, but in some cases we needed to load a map with ajax and found that the map scripts weren’t available when we needed them.
When content is loaded with ajax scripts and styles enqueued with wp_enqueue_script and wp_enqueue_style aren’t processed. In these cases, the easy option is, as I mentioned, to enqueue the needed scripts and styles on every page. The easy route seemed wasteful, so I came up with an alternative solution.
When the ajax content is added to the DOM, the inline <script> runs. It checks if the required <script> and <link> tags are already present. If they are not, then the scripts are loaded with a jQuery ajax call, and the CSS stylesheets are loaded into the <head> tag.
The code should work for in both the standard rendering case as well as when content is loaded with ajax. What you’re seeing above is a cleaned up version of our actual code, which you can see on GitHub, if you’re interested. It looks like a lot of code, but it’s mostly comments.
So next time you’re developing a plugin take those few extra minutes to make sure that your plugin is a good citizen on whatever WordPress site it may be installed on.