Categories
Core

Custom AJAX Endpoints in 2.4

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 currentpageurl?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.

By Mike Jolley

I help build things at Automattic.

19 replies on “Custom AJAX Endpoints in 2.4”

Mike,
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?

Like

Hello Mike

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?

thanks

Like

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!

Like

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.

Like

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.

Am I misunderstanding how this works?

Like

Comments are closed.