Separating frontend and admin code

April 19th, 2011 – 18 Comments

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';
	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';
		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';
	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.

18 Responses to Separating frontend and admin code

  1. antonis kioupliotis
    By 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.

  2. Yannis Larios | Wedding Photographer Greece | ?????????? ?????

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

  3. Chris Carvache
    By 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!

  4. Terence
    By 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: 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.!/GEMawMxPh/!/OikhhgR7R/

    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.


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

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

      • Terence
        By 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.


  5. Wesam Alalem
    By 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.

  6. gamos
    By 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

  7. Strefa ADwords
    By Strefa ADwords on 29 September, 2011

    The tip should be usefull. Thx

  8. Port119
    By Port119 on 7 September, 2011

    Simple, but very useful solution. Thx ;-)

  9. Daniel Tan
    By Daniel Tan on 27 July, 2011


    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:

  10. Lonnielo
    By 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’.


  11. Ross
    By 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.

  12. Justin | Mazzastick
    By Justin | Mazzastick on 21 April, 2011


    Thanks this should help my blog tremendously.

  13. Chris Carvache
    By Chris Carvache on 21 April, 2011

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

  14. Eunus Hosen
    By Eunus Hosen on 20 April, 2011

    Awesome tips. Simple solution!

  15. Sumon @ Sumon Seleem's Blog
    By 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.

  16. Latz
    By Latz on 19 April, 2011

    Great idea, simple solution!

Check out our must read articles about Analytics