Stop Loading Your Code On Every Admin Page
I see it every day. It comes with the territory of supporting thousands of Store Locator Plus customers around the world. We login to a customer’s site to find the reason our plugin is not working is because some other plugin is pissing their code all over our admin pages.
There is seldom, if ever, a reason that your plugin should be loading your code on my pages. But it is far easier to load your code everywhere on every page. It is far, far easier to write something like “if this is an admin page, load my code” than “if this is one of MY admin pages, load my code”. Sadly it is really not much more work to do the second option and every single WordPress site your plugin runs on will be that much faster. Now if the other dozen plugins and the theme itself would be that smart WordPress sites everywhere would be a lot happier.
This video blog discusses how the plugin “HMLive Extensions” that I’ve forked and renamed to protect the guilty is “doing it wrong”. It is loading a huge block of HTML code on EVERY SINGLE ADMIN PAGE then hiding it when it can with CSS trickery. That only makes it slower and more bloated.
The take-aways from this video should be:
- Use classes and object oriented programming techniques.
- Do not use is_admin() by itself to decide whether or not to load your huge chunks of code.
- Use class_exists() to test a class exists before defining it.
- Use a simple array with slugs , in this case $_REQUEST[‘post_type’] variable values, to determine which admin pages to run your code on.
No this is not the “be-all-end-all” fix for this type of issue. The posts and videos in this series are meant to be a general guide to shed some light on ways to improve code implementation and hopefully make your plugins and themes better citizens within WordPress. Incur overhead only when needed and don’t stomp on other people’s code.