What’s the point of using WordPress’s built in admin-ajax.php?

First, like many things in WordPress, it’s not like someone decided “let’s make this neat and convenient”. It was more like someone slapped it together for something, then it got used more in admin, then it got used a lot in admin, then it became kind of practice to use it for non-admin as well. Or something along these lines.

Second, it’s not as much what it does, as knowing that it always does same thing. You have no idea what request to the arbitrary part of front end is doing. What if analytics plugin is running, counting it as page view? What if there is complex redirect logic that handles seasonal URLs? What if, what if…

Ajax endpoint is meant for ajax:

  • it declares DOING_AJAX constant for context, many things look for it to skip doing whatever they are doing
  • it sets up HTTP headers and stuff
  • it gives you basic code organization convention, including user/anonymous distinction