Separating frontend and admin code

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.

Separating.

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.

Tags: ,


Yoast.com runs on the Genesis Framework

Genesis theme frameworkThe Genesis Framework empowers you to quickly and easily build incredible websites with WordPress. Whether you're a novice or advanced developer, Genesis provides you with the secure and search-engine-optimized foundation that takes WordPress to places you never thought it could go.

Read our Genesis review or get Genesis now!

18 Responses

  1. LatzBy Latz on 19 April, 2011

    Great idea, simple solution!

  2. Sumon @ Sumon Seleem's BlogBy Sumon @ Sumon Seleem's Blog on 19 April, 2011

    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.

  3. Eunus HosenBy Eunus Hosen on 20 April, 2011

    Awesome tips. Simple solution!

  4. Chris CarvacheBy Chris Carvache on 21 April, 2011

    This is great man! I love speed optimization tips like this.

  5. Justin | MazzastickBy Justin | Mazzastick on 21 April, 2011

    Joost,

    Thanks this should help my blog tremendously.

  6. RossBy Ross on 21 April, 2011

    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.

  7. LonnieloBy Lonnielo on 28 April, 2011

    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

  8. Daniel TanBy Daniel Tan on 27 July, 2011

    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

  9. Port119By Port119 on 7 September, 2011

    Simple, but very useful solution. Thx ;-)

  10. Strefa ADwordsBy Strefa ADwords on 29 September, 2011

    The tip should be usefull. Thx

  11. gamosBy gamos on 17 January, 2012

    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

  12. Wesam AlalemBy Wesam Alalem on 8 February, 2012

    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.

  13. TerenceBy Terence on 14 April, 2012

    “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.

    • Joost de ValkBy Joost de Valk on 14 April, 2012

      That’s like comparing a truck to a racecar Terence: nonsense.

      • TerenceBy Terence on 15 April, 2012

        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.

  14. Chris CarvacheBy Chris Carvache on 14 April, 2012

    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!

  15. Thanks for this! | I’m learning to develop WordPress plug-ins and this is indeed useful to keep a plug-in light. Much Appreciated!

  16. antonis kioupliotisBy antonis kioupliotis on 16 July, 2012

    Now I have finally a solution to the problem. Ultimately it was very simple, it suffices to know the answer.