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.


Keep yourself in the loop!

This field is hidden when viewing the form
This field is hidden when viewing the form
This field is hidden when viewing the form


19 responses to “Custom AJAX Endpoints in 2.4”

  1. […] 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 […]

    1. No – this is for frontend requests.

  2. […] Custom Ajax Endpoints for performance […]

  3. Yeah! Working twice faster than ever before, Thanks!

  4. […] 2.4 also introduces custom AJAX endpoints, which make it more performant for AJAX requests. The version 2.4 beta announcement had a good […]

  5. 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.
    http://www.vintagegifts.nl/?wc-ajax=get_refreshed_fragments

    thanks

    1. You need to exclude ajax calls from cache – so exclude anything containing wc-ajax.

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

      2. Mike

        Can you help me please?

        thanks

        1. Not really; not familiar with WP Rocket. Look at their docs/support area.

          1. I’m going to do thanks

  6. 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

  7. 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!

    1. We don’t use wp-ajax any more 🙂

      1. 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.

      2. 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.

  8. 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.

    https://gist.github.com/johnmontfx/719658db79be0780fab5

    Am I misunderstanding how this works?

  9. Presumably john, you have to register the function with wc_Ajax.. in Line 1, not wp_Ajax…

Leave a Reply

Your email address will not be published. Required fields are marked *