How to reduce server load using admin-ajax in WordPress. How to speed up WordPress - getting rid of admin-ajax Adjusting the frequency of requests to the server

Hi all. We continue the topic of speeding up a WordPress site. In the article we removed unnecessary things from the files, but not everything yet. In general, the good thing about WordPress is that it allows you to change the code the way you want, to suit different desires.

When you buy hosting, then, of course, you estimate how much memory is needed for the site and what the load will be. And, if you are limited in funds, then you should save these parameters.

There is such a mechanism in the engine - HeartBeat API. Why is it needed? What it is?

HeartBeat API is responsible for

  • for autosave;
  • for blocking a page or post for all other site administrators while you edit something there;
  • for updating information about the user's presence in the console.

You understand, I hope that at this moment there is simply a devouring of resources, because there are constant requests for hosting to the admin-ajax.php file. And if you like to open several admin panel tabs at once, then this is a total bummer. Forgive me for the jargon. Requests are sent autonomously for each tab.

Essentially HeartBeat API is the most resource-intensive mechanism of all in the WordPress engine.

Do you need such a mechanism? Of course, everyone decides for themselves. I don't need it.

It is believed that it makes sense to disable it if you are the only administrator, and also if there are enough resources in your account, but while the admin is in the console, the load increases, and significantly.

According to my observations, after turning off the HeartBeat API, the occupied volume of my site decreased by almost 2.5 times! I'm serious. It’s a pity I didn’t take a screenshot, but I don’t want to go back to the original state.

How to speed up a WordPress site by turning off the HeartBeat API

Shutdown HeartBeat API code

Everything is very simple. You need to add it to the beginning of the functions.php file, right after the tag

Add_action("init", "stop_heartbeat", 1); function stop_heartbeat() ( wp_deregister_script("heartbeat"); )

And you will be happy.

Attention! This code may not be suitable for your site. It all depends on the installed theme, plugins and settings. You can try and, if the code is incompatible with any element of the site or something breaks and does not work, you can always return everything back. Just erase the code.

HeartBeat Control plugin

Not comfortable with codes and afraid of harming your site? There is such a solution for you.

Install the HeartBeat Control plugin (select plugins in the console, install a new one, search for the plugin, install and activate).

Go to settings (in the console Tools -> HeartBeat Control). In the settings you can select the following options:

1. Whether or not to disable HeartBeat for the entire site or use it for some sections.

2. You can and should edit the frequency of requests to the admin-ajax.php file. When installing, it is most likely indicated as 15 seconds. If you change, say, to 30 seconds, then the resource consumption for these requests will decrease by 25%! And if you increase the time even more, then even more. Impressive, right?

Attention!

  • Disabling the HeartBeat API disables the autosave feature. From time to time, save pages and entries when editing them (as necessary).
  • Disabling the HeartBeat API occurs in a specific template. If you change the template, then everything must be done again.
  • If you have updated the current theme, then after the update the shutdown must be repeated.

Good luck to all!

2016-05-05T09:48:44+00:00 Hope WordPress how to speed up wordpress

Hi all. We continue the topic of speeding up a WordPress site. In the article How to speed up WordPress by minifying CSS, HTML and Javascript, we removed unnecessary stuff from the files, but not all of it. In general, the good thing about WordPress is that it allows you to change the code the way you want, to suit different desires. When you buy hosting, then, of course, you consider...

Nadezhda Trofimova [email protected] Administrator Blog site

You might also be interested in:

How to make a Favicon for a website

A favicon is a beautiful icon that everyone sees next to the name of the site in the browser. Favicon allows your site to stand out both in browser bookmarks and...

Beautiful buttons for the site

Good day to all. Suddenly there are people who want to place beautiful buttons on their website, but they can’t find them. In fact, I tried in search engines...

Example 404 pages

Hello, dear reader of my blog. This article is about absolutely nothing, I just finally got around to page 404.

How and where Title and Description tags are written

What are Title and Description tags? These tags are necessary to optimize your site. If tags are written incorrectly, search engines will not index your article. A lot of...

For better SEO and user experience, experts always recommend for the end user. In this context, if you have a lot of users visiting your site, the WordPress dashboard may 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 will explain about this file and how you can reduce server response time 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 auto-save feature when login expires and a ban warning while another user is writing or editing a post on WordPress.

Two very distinctive features of the Heartbeat API:

1. Autosave

Whenever you save a post draft and continue working on it, WordPress automatically saves your post additions. There is a clear difference between autosaving and manually saving a project. Check out the following screenshot which shows both types of saves:

2. Block message

Whenever you try to edit a post that another user is working on, a warning will pop up about the situation. 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.

The WordPress Heartbeat API generates requests to communicate with the server and fires events to receive/reply data. This generally increases the load on the server and ultimately slows down the WordPress admin panel.

Living example

We logged into the WordPress dashboard and started editing the post. Next, we left the tab open for a few minutes and started browsing other tabs. The instrument panel is still in the system and you can see that admin-ajaxcontinuously sends requests.

According to the ticket mentioned above, admin-ajax.php in WordPress generates requests every 15 seconds. The request can be any connection to the server.

Speed ​​up WordPress admin panel

To speed up the WordPress backend, the best approach is to disable the Heartbeat API, or at least set it to a longer time period so that it doesn't generate requests to the server every few seconds.

Install the Heartbeat Control plugin

Login to your WordPress admin 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 to configure the plugin.

You can select 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.
  • Message editor: Check this box if you want to allow the Heartbeat API to enable autosave and blocking mail features.

Select this option if you want the WordPress Heartbeat API to be disabled in certain locations. Be careful when choosing a location because other plugins may also use the WordPress Heartbeat API. If you are the only user of the WordPress backend, I would suggest disabling it everywhere and then checking to see if it breaks the website. However, if your site has more than one user who contributes regularly, we would suggest that you only enable the Heartbeat API on post edit pages.


This dropdown menu allows you to set the time interval, ranging from 0 - 300 seconds, to execute admin Ajax requests. If you install it on 120 seconds, then the request will be generated every 120 seconds. This will significantly reduce the load on the server. Customize it to suit your needs.

Creating Multiple Rules

You can create multiple rules based on your requirements. For example, you want WordPress to fire 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 set up, 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 response. Scroll down a little and see if there is an entry POST admin-ajax.php. If yes, expand it and go to the Post tab. Here you can identify the culprit. In our case, the “desktop switch” plugin uses the admin-ajax.php file and continuously sends requests to the server. This is the time to make a decision to either 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 it is not used correctly, it can increase the loading time on the WordPress backend and on the site itself, sending requests back and forth to WordPress.

There are only two solutions to this problem. Or disable the Heartbeat API/enable it only in a few places.

If you are using any caching plugin, such as W3TC, be sure to disable the object cache. This will also speed up your WordPress dashboard.

If you have any suggestions or request, feel free to leave a comment below.

A common scenario when working with WordPress is diagnosing increased usage of admin-ajax.php. If you've been working with WordPress for a while, you've most likely encountered this issue when running third-party speed tests or checking server access logs. Typically, increased usage of admin-ajax.php is associated with the use of third-party plugins or frequent uncacheable requests from the admin panel based on the Heartbeat API (for example, autosaving drafts). It is very important to immediately identify such high spikes in hits to admin-ajax.php, as they can lead to the site being unavailable. Check out the recommendations below for troubleshooting admin-ajax.php issues in WordPress.

What is the admin-ajax.php file?

The admin-ajax.php file itself is not bad if used correctly. It acts as part of the core. The file was added by the WordPress development team in release 3.6. The purpose of admin-ajax.php is to create a connection between the browser and the server using AJAX. This allows for advanced features including improved autosave, version tracking, login timeouts, session management, and post lock notifications when multiple authors are editing a post. All of these features are great, especially for those users who support sites with multiple authors and administrators.

“The idea is to implement a relatively simple API that will send XHR requests to the server every 15 seconds and fire events (or callbacks) when data is received. Other components will also be able to “hook up” or receive notifications about the activity of other users. This can be used to block simultaneous editing of widgets or menus, or other tasks that require regular updates from the server."

Unless you are a technically savvy user, the only situation that will be important to you in the context of admin-ajax.php is trying to understand why this file is slowing down in tests (for example, in Pingdom). Or you may see a bunch of requests for this file in the hosting access logs.

The appearance of admin-ajax.php in speed tests and access logs is usually the result of two different scenarios: one in the frontend and one in the backend. We'll talk about this later.

Plugins may cause admin-ajax.php to slow down

The most popular problem faced by users in the context of admin-ajax.php is caused by third-party plugins. This is usually visible on the front end and shows up in speed tests. Developers use this file or AJAX to add functionality to their plugins. Just because you see a request to admin-ajax.php does not mean that it will necessarily slow down the site. AJAX loads after the page is presented, and some requests may load asynchronously. But this does not always mean that the developer performed the download correctly and wrote efficient code in terms of performance.

Below are examples of how we quickly diagnosed slowdowns in admin-ajax.php. We used GTmetrix because it allows us to examine individual post and response data. Pingdom, unfortunately, does not allow us to see data requests in forms. You can also use Google Chrome Devtools or WebPageTest.

High load on admin-ajax.php when using the Visual Composer plugin

Below is our WordPress site where we were experiencing very high latencies when using admin-ajax.php. As you can see from GTmetrix, the latency of requests to admin-ajax.php is over 8 seconds. The rest of the “waterfall” looks fine, but this delay must be addressed by site owners.

If you click on the POST admin-ajax.php request, you will see the following three tabs: Headers, Post and Response. When diagnosing the problems we're looking at, we'll only be interested in the Post and Response tabs. Hints can be seen in the Response tab. We see that part of the request is associated with the vc_shortcodes-custom-css script.

A quick copy and paste into Google reveals that the problem is caused by the Visual Composer plugin.

The next step for the user is to ensure that the Visual Composer plugin is completely up to date. If yes, then you need to contact the plugin developers and tell them about the problem with admin-ajax.php. This is actually a fairly common problem, judging by posts on Stack Overflow. If a developer can't solve it in their plugin, there are always several alternative plugins that do roughly the same thing - that's the power of WP. For example, if you need page builders, then you can try Beaver Builder or Elementor.

High latency with admin-ajax.php due to Notification Bar plugin

The second example that we encountered ourselves occurred after installing a third-party plugin. We noticed a sharp increase in requests to admin-ajax.php.

Again, if you click on the admin-ajax.php POST request, then you can explore everything in detail. We see that the data contains the string “action=mtsnb_add_impression&bar_id=88&ab_variation=none”.

Correctly, the first part of the post action usually refers to the plugin's CSS class. We do a quick Google search for "mtsnb" and find that this class is used in the Notification Bar plugin that we have running on our site.

If you are a developer, there are other ways to track which plugin may be generating a POST response, but we find the methods described in this article to be very effective.

The old-fashioned method of determining the source of admin-ajax.php requests

Above we have given several simple ways to find a plugin on a WordPress site that may lead to this problem with admin-ajax.php. In our experience, these methods work in 99% of cases. However, it also happens that the POST data or actions may not be so clear, and therefore you will have to go the old proven way. Simply disable all plugins on your WordPress site. Run a speed test. If admin-ajax.php is not found anywhere, then enable each plugin in turn and conduct a speed test. This way you can quickly identify the problematic plugin.

CPU problems with admin-ajax.php in the backend

The second major issue is that the WordPress Heartbeat API uses admin-ajax.php to trigger AJAX calls from the web browser, which can lead to high CPU usage. Each AJAX request also loads the full WordPress backend, bypassing all forms of caching. Typically, you won't see this in speed tests. This is especially important to know for those who use cheap shared hosting, since they don't have much wiggle room when it comes to CPU. This problem has surfaced with HostGator hosting before.

One of the scenarios that you need to be very careful with is auto-saving in WordPress and having open tabs. For example, we launched a new project on our website and left the tab open. The Heartbeat API by default polls the admin-ajax.php file every 15 seconds to do automatic saving. This can be seen in the access logs. If we left the tab open for a day, one post could generate 5,760 requests per day.

That's too many backend requests for one person. Imagine what would happen if you had several editors on your site. These requests will increase exponentially. We encountered this during our research with DARTDrones, in which we helped expand their WooCommerce site for Shark Tank. They had over 4100 calls to admin-ajax.php per day, while there were only 2000 visitors. This is not the best ratio of requests to visits. Our research showed that the majority of requests were coming from the console - they were generated due to the fact that administrators were updating the site, preparing it for display.

There is a way to detect some problems with calls to admin-ajax.php in the backend when it comes to autosaves, revisions, locking records, etc. To do this, you need to install the Heartbeat Control plugin.

The Heartbeat Control plugin is currently installed on over 50,000 sites and has a rating of 5 out of 5. It's been 2 years since it was updated, but it still works the same, since the Heartbeat API has not been updated either.

The plugin allows you to control the frequency of calls to the WordPress Heartbeat API with a couple of clicks. The first option allows you to control the location of the heartbeat - for example, disable it everywhere or allow it only on post editing pages. We recommend using the latter option, as completely disabling the plugin may cause some plugins that depend on the Heartbeat API to stop working.

The second option allows you to set the polling frequency. Changing from 15 seconds to 60 seconds will allow you to significantly save the number of requests and save server resources (reducing CPU load).

You can also use a premium plugin like perfmatters to turn off heartbeat, change polling frequency, and modify autosave interval.

Conclusion

As you can see, there are several quick and easy options for diagnosing and fixing problems with admin-ajax.php. Typically, they appear due to third-party plugins that may conflict with each other or have a poor codebase.

Have you encountered this problem on your website? If so, please share your stories.

The flow of useful advice to users of the world's most popular free CMS will not end. Precisely because it is popular and because it has features that are important to remember. Previously we talked about how:

Today we will expand the second theme a little and disable a very resource-intensive and not everyone needs WordPress mechanism called HeartBeat API.

HeartBeat API— WordPress functionality that provides things like:

  • Autosave;
  • Locking a post or page for other site editors while the post or page is edited by one person;
  • Update information that the user is still in the administrative part (console).

It does this by maintaining a connection between your browser (where you edit the site) and the server (on which the site is stored).

In fact, this is expressed in constant requests with an interval of 15-30 seconds (depending on the installed version) to one specific file admin-ajax.php from almost any page of the administrative panel. The situation gets worse if you prefer to keep multiple console tabs open at once: requests are sent autonomously from each tab. In fact, HeartBeat API is the most resource-intensive of all the standard WordPress mechanisms.

We will disable this function today. It is recommended to disable the HeartBeat API for your WordPress site if:

  • In general, your sites have enough resources on your account, significant exceedances occur only while working in the console;
  • The site is edited only by you personally(that is, the site has 1 editor, not several).

Attention! This solution is not suitable for all sites. This depends on the theme installed on the site, plugins and individual site settings. You can try it out and if it turns out to be incompatible with any element of the site or affects a site function that is important to you, you can always enable the HeartBeat API by returning the previous settings.

How to disable HeartBeat API

To do this you need to make changes to the file functions.php inside the folder with the current active theme (template) of the site. This can be done in a file manager or via FTP.

The path to the functions.php file looks like this: /path_to_site_root/wp-content/themes/theme_name/functions.php.

Add at the very beginning of the file after the tag

Add_action("init", "stop_heartbeat", 1); function stop_heartbeat() ( wp_deregister_script("heartbeat"); )

These lines disable HeartBeat for the entire site.

HeartBeat Control plugin

There is also a less radical solution than completely disabling the HeartBeat API. It is provided by a special plugin HeartBeat Control. Install it, activate it and go to settings (Tools -> HeartBeat Control). In settings you can select the following options:

1. Whether to disable HeartBeat for the entire site or allow its use only for certain sections of it, for example, for the page for editing and publishing posts in the administrative panel.

2. Frequency of requests to the admin-ajax.php file. By default it can be 15 seconds - change this value, for example, to 60 seconds, and thereby Reduce resource consumption for these queries by as much as 75%!

What is important to remember

  • By disabling the HeartBeat API, we disabled the autosave feature. Save edited pages and posts periodically (as needed).
  • Since the HeartBeat API is disabled at the theme (template) level of the site, as soon as you change the theme (template), the HeartBeat API will work again. The disabling procedure will need to be repeated for each new theme you start using on the site.
  • The same applies to updating the current theme: after updating, the shutdown procedure must be repeated.

We wish you a pleasant work!

I was forced to create this post by a problem that began to knock on my site more and more insistently. The problem is opening the site pages and working in the admin panel. They became so strong and frequent that it became difficult to compose even a small message. The connection to the server kept dropping. High load on the CPU, which means exceeding the so-called soft limit, occurred constantly; I started receiving email messages very often from Metrica and the Jetpack plugin, stating that the site was often unavailable, even at a time when I was not working on it. In short, a terrible horror overtook me again with the problem of site speed. And I again contacted the support service at my hosting “Gino”

The answer came immediately, and with further dialogue with small explanations, I understood the essence of the problems and was able to fix them.

Hosting support first of all drew my attention to the large number of requests to /wp-admin/admin-ajax.php.“Requests create actions in the site’s administrative panel. This behavior is typical if one of the plugins does not work correctly. Disable site plugins one by one and monitor its operation. If you find a problematic plugin, try updating it or installing a stable version, or replace it with an analogue that does not cause problems.”

There are quite a lot of plugins installed on my site and checking each one individually is very problematic. Therefore, I paid attention to the link that technical support left me for the plugin Heartbeat Control. Installing it and some other settings solved my problem!

The technical support service also pointed out another problem and suggested taking some steps to correct the code in the wp-cron.php file. But about this problem at the end of the message.

Well, now let's move on to the essence of the above.

What is the Heartbeat API on WordPress and how to limit it

The word “Heartbeat” is translated as “Heartbeat” and it is called that for the API for a reason. It provides a certain frequency of requests to the server. By default and depending on the admin panel page, this happens at different intervals: when editing records - every 15 seconds, when working in the Console - one minute. The WordPress Heartbeat API is a feature that provides:

  • Autosave when creating messages;
  • Blocking posts or pages for other site editors while a post or page is edited by one person;
  • Update information that the user is still in the administrative part (console or post editing page).

This function, written in the WordPres engine, is very important, especially when several authors can work on the site and administration is collective. However, it can also cause the site to slow down and significantly increase the load on the server.

If you are the only author of the site, then the Heartbeat API function can either be completely disabled or configured to a mode that is convenient for you, allowing you to regulate the load on the server.

Completely disabling the Heartbeat API

This hard case scenario, which I didn't want to do, is that you need to do some operation in the functions.php file of your theme currently installed on the site.

Add_action("init", "stop_heartbeat", 1); function stop_heartbeat() ( wp_deregister_script("heartbeat"); )

Of course, when updating this theme or replacing it with another, you will have to do this operation again.

Heartbeat Control plugin and its settings

There are only two settings and, despite the fact that the plugin is in English, for those who do not know this language, it will not be difficult to understand them. Let me remind you that setting up the plugin use default means that the Heartbeat API is not disabled on any page, and the request interval to the server is 15 seconds. That is, everything looks as if this plugin is not on your site.

4. Using settings for admin panel pages

For the sake of clarity, I will clarify:

1. use default - use the Heartbeat API by default;
2. disable everywhere — disable Heartbeat API everywhere;
3. disable on dashboard page — disable Heartbeat API on the dashboard page (console);
4. allow onli from post edit pages - use the Heartbeat API only when editing pages.

2. Adjusting the frequency of requests to the server

Everything is very simple here. You just need to select the appropriate interval of requests to the north. The rarer the “pulse” - the rarer the requests - the less the load on the server.

After making the settings, save the changes.

Since automatic saving of recordings during their editing remains a desirable function (you can delete unnecessary edits later by cleaning them with special plugins), and losing an almost completed recording for some technical reason is unpleasant (lost Internet or connection with the server, etc.) is very undesirable . Therefore, the setting for the first block in the field can be set to “use default” mode, but it will be quite enough to make the settings in the second block, since setting the request frequency timer to the admin-ajax.php file. V 60 sec. will reduce resource consumption for these requests by as much as 75%!

Of course, there are many reasons why websites may slow down. Without the proper knowledge and working with them, we simply do not know what causes such very undesirable processes. We probably need to contact specialists more often, including hosting technical support.

At the beginning of the article, I mentioned that support workers brought to my attention another reason for the difficulty in the operation of my sites. Reason -

Unwanted processes in the wp-cron.php file

Technical support, as a result of researching the operation of my sites, noticed that “wp-cron.php processes are observed” on them. They recommended: "If running this file is causing an unhealthy load on the server - you can disable these tasks."

  1. To do this in the WordPress configuration file wp-config.php add the line:
    define('DISABLE_WP_CRON', 'true');
    You can add it somewhere after installing the language, i.e. after the line
    define('WPLANG', 'ru_RU');
  2. Another solution is in the file itself wp-cron.php comment out the line: wp-cron.php ignore_user_abort(true); To do this, you need to precede it with a double slash. It will turn out like this: //ignore_user_abort(true); This line is located in row 12 of the file.

That's all, friends.

(Visited 304 times, 1 visits today)