• Resolved afurnisstap

    (@afurnisstap)


    Hi there,

    I’m very new to WordPress but I am a developer trying to add caching to an existing WP REST API and I have come across your plugin hoping it can help. I have however, never used PHP in my life and never edited plugin code before so I’m just looking for a bit of basic assistance! I am using the ACF to REST API plugin and have various endpoints that I’d like to add browser caching to such as:

    /wp-json/acf/v3/pages/1611
    /wp-json/acf/v3/pages/59
    /wp-content/uploads/2019/03/HomepageMobileMar19-3.jpg
    /wp-json/acf/v3/get_blog_block_posts

    I have seen your FAQ on adding code for a custom endpoint but frankly I have no idea where to put this. Does it just go in wp-rest-cache.php?

    Thanks in advance ??

Viewing 9 replies - 1 through 9 (of 9 total)
  • Plugin Author Acato

    (@acato)

    Hi @afurnisstap,

    Thank you for using our plugin.

    It is bad practice to edit a plugin, since your changes would be lost when the plugin is updated. Most frequently custom code is simply added to the functions.php of your theme, but keep in mind if you are not using your own theme these changes might also be lost when updating the theme. If so, another option is to create a child theme for all your custom code. See: https://developer.www.ads-software.com/themes/advanced-topics/child-themes/ for instructions on how to create a child theme.

    Thread Starter afurnisstap

    (@afurnisstap)

    Excellent! Thank you Acato!

    With your help I’ve managed to make some progress and have found functions.php in my theme. I copied you FAQ code from the link I previously provided and changes ‘posts’ the ‘pages’ for my use case.

    /**
     * Register the /wp-json/acf/v3/pages endpoint so it will be cached.
     */
    function wprc_add_acf_pages_endpoint( $allowed_endpoints ) {
        if ( ! isset( $allowed_endpoints[ 'acf/v3' ] ) || ! in_array( 'pages', $allowed_endpoints[ 'acf/v3' ] ) ) {
            $allowed_endpoints[ 'acf/v3' ][] = 'pages';
        }
        return $allowed_endpoints;
    }
    add_filter( 'wp_rest_cache/allowed_endpoints', 'wprc_add_acf_pages_endpoint', 10, 1);

    I have added this at the top of functions.php and saved the file. I’m still getting max-age=1 when sending a GET request to a page URL such as /wp-json/acf/v3/pages/1611 though (I have set the Cache timeout setting in Settings > WP REST Cache to 1 hour). Any ideas where I’m going wrong?

    Plugin Author Acato

    (@acato)

    Hi @afurnisstap

    We try to alter the response as little as possible, so the max-age is untouched (for now, maybe we should consider to change it). There are two ways to check if the request is using the cache:

    1. Check the reponse headers for X-WP-Cached-Call, if it is present and is set to served-cache than the cache is working.
    2. Check if your request is in the list wp-admin > Settings > WP REST Cache > Endpoint API Caches.
    Thread Starter afurnisstap

    (@afurnisstap)

    Ahh I see!

    OK, so I can see a few entries in the Endpoint API Caches list such as:

    /wp-json/acf/v3/pages/38
    /wp-json/acf/v3/pages/7
    /wp-json/acf/v3/pages/285

    And the response in Postman does have the X-WP-Cached-Call header for these requests so it is working to an extent but the example I gave previously (/wp-json/acf/v3/pages/1611) is nowhere to be seen and the X-WP-Cached-Call header is not present for that one. Is there some condition not being met that means some are cached but some aren’t?

    One further question regarding what you’ve said about not setting the max-age – will this mean that page speed tools like https://www.webpagetest.org/ and Google PageSpeed won’t recognise that the item is being cached? That’s the main reason I’m doing all of this so I’d hope for the caching to be picked up by those tools! Currently https://www.webpagetest.org/ is telling me:

    FAILED – (1 seconds) – https://domain.com/wp-json/acf/v3/pages/1611

    …which I’m guessing is based off the max-age?

    Thread Starter afurnisstap

    (@afurnisstap)

    I just re-published/updated the page and now it’s appearing in the Endpoint API Caches list! I will update once I know if it resolves the issues given to me by page speed tests…

    Plugin Author Acato

    (@acato)

    Hi @afurnisstap

    Given the fact that there are already cache-entries for the /wp-json/acf/v3/pages/ endpoint, I don’t see any reason why that one specific response isn’t cached. The only reason why a response is not cached by the plugin is if the original call is returning an error by either returning a HTTP Code other than 200, or if the returned JSON isn’t valid JSON.

    About the pagespeed test: It could very well be because of the max-age. When we developed this plugin it was not to score on pagespeed tests, but to speed up the WordPress REST API. I will add it to a list of possible improvements of the plugin and discuss it internally. As a quick fix I will try to (later today or at least this week) implement a filter hook so you can change the max-age value.

    Thread Starter afurnisstap

    (@afurnisstap)

    Great, thanks @acato. All understood.

    A filter hook to set the max age would be great if possible.

    Plugin Author Acato

    (@acato)

    Hi @afurnisstap

    I have just added two filters which allow you to change the headers for the cached response:

    Add the cache control header if it is not present:

    /**
     * Add Cache-Control header if not present.
     */
    function wprc_filter_headers( $headers, $request_uri ) {
        if(!array_key_exists('Cache-Control', $headers)) {
            $headers['Cache-Control'] = 'max-age=3600';
        }
        return $headers;
    }
    add_filter('wp_rest_cache/cache_headers', 'wprc_filter_headers', 10, 2);

    Set the max-age to 3600:

    /**
     * Set max-age to 3600.
     */
    function wprc_filter_header($header_value, $header_name, $request_uri) {
        if($header_name == 'Cache-Control'){
            return 'max-age=3600';
        }
        return $header_value;
    }
    add_filter('wp_rest_cache/cache_header', 'wprc_filter_header', 10, 3);

    A new version of the plugin is now available. Please note: the headers are also cached, so you will have to clear the cache in order for the filters to work.

    • This reply was modified 6 years ago by Acato.
    Thread Starter afurnisstap

    (@afurnisstap)

    Excellent. Thank you so much for getting this out so quickly. Good job!

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Where to add custom domain code’ is closed to new replies.