WordPress core offers plugin developers a certain endpoint which they can use for AJAX requests. This endpoint is
wp-admin/admin-ajax.php. Despite it’s naming, it can be used for both frontend and admin ajax requests, and can even be used for non-logged in users.
We have found one drawback with the use of this endpoint however. It loads WordPress admin, and in certain cases this can be a huge performance issue because of that extra overhead.
To help improve performance of frontend AJAX requests (such as add to cart and checkout) we’ve implemented Custom AJAX Endpoints in WooCommerce 2.4.
These use a new endpoint: /wc-ajax/request-name
Because this endpoint is not within admin, it will not load the WP administration area, resulting in a faster request. As an example, in testing, adding items to the cart was around 40% faster using the new endpoints.
There are two possible gotchas which users need to be aware of with the new endpoints.
1. If using caching plugins, the wc-ajax endpoint, like the wc-api endpoint, needs to be excluded from cache.
2. If forcing SSL or non-SSL for certain URLs, /wc-ajax/ should be allowed to work with either. Because this is an AJAX request, secure pages will need to use a secure ajax endpoint, and non-secure pages will need to use a non-secure ajax endpoint to avoid “Access-Control-Allow-Origin” errors.
Check with your web host if you’re unsure of either of these points.
One final note, if you need to detect AJAX requests for any reason via code, our AJAX endpoint will ensure the standard DOING_AJAX constant is set, as well as a custom WC_DOING_AJAX constant.
If you would like to view the code behind these new endpoints, see the class-wc-ajax.php file here.