Gold Penguin Logo with Text

What is POST admin-ajax.php in WordPress?

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.
Updated December 10, 2022
code scrambled on a computer monitor as digital art
code scrambled on a computer monitor as digital art

WordPress is a popular content management system (CMS) that is used by many websites. One of the features of WordPress is the ability to use the admin-ajax.php file to handle Ajax requests. 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. 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?

AJAX stands for “Asynchronous JavaScript and XML”. It is a web development technique used to create interactive web applications.

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's admin-ajax.php used for?

Ajax is a technique used by web developers to create interactive and dynamic websites. It allows web pages to communicate with a server and update parts of the page without needing to refresh the entire page. This makes for a smoother and more seamless user experience.

The admin-ajax.php file is a key part of WordPress' implementation of Ajax. It is a PHP script that receives Ajax requests and processes them. WordPress uses this file to handle many different types of requests, such as updating a post, getting a list of posts, or searching for content.

In order to make an Ajax request to admin-ajax.php, you will need to use the jQuery.ajax() function. This function allows you to specify the type of request (e.g. GET or POST), the URL to send the request to, and any data that you want to include with the request. Here is an example of using jQuery.ajax() to make a POST request to admin-ajax.php:

jQuery.ajax({
  type: "POST",
  url: "/wp-admin/admin-ajax.php",
  data: {
    action: "my_action",
    post_id: 123
  },
  success: function(response) {
    // do something with the response
  }
});

In this example, we are sending a POST request to admin-ajax.php with two pieces of data: action and post_id. The action parameter is used to specify which action to perform, and the post_id parameter is used to specify the ID of the post that we want to perform the action on.

Once admin-ajax.php receives the request, it will look for a function with the name my_action and execute it. This function should be registered with WordPress in order for it to be called when the request is received. Here is an example of how to register the my_action function:

function my_action() {
  // get the post_id parameter from the request
  $post_id = $_POST['post_id'];

  // do something with the post
  $post = get_post($post_id);
  $title = $post->post_title;
  $content = $post->post_content;

  // return the post data
  $response = array(
    'title' => $title,
    'content' => $content
  );
  wp_send_json_success($response);
}
add_action('wp_ajax_my_action', 'my_action');
add_action('wp_ajax_nopriv_my_action', 'my_action');

In this example, the my_action function first retrieves the post_id parameter from the request and uses it to get the post data. It then constructs a response containing the post's title and content and sends it back to the client using the wp_send_json_success() function.

In addition to the parameters that you can include with your Ajax requests, there are also several actions that are built into WordPress that you can use. For example, the heartbeat action is used to ping the server and keep the user's session alive. Here is an example of how to use the heartbeat action:

jQuery.ajax({
  type: "POST",
  url: "/wp-admin/admin-ajax.php",
  data: {
    action: "heartbeat"
  },
  success: function(response) {
    // do something with the response
  }
});

Conclusion

As you can see, the admin-ajax.php file is an important part of WordPress' implementation of Ajax. It allows you to send requests to the server and perform various actions, such as updating posts or pinging the server. By using the jQuery.ajax() function, you can easily make Ajax requests toadmin-ajax.php` and take advantage of the many actions that are built into WordPress.

One thing to keep in mind is that the admin-ajax.php file is only intended for use within the WordPress admin area. If you try to make an Ajax request to this file from the front-end of your website, it will not work. In order to make Ajax requests from the front-end of your website, you will need to use a different approach.

Want To Learn Even More?
If you enjoyed this article, subscribe to our free monthly newsletter
where we share tips & tricks on how to use tech & AI to grow and optimize your business, career, and life.
Written by Justin Gluska
Justin is the founder of Gold Penguin, a business technology blog that helps people start, grow, and scale their business using AI. The world is changing and he believes it's best to make use of the new technology that is starting to change the world. If it can help you make more money or save you time, he'll write about it!
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
Join Our Newsletter!
If you enjoyed this article, subscribe to our newsletter where we share tips & tricks on how to make use of some incredible AI tools that you can use to grow and optimize a business
magnifiercross