Limiting WordPress Load With The Shortinit Constant

What is the SHORTINIT Constant?

The SHORTINIT constant is a configuration constant in WordPress that limits the loading of non-essential WordPress functionality on pages in order to improve performance. When the SHORTINIT constant is defined as true, WordPress will skip loading portions of code that power widgets, embeds, and other functionality that may not be critical on pages where performance is the priority.

The main purpose of using the SHORTINIT constant is to limit the work done during the WordPress initialization process on each page load. This allows pages to load faster and reduces overall load and latency on high-traffic WordPress sites. The constant essentially forces WordPress to take a “short initialization” path that optimizes for performance over functionality.

Definition and Purpose

The SHORTINIT constant is defined in the WordPress core code (wp-settings.php file). By adding the code define(‘SHORTINIT’, true); to the wp-config.php file, the constant becomes defined for the current WordPress site.

When SHORTINIT is defined as true, WordPress initializes by loading only the database, options table, and key language and user preference variables. It skips loading plugins, themes, and widgets that can contribute significant overhead.

How It Limits Loading Non-Essential Code

With SHORTINIT enabled, WordPress takes a stripped down initialization path that skips unnecessary work relating to:

  • Loading active widgets
  • Loading most localization functions
  • Loading active plugins
  • Loading the currently enabled theme
  • Loading user profiles and roles

This prevents CPU cycles, memory, and I/O from being used on functionality unlikely to be needed on that page. For example, sidebar widgets may not be relevant on high traffic pages like the home page that need to load as fast as possible.

When to Use the SHORTINIT Constant

Enabling the SHORTINIT constant provides the most benefit for high-traffic WordPress sites experiencing load and latency issues. However, it can also be useful for scripts and background processes that only require WordPress core functionality.

On High-Traffic Sites Experiencing Performance Issues

High-traffic WordPress sites with pages that need to load extremely quickly are ideal candidates for using SHORTINIT. Home pages, internal site traffic pages, and any URLs accessed frequently can benefit.

For these sites, limiting unnecessary work during initialization and trimming even 100-200ms of load time can improve the user experience. Sites facing performance bottlenecks may see significant gains.

When Only Essential WordPress Functionality is Needed

The SHORTINIT constant can also be leveraged in contexts where only the core WordPress environment and database access is required. For example, command line scripts and maintenance processes may enable SHORTINIT since they don’t utilize plugins, themes, widgets etc.

For Command Line Scripts and Cron Jobs

WordPress cron events, scheduled jobs, and command line scripts can activate SHORTINIT since they rarely rely on functionality the constant disables. Improved performance usually outweighs the lost functionality.

Enabling the SHORTINIT Constant

To enable and configure the SHORTINIT constant for a WordPress site, it must be defined in the wp-config.php file prior to the core loading.

Adding Definition to wp-config.php

Edit wp-config.php in the root directory and add the following above the / That’s all, stop editing! Happy blogging line:

define('SHORTINIT', true);

This single line addition before load enables SHORTINIT site-wide. No other changes are required.

Alternative Methods

While the wp-config approach works best in most cases, some alternatives for enabling SHORTINIT are:

  • Defining in individual .php scripts using require_once and get_header()
  • Including as part of the WP CLI command when executing scripts
  • Adding a custom plugin that defines the constant

These can provide more granular control over where SHORTINIT is enabled.

Code Examples

Basic SHORTINIT definition:

define('SHORTINIT', true);

Enable via custom plugin:

<?php  
/*
Plugin Name: Enable SHORTINIT
*/

define('SHORTINIT', true);

Impacts of Enabling SHORTINIT

Using the SHORTINIT constant can significantly improve WordPress performance and load times. However, there are also some compatibility considerations site owners should be aware of.

Faster Initial Page Loads and Reduced Latency

The main benefit of SHORTINIT is faster page loading, particularly for a WordPress site’s home page and other high traffic URLs. Limiting unnecessary work directly translates into reduced load times.

Average sites may see .1 second – .3 second improvements. Sites running expensive code on initialization can see even more dramatic gains from avoiding the work.

Potential Compatibility Issues

Plugins, themes, and custom code that require widgets, embeds, user sessions, or other common functionality to be loaded may break with SHORTINIT enabled.

Issues only occur if code actually needs those components on the specific request where SHORTINIT is active. But complex sites may encounter edge case conflicts.

Loss of Non-Essential Functionality

With SHORTINIT limiting components that get initialized, any WordPress features normally powered by those components will fail to load.

Embeds from YouTube, Twitter, Facebook and other platforms will likely not work properly. Similarly, plugins for related images, contact forms, galleries, and more may have issues.

Essential site functionality powered by wp-admin dashboards and the database typically remains intact.

Getting the Most Out of SHORTINIT

To improve performance gains and avoid compatibility issues, here are some tips for making the most effective use of the SHORTINIT constant:

Useful Constants to Combine With SHORTINIT

For even faster WordPress performance, SHORTINIT can be combined with these constants:

  • WP_MEMORY_LIMIT – Increase PHP memory limit from default
  • WP_MAX_MEMORY_LIMIT – Allows unlimited memory for request
  • WP_CACHE – Enable built-in object cache
  • SCRIPT_DEBUG – Disable script debugging

Tweaking to Balance Performance and Functionality

Site owners can tweak where SHORTINIT gets used to find an optimal balance. For example, leaving it off admin dashboards while enabled on public pages may be ideal.

Testing Following Enablement

Be sure to thoroughly test a WordPress site after activating SHORTINIT to catch any compatible issues. Check forms, galleries, imports, API integrations, etc. to confirm functioning.

Key Takeaways

The SHORTINIT constant can unlock performance gains for WordPress sites through simplified initialization. While not appropriate for every site, it can provide big speed boosts in the right implementation.

Purpose and Optimal Uses of SHORTINIT

  • Limits non-critical work on initialization
  • Best for high-traffic sites needing faster page loads
  • Also useful for command line scripts and background jobs

Summary of Impacts and Trade-Offs

Enabling SHORTINIT delivers better performance at the cost of losing functionality like embeds during initialization. Test thoroughly and selectively apply it only where speed outweighs potential compatibility issues.

Leave a Reply

Your email address will not be published. Required fields are marked *