Skip to content

How-to Guides

Technical References

Caching /

Using WP_Rewrite instead of _GET parameters to leverage full page caching

Often in code we’re used to doing something like this http://example.com/my-great-article/?all_pages=1. However, this format doesn’t work with full page caching provided by batcache. To leverage full page caching we need to use the WP_Rewrite API.

You can either use rewrite endpoints or use the add_rewrite_rule() and add_rewrite_tag() functions . You can read more about the Rewrite API here. With the help of these functions, you can rewrite your url so that it will now be: http://example.com/my-great-article/all_pages/

You can also speed up ajax requests with this technique. You can create http://example.com/ajax/my_frontend_ajax_function/parameter_1/ to make ajax requests that are cached for 5 minutes for all users with batcache. This can significantly reduce request time. Note that the ajax rewrites are to index.php and not to admin-ajax.php. You are creating a new endpoint that will call your PHP function, not rewriting the query to pass to admin-ajax.php.

Last updated: November 19, 2020