In most cases, your plugin has stuff it needs to do in the admin area. It probably also has stuff it needs to do in the frontend. These two never happen at the same time, so you can actually separate the two. The easiest way of separating frontend and backend code is having a set of classes, most of my plugins end up with at least three classes:
- A common class, for stuff that is needed everywhere, for instance declaring a custom post type.
- An admin class, for all the admin related stuff, like my admin settings page.
- A frontend class, for all the stuff that is specific to the frontend, like loading a script or performing a shortcode.
You separate these by checking for two simple things:
if ( is_admin() ) require 'class-admin.php'; else require 'class-frontend.php';
Easy enough right? Well you might want to take it even one step further. WordPress sets a constant for when you’re doing an AJAX request. This constant is DOING_AJAX. When WordPress is performing an AJAX request in the admin area, you probably won’t need to load the code for your admin settings pages, but you do need to load the code for your AJAX requests.
So you’ll end up with code like this:
if ( is_admin() ) {
if ( defined('DOING_AJAX') && DOING_AJAX )
require 'class-admin-ajax.php';
else
require 'class-admin.php';
} else {
require 'class-frontend.php';
}
This also means that the first snippet should be amended a bit, even when you’re not doing anything with AJAX, you should load it like this:
if ( is_admin() && ( !defined( 'DOING_AJAX' ) || !DOING_AJAX ) ) require 'class-admin.php'; else require 'class-frontend.php';
By loading your code in a modular way like that, you’re making 100% sure your code has the lightest footprint possible and you’re not loading stuff that you (could) know upfront will never be used.



Great idea, simple solution!
I’m learning to develop WordPress themes and plug-in. This is a very useful trick to keep a plug-in as light as possible. Thanks for this! Going to check the source codes of your WordPress SEO plugin.
Awesome tips. Simple solution!
This is great man! I love speed optimization tips like this.
Joost,
Thanks this should help my blog tremendously.
Thanks for noting things like this…nothing annoys me more, re: WP plugins, than those that output a bunch of misc. code (and CSS, per your other post) that isn’t necessary. I’ve had several situations where I hand off a polished project to a semi-savvy client. When I return to the site, I’ll find that not only have they installed several plugins for things they decided they needed….but they picked plugins that made an absolute disaster out of my organized code, and now the site is crawling. In any case…I certainly appreciate it when authors pay attention to these details and Joost, you’re at the top of that list. Cheers.
okay – i really like this idea, however can you please post where this code belongs? Personally i have no problems editing function codes, its the ‘which filename.php i have trouble with’.
thx
Lonnielo,
look for main plugin filename containing Plugin information header. It should contain
* Plugin Name:
near beginning of .php file.
Find out more about it here:
http://codex.wordpress.org/Writing_a_Plugin
Simple, but very useful solution. Thx ;-)
The tip should be usefull. Thx
Thanks for this idea. It is really helps me.
I am using Yoast in my blog and it is the best help for my needs.
Thanks again.
John Gamos
It took me hours of debugging and googling to figure out that is_admin() returns true when called through AJAX. With your solution I can handle that.
Thanks a lot.
“Why Yoast is so speedy
Finding the WordPress hosting solution that’s right for your site isn’t easy. Trust me, I learned the hard way. You’ve probably noticed it though: yoast.com loads pretty darn fast.”
I don’t think your website is very fast. Pretty darn slow actually.
I just did a comparison with your site and my latest site.
http://tools.pingdom.com/fpt/#!/GEMawMxPh/www.yoast.com
http://tools.pingdom.com/fpt/#!/OikhhgR7R/www.socialstrategy.co.uk
Your site is half the page load size but much slower ~ nearly twice the delay in loading.
Mine loads in less than a second.
And its running on Apache and not using W3TC either.
This website speed thing is all smoke and mirrors!
Now tell me I am wrong and you didn’t mean page load speed.
Terence.
That’s like comparing a truck to a racecar Terence: nonsense.
Of course its nonsense, Joost.
That’s why I said it was all “smoke and mirrors!”.
That said, I am quite impressed with what can be accomplished simply by free subscription and routing your DNS through Cloudflare.
Terence.
Who’s site loads faster is irrelevant and actually completely off-topic. What’s important is that this is a best-practice coding tip that leads to better quality plugins and sites. Check the ego bro and stay focused man!
Thanks for this! | I’m learning to develop WordPress plug-ins and this is indeed useful to keep a plug-in light. Much Appreciated!
Now I have finally a solution to the problem. Ultimately it was very simple, it suffices to know the answer.