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.
19 replies on “Custom AJAX Endpoints in 2.4”
[…] Hedgehog” is now available. There have been ~40 commits since beta 4, including a tweak to the ajax endpoints for wider host support, and some tweaks to the variation editor based on user […]
Hello, is this a resolution to the problem being discussed here?
No – this is for frontend requests.
[…] Custom Ajax Endpoints for performance […]
Yeah! Working twice faster than ever before, Thanks!
[…] 2.4 also introduces custom AJAX endpoints, which make it more performant for AJAX requests. The version 2.4 beta announcement had a good […]
How do I use the wc-ajax endpoint out caching leave?
I use wp rocket
Is it about this file?
Because that gives to test a long wait time.
You need to exclude ajax calls from cache – so exclude anything containing wc-ajax.
How do you do that?
I can not find where I like to look.
And where would I put in WP rocket?
Can you help me feather?
Can you help me please?
Not really; not familiar with WP Rocket. Look at their docs/support area.
I’m going to do thanks
I am prompted to WP Rocket to help me. but they can not
Can you help me if I have the plugin W3 Total Cache use?
Mike, we’re having similar performance issues with wc-ajax on the front end. We’re using WP Rocket and have confirmed with them that the Ajax resources are not being cached, but wp-ajax is adding 500ms+ to page load times. Here’s a report: http://goo.gl/1itPV2
What are your thoughts about what can be done to improve performance? Thanks, Mike!
We don’t use wp-ajax any more 🙂
Thanks, Mike. I meant to write “wc-ajax”, not “wp-ajax”. I did some more testing to be sure it wasn’t a plugin and was WC core and I believe it is. It’s the same issue mentioned in a number of posts in various support forums. Please advise.
Load the endpoint directly to debug – its just a regular non-cached request. If the rest of your site is cached, its logical that its slower.
This seems really brilliant, as I’m wanting to use a more efficient ajax functionality. But I’m curious if this allows me to use the standard registration of ajax functions. I’ve defined a function in functions.php that works fine with the standard wp ajax.
In the following gist (which shows the function/action as well as my html), I tried to swap out line #17 with ‘?wc-ajax= my_ajax_function’ (I also included the full http:// URL) — but now the function isn’t running.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
hosted with ❤ by GitHub
Am I misunderstanding how this works?
Presumably john, you have to register the function with wc_Ajax.. in Line 1, not wp_Ajax…