Admin ajax php takes a long time to load. How to reduce server load using admin-ajax in WordPress. Disabling Heartbeat API completely
For better SEO and user experience, experts always recommend for the end user. In this context, if a lot of users visit your site, the WordPress dashboard can be slow to respond.
While testing the speed of your WordPress site using online speed testing tools, you may have noticed that the file admin-ajax.php responsible for the slow loading experience. In this article, we'll walk you through this file and how you can reduce server response times and CPU usage by reducing the number of requests generated by admin-ajax.php.
What is admin-ajax.php in WordPress?
Back in 2013, WordPress introduced the WordPress Heartbeat API, which provided several important features, such as an autosave function, upon expiration of a lock warning login while another user is writing or editing a WordPress post.
Two very salient features of the Heartbeat API:
1. Autosave
Whenever you save a post project and continue working on it, WordPress will automatically save the post additions. There is a clear distinction between autosaving and manually saving a project. Check out the following screenshot, which shows both types of saves:
2. Blocking message
Whenever you try to edit a post that another user is working on, a pop-up warning about the situation will appear. There are three actions available to you.
The above features are made possible by the WordPress Heartbeat API, which creates a connection between the server and the browser for the corresponding message and responses.
WordPress Heartbeat API generates requests for communication with the server and triggers events for receiving / responding data. Typically, this increases the load on the server and ultimately slows down the WordPress admin panel.
Live example
We entered the WordPress dashboard and started editing the post. Next, we left the tab open for a few minutes and started browsing the other tabs. The dashboard is still in the system and you can see that admin-ajaxsends requests continuously.
According to the ticket mentioned above, admin-ajax.php in WordPress generates requests every 15 seconds. Any connection with the server can be a request.
Speed up your WordPress admin panel
To speed up the WordPress backend, the best approach is to disable the Heartbeat API, or at least set a longer amount of time so that it doesn't generate requests to the server every few seconds.
Install the Heartbeat Control plugin
Login to your WordPress admin area and go to Plugins >> Add New, find Heartbeat control, install and activate his.
Go to the tab Settings >> Control Heartbeat Settings... There you will find three drop-down menus for configuring the plugin.
You can choose the area where the Heartbeat API will work. There are three options to choose from:
- WordPress Dashboard: This will enable the Heartbeat API in the WordPress dashboard.
- Frontend: This will enable the API in the web interface.
- Post editor: Check this box if you want to allow Heartbeat API to turn on autosave and block mail functions.
Select this option if you want the WordPress Heartbeat API to be disabled in specific locations. Be careful when choosing a location because other plugins may also use the WordPress Heartbeat API. If you are the only WordPress backend user I would suggest disabling it everywhere and then checking if it breaks the website. However, if your site has more than one user who contributes regularly, we would suggest that you only allow the Heartbeat API on the post edit pages.
This dropdown menu allows you to set the time interval, between 0 - 300 seconds, for administering Ajax requests. If you set it to 120 seconds, then a request will be generated every 120 seconds. This will significantly reduce the load on the server. Customize it according to your needs.
Creating multiple rules
You can create multiple rules based on your requirements. For example, you want WordPress to call every 120 seconds, but after editing, fire for 60 seconds. To do this, you need to create two rules. One for the WordPress dashboard and another for the post editor and set their frequency to 120 and 60 respectively.
Now that you have everything configured, it's time to check which plugins are slowing down your website using the admin-ajax.php file.
Go to GTmetrix, enter your site url. It will take a few minutes to analyze the site. After that go to the Waterfall tab and you will see how long the file takes for connections and responses. Scroll down a bit and see if there is an entry POST admin-ajax.php... If so, expand it and go to the Post tab. You can identify the culprit here. In our case, the "desktop switch" plugin uses the admin-ajax.php file and continually sends requests to the server. It's time to decide whether to replace it with another plugin or remove it.
Final Words
Given its feature set, the Heartbeat API is very useful on your WordPress site. However, if not used correctly, it can increase load times on the WordPress backend and on the site itself by sending queries to and fro in WordPress.
There are only two solutions to this problem. Or disable Heartbeat API / enable it only in a few places.
If you are using any caching plugin like W3TC, remember to disable the object cache. It will also speed up your WordPress dashboard.
If you have any suggestions or request, feel free to leave a comment below.
V technical support the site is often asked questions related to the work of the admin-ajax.php script on the hosting (often its work is interrupted, ends with an error, or causes a heavy load on the hosting). How to solve all these problems?
What is admin-ajax.php
The admin-ajax.php script (the so-called WordPress heatbeat, included since WordPress 3.5.2) performs the extension of the WordPress user session - for the convenience of using the admin panel. If the administrator or editor often works with the site, then this eliminates the need to enter a username / password each time. But for 99% of site visitors, this functionality is most likely not needed: after all, they only read the content, do not edit it.
In some cases, admin-ajax.php can be responsible for increased memory and CPU consumption on hosting: due to frequent - every 15 seconds - calls to the database against the background of other requests (most of which can already be cached). Since the admin-ajax.php functionality is dynamic (it cannot be cached), the Airee cloud, like any other caching tool, passes all requests directly to the server, creating a significant load.
How to deal with admin-ajax.php
The best way to remove the load from admin-ajax.php is to completely disable this functionality. This is best done either by editing within the WordPress engine or by installing the appropriate plugin.
To disable WordPress Heartbeat for all pages except the new post page, you need to add to your theme's header.php:
add_action ("init", "my_deregister_heartbeat", 1); function my_deregister_heartbeat () (global $ pagenow; if ("post.php"! = $ pagenow && "post-new.php"! = $ pagenow) wp_deregister_script ("heartbeat");)
To solve this problem through WordPress plugins you can use
How can I upload any files like images to the server using AJAX and jQuery? This is done quite simply! And below we will analyze everything in detail.
In those "ancient" times, when there was no jQuery yet, or maybe it was, but browsers were not so sophisticated, uploading a file to a site using AJAX was a dreary affair: through all sorts of crutches like iframe. I did not find those times, and who is interested in it now. And now another thing is interesting - that saving files to the site is very simple. Even without experience and understanding of how AJAX works, a webmaster can quickly figure out which way. And this article will help him. If you reinforce these capabilities with WordPress functions, then securely processing and uploading files to the server becomes completely trivial and even interesting (for an example with WordPress, see the end of the article).
However, no matter how simple everything is, it should be noted that the minimum experience with files and basic knowledge of Javascript, jQuery and PHP are still required! At the very least, you need to understand how files are uploaded to the server, how AJAX works in general terms, and at least a little you need to be able to read and understand the code.
The method described below is quite stable, and in fact relies on the Javascript object new FormData (), which is basicly supported in all browsers.
For a clearer perception of the material, it is divided into steps. That's all, let's fly ...
AJAX File Uploads: A Generic Example
It all starts with the presence on the site of the input field of type file. It is not necessary for this field to be part of the form (tag