Optimize file loading in WordPress

If your WordPress themes and plugins contain a lot of files, then loading all files where they’re not needed can waste your memory, thus reduce the website performance and increase the loading time. For example, you include theme setting files in functions.php, meaning those files will be loaded redundantly in the front-end too (they don’t have business here!).

Another problem may occur is collision with other libraries, plugins and break your website. Especially when the themes, plugins go large, it’s very difficult to chase the error, debug and fix it.

To avoid those problems, we have to optimize file loading! The principle is simple: load them only when needed.

We can define some sections of the code in a theme, plugin:

  1. The code used site-wide, including: define constants (want to know why?), register custom post types, widgets, sidebars, menus, etc.
  2. The code used in the front-end only: shortcodes, template output filters, enqueue scripts & styles, header & footer injection, etc.
  3. The code used in the back-end only: settings, meta box, ajax, etc.

The site-wide code can be put in the functions.php in theme, or main plugin file. But if you have many functions, hooks there, you might want to put them in separated files and just include them in functions.php or main plugin file.

The code used in front-end or back-end are organized similarly in separated files. You can put these files in separated folders to make the theme structure clear and easy to understand. And in the functions.php or main plugin file, we’ll load these files conditionally.

To know we’re in the back-end or front-end, we use the constants WP_ADMIN. If it’s defined and true, then we’re in the back-end, otherwise we’re in the front-end. Another way is use function is_admin, but I prefer using constant as it’s simpler and maybe faster.

For ajax actions, there’s also a constant DOING_AJAX which is true if we’re processing an ajax action.

There’re some several constants useful for this type of checking:

  • IS_PROFILE_PAGE: true if we’re in the profile page
  • IFRAME_REQUEST: true if we’re in an iframe uploader

So, basically, the code looks like this:

// Define plugin/theme constants
// Should define paths & URLs
define( 'CONSTANT_1', 'constant_1_value' );
define( 'CONSTANT_2', 'constant_2_value' );

// Common files
include 'inc/common_1.php';
include 'inc/common_2.php';

// Load files ONLY in the back-end
if ( defined( 'WP_ADMIN' ) && WP_ADMIN )
{
    include 'inc/admin/admin_1.php';
    include 'inc/admin/admin_2.php';

    // For AJAX
    if ( defined( 'DOING_AJAX' ) && DOING_AJAX )
    {
        include 'inc/admin/ajax_1.php';
        include 'inc/admin/ajax_2.php';
    }

    // Profile page
    if ( defined( 'IS_PROFILE_PAGE' ) && IS_PROFILE_PAGE )
    {
        include 'inc/admin/profile_1.php';
        include 'inc/admin/profile_2.php';
    }
}
// Load files ONLY in the front-end
else
{
    include 'inc/frontend/frontend_1.php';
    include 'inc/frontend/frontend_2.php';
}

As you see, the more we know about the current requested page/action, the more we control files loaded. There’re maybe more useful conditions that I don’t know, and if you know any of them, please share with me in the comment.

2 thoughts on “Optimize file loading in WordPress

    1. It depends on the content of the file. If the side-wide file just register some hooks, it should work.

Comments are closed.

Newsletter

Subscribe to our newsletter to receive news and tutorials about WordPress and web development.

Memory - An elegant, clean and powerful WordPress blog theme! Learn more →