For better SEO and user experience, experts always recommend speeding up WordPress websites for the end user. In this context, what if your site has more than one user who contributes regularly and the dashboard becomes slow to respond?
While testing the speed of your website using online speed testing tools, you might have observed that admin-ajax.php is responsible for the slow loading experience. In this article, I will talk about this file and how you can reduce the server CPU usage by decreasing the number of requests generated by admin-ajax.php.
What is admin-ajax.php in WordPress?
Back in 2013, WordPress introduced WordPress Heartbeat API that provided several important functionalities such as the autosave feature, login-expiration and post lock warning while another user is writing or editing a WordPress post.
Two very prominent features of the Heartbeat API are:
Whenever you save a draft of a post and continue working on it, WordPress automatically saves the additions to the post. There is a clear difference between the autosave and manually saving the draft. Check out the following screenshot that shows both types of saves:
- Post Lock
Whenever you try to edit a post that another user is already working on, a pop-up warning about the situation will appear. There are three actions open to you.
The above-mentioned features are made possible because of the Heartbeat API that creates connections between the server and the browser for appropriate communication and responses.
Heartbeat API generates requests for communication with the server and triggers events on receiving data/response. This usually increases the load on the server and eventually slows down the WordPress Admin Dashboard.
A Live Example
I am logged into my WordPress dashboard and have started drafting a post. Next, I will leave the tab open for several minutes, and starts browsing in other tabs. The dashboard is still logged in, and you can see that admin-ajax is continuously sending requests.
According to the ticket mentioned above, admin-ajax.php generates requests after every 15 seconds. The request can be any communication with the server.
Speed up WordPress Admin Dashboard
To speed up the WordPress backend, the best approach is to disable the Heartbeat API or at least set a longer time interval so that it does not hit the server after every few seconds.
Install Heartbeat Control Plugin
Login to your WordPress Admin Dashboard, navigate to Plugins >> Add New, search for Heartbeat Control, Install and Activate it.
Configure Heartbeat Control Plugin
Navigate to Settings >> Heartbeat Control. There you will find two drop-down menus for configuring the plugin.
- Control Heartbeat Locations:
You can select the areas where the Heartbeat API will work. There are three options to choose from.
- Disable Everywhere: This will disable the Heartbeat API completely.
- Disable on Dashboard Page: This will disable the API only on the Dashboard page.
- Allow Only on Post Edit Pages: Mark this checkbox, if you want to allow the Heartbeat API to be enabled on autosave and post lock features only. It will disable the API on all other areas.
Be careful while choosing the location because other plugins might also be using the Heartbeat API. If you are the sole user of the WordPress backend, I would suggest disabling it everywhere and then check whether it breaks the website. However, if your site has more than one user who contributes regularly, I would propose that you allow the Heartbeat API only on the post edit pages.
- Override Heartbeat Frequency:
This menu allows you to set the time interval for executing admin-ajax requests. By setting it to 60 seconds, the request will be generated after every 60 seconds. That will drastically reduce the load on the server.
Detect Plugins That Use the Heartbeat API
Now that you have set up everything, it is time to check which plugins are using the admin-ajax.php file and slowing down the website.
Go to GTmetrix, enter the URL of your site. It will take few moments to analyze the site. Once done, navigate to Waterfall tab and you will see how much time a file is taking to connect and response. Scroll down a bit, and see if there is any entry for POST admin-ajax.php. If yes, expand it and navigate to the Post tab. From here, you can identify the culprit plugin. In my case, the plugin desktop switch is using the admin-ajax.php file and is continuously sending requests to the server.
Considering its feature set, the Heartbeat API is very useful on your WordPress website. However, if not used correctly, it will increase the load time of the WordPress backend and the front end.
There are only two solutions for this issue. Either, disable the Heartbeat API/ enable it in a few locations only, or upgrade to a hosting plan that could handle the load of the requests.
If you are using any caching plugin, such as W3TC, don’t forget to disable the object cache. This will also speed up the WordPress dashboard.