When you visit a WordPress site, your browser sends a request to the server for the specific page you want to load. For example, when you visit the home page of a WordPress site, your browser requests the home page content from the server.
Similarly, when you click on a link to another page on the same WordPress site, your browser sends a request for that page to the server.
However, when you click on certain elements on a WordPress site, such as a button or link that triggers an AJAX action, your browser sends a POST request to the admin-ajax.php file on the server instead of requesting a specific page.
What does AJAX stand for?
With AJAX, web applications can send data to and receive data from a server asynchronously (in the background) without interfering with the display and behavior of the existing page.
For example, a web application could use AJAX to send data to the server when a form is submitted or when a link is clicked, and then receive data back from the server without reloading the entire page.
What is admin-ajax.php?
Admin-ajax.php is a WordPress core file that handles AJAX requests from the front end of your WordPress site. When you click on an element that triggers an AJAX action, your browser sends a request to admin-ajax.php, which then executes the appropriate code to carry out the action.
For example, if you click on a button to load more posts from a blog archive, the request sent to admin-ajax.php would include information about which action to execute (in this case, "load_more_posts"), as well as any other data required to carry out the action (such as the number of posts to load).
Admin-ajax.php is a powerful WordPress core file that allows you to add AJAX functionality to your WordPress site without having to write any code yourself. However, because admin-ajax.php is a core file, it is not intended for direct modification. If you need to customize the way admin-ajax.php works on your site, you should do so via a plugin or child theme.
We can see that admin-ajax.php loads when a user visits our site. It is a POST method, meaning it is a type of HTTP request that sends data to the server. The POST method is used to send data when submitting a form, for example.
The admin-ajax file uses a PHP handler to process the AJAX request. This means that when a request is made to admin-ajax.php, the PHP code associated with the 'action' being requested is executed.
For example, if the 'action' being requested is "load_more_posts", the PHP code responsible for loading more posts from the archive will be executed.
Admin-ajax.php is an important part of the WordPress platform because it helps to manage and process AJAX requests. This file is used to handle administrative tasks, such as creating or deleting posts, adding new users, or changing passwords. Because this file is so important, it’s crucial to understand the basics. The method is requested on page load but is small and shouldn't increase loading time to your site.