Introduction: The Spinning Wheel of Frustration
Have you ever logged into your WordPress dashboard only to find it sluggish, unresponsive, or displaying a 504 Gateway Timeout error? You check your server resources and see a massive spike in CPU usage. Upon closer inspection, one file keeps appearing at the top of the process list: admin-ajax.php.
For many WordPress site owners and developers, admin-ajax.php is a necessary evil. It is the bridge that allows WordPress to perform asynchronous tasks without refreshing the page—things like auto-saving posts, dashboard notifications, and real-time data updates. However, when poorly coded plugins or excessive 'heartbeat' requests go unchecked, this file can bring even the most robust servers to their knees.
In this guide, we will dive deep into why admin-ajax.php causes high CPU usage and provide actionable steps to optimize your WordPress backend for peak performance.
What is Admin-Ajax.php and Why Does it Cause Lag?
AJAX (Asynchronous JavaScript and XML) allows a web page to communicate with the server in the background. In the context of WordPress, admin-ajax.php is the central hub for these requests.
Whenever a plugin needs to update a cart count, save a draft, or check for a license update without a full page reload, it sends a request to this file. The problem is that every single AJAX request loads the entire WordPress core. This means the server has to initialize PHP, connect to the database, and load all active plugins just to process a tiny piece of data. If you have 20 users logged in and a dozen plugins sending requests every 15 seconds, your CPU usage will skyrocket.
Step 1: Diagnosing the Source of the Bloat
Before you start disabling features, you need to know which plugin or process is the culprit. You can do this using two primary methods.
Method A: Using Chrome Developer Tools
- Open your WordPress site and press
F12to open Developer Tools. - Click on the Network tab.
- In the filter box, type
admin-ajax.php. - Refresh the page or perform actions in the dashboard.
- Look at the Time and Status columns. If you see dozens of requests firing every few seconds, click on one and look at the Payload or Response tab to see which plugin is initiating it.
Method B: The Query Monitor Plugin
Install the Query Monitor plugin. It provides a detailed breakdown of all AJAX calls, including which plugin is responsible for them. This is the gold standard for backend debugging.
Step 2: Controlling the WordPress Heartbeat API
The most common cause of admin-ajax.php spikes is the WordPress Heartbeat API. Introduced in version 3.6, it creates a connection between the browser and the server for tasks like post-locking (preventing two people from editing the same post) and real-time notifications.
By default, it pulses every 15 to 60 seconds. On a busy site, this is overkill.
The Plugin Solution
Install the Heartbeat Control plugin by WP Rocket. It allows you to:
- Disable Heartbeat entirely in specific areas (like the frontend).
- Increase the interval from 15 seconds to 300 seconds.
- Limit Heartbeat to only the post editor.
The Code Solution
If you prefer not to add another plugin, you can add this snippet to your theme's functions.php file to slow down the heartbeat frequency to 60 seconds:
add_filter( 'heartbeat_settings', 'xerowp_slow_heartbeat' );
function xerowp_slow_heartbeat( $settings ) {
$settings['interval'] = 60; // Set interval to 60 seconds
return $settings;
}
Step 3: Optimizing WooCommerce Cart Fragments
If you run an e-commerce store, WooCommerce often triggers an AJAX request called wc-ajax=get_refreshed_fragments. This is designed to update the cart icon count across your site. However, it often runs on every single page load—even on blog posts where there is no cart icon.
To fix this, you can use a plugin like 'Disable Cart Fragments' or use the following code to dequeue the script on non-shop pages:
add_action( 'wp_enqueue_scripts', 'xerowp_disable_wc_fragments', 99 );
function xerowp_disable_wc_fragments() {
if ( function_exists( 'is_woocommerce' ) && ! is_cart() && ! is_checkout() ) {
wp_dequeue_script( 'wc-cart-fragments' );
}
}
Step 4: Identifying and Replacing Heavy Plugins
Some plugins are notorious for 'chatty' AJAX behavior. Common offenders include:
- Live Chat Plugins: These constantly poll the server to check for new messages.
- Real-time Analytics: Plugins that track user movement in real-time.
- Social Share Counters: Plugins that fetch share counts via AJAX on every page load.
- Inventory Managers: Tools that sync stock levels across multiple platforms.
If you find a plugin causing high CPU usage, look for an alternative that uses the WordPress REST API instead of admin-ajax.php. The REST API is generally more efficient and easier to cache.
Step 5: Implement Object Caching
Since admin-ajax.php requests hit the database, implementing an object cache like Redis or Memcached can significantly reduce the CPU load. Object caching stores the results of database queries in the server's RAM, allowing WordPress to retrieve data almost instantly without taxing the processor.
At XeroWP, we provide Redis object caching out of the box because we know how much of a difference it makes for dynamic, AJAX-heavy sites.
Why Managed Hosting is the Ultimate Solution
While code optimizations help, the underlying infrastructure of your host plays a massive role. Generic shared hosting environments often throttle CPU usage the moment admin-ajax.php starts working.
At XeroWP, our managed WordPress hosting environment is built to handle high-concurrency AJAX requests. We use high-performance NVMe storage, the latest PHP versions, and server-side optimizations specifically tuned for the WordPress Heartbeat API. We don't just give you a server; we give you a finely-tuned engine that keeps your dashboard snappy, even when you're managing complex workflows.
Final Takeaway
High CPU usage from admin-ajax.php doesn't have to be a permanent headache. By diagnosing the source with Query Monitor, taming the Heartbeat API, and optimizing WooCommerce fragments, you can restore speed to your backend.
If you're tired of fighting with server resources and want a platform that handles the heavy lifting for you, check out XeroWP's managed hosting plans today. We take care of the performance, so you can focus on growing your business.
