• Hi,

    Our Endpoint: https://tejtips.com/wp-json/tej/v1/events?tmz=Europe/Lisbon&idiom=pt&mod=2
    (We also tested without query vars: https://tejtips.com/wp-json/tej/v1/events)

    Our route:

    register_rest_route('tej/v1', 'events', array(
        'methods' => 'GET',
        'callback' => 'GetEvents',
        'args' => array(
            'tmz' => array(
                'required' => false,
                'validate_callback' => function ($param, $request, $key) {
                    ...
                }
            ),
            'idiom' => array(
                'required' => false,
                'validate_callback' => function ($param, $request, $key) {
                    ...
                }
            ),
            'mod' => array(
                'required' => false,
                'validate_callback' => function ($param, $request, $key) {
                    ...
                }
            )
        ),
        'permission_callback' => '__return_true'
    ));

    Our filter

    function wprc_add_next_events_endpoint( $allowed_endpoints ) {
        if ( ! isset( $allowed_endpoints[ 'tej/v1' ] ) || ! in_array( 'events', $allowed_endpoints[ 'tej/v1' ] ) ) {
            $allowed_endpoints[ 'tej/v1' ][] = 'events';
        }
        
        return $allowed_endpoints;
    }
    add_filter('wp_rest_cache/allowed_endpoints', 'wprc_add_next_events_endpoint', 10, 1);

    In the backoffice, in the “Endpoint API Caches” tab, our endpoint does not appear and we did not notice any improvements in behavior (response time reduction, for example).
    We verify that the route is added to the $allowed_endpoints looking something like:

    "tej/v1":[
       "events"
    ]

    It looks good to us but we never have the desired behavior…
    Can you help?
    Thanks!

Viewing 9 replies - 1 through 9 (of 9 total)
  • Thread Starter ttips

    (@ttips)

    The wp-rest-cache.php file is also present in the wp-content/mu-plugins directory.
    If we test with endpoints like “/wp-json/wp/v2/posts”, the plugin works correctly.

    Plugin Author Richard Korthuis

    (@rockfire)

    Hi @ttips

    Thank you for using our plugin!

    I don’t see anything wrong with your code. I do see in your response headers that you are using cloudflare. Could it be that cloudflare is already caching it? Although that doesn’t seem logical since the posts endpoint is being cached by our plugin.

    Thread Starter ttips

    (@ttips)

    Hi @rockfire,

    Are there any details that are missing?
    Should “add_filter” be called after “add_action(‘rest_api_init’,…)”? (we’ve tested it before and after and it’s the same).
    We have “register_rest_route” inside “add_action(‘rest_api_init’,…)”. It’s fine like this?

    Thank you!

    Plugin Author Richard Korthuis

    (@rockfire)

    Hi @ttips

    We process the filter from the init and the rest_api_init action hook with default (i.e. ’10’) priority. We save the results to the database (this is because the actual cache check is done before any plugin (and therefore any hook/filter) is run), so the best way to check if your changes are saved to the allowed endpoint is to check the wp_options table and check for the record where option_name is wp_rest_cache_allowed_endpoints. Your endpoint should be somewhere in that record.

    Thread Starter ttips

    (@ttips)

    Hi @rockfire,

    Yes it is there but still not caching…

    Thread Starter ttips

    (@ttips)

    Hi,

    And in the wp_rest_cache_item_allowed_endpoints option?
    Nothing appears on my endpoint….

    • This reply was modified 2 years, 5 months ago by ttips.
    Plugin Author Richard Korthuis

    (@rockfire)

    Hi @ttips

    You can ignore that last option, that is only used for WP core endpoints.

    I just did a test on my local machine where I used your code to register the route (and created some dummy output) and add the filter. It works as expected and caches the endpoint. So if it isn’t working on your side it has something to do with other code, because this is working for me.

    Hi, I’m having the same issue. The plugin is caching only the default endpoints.

    Plugin Author Richard Korthuis

    (@rockfire)

    @tedcarter Could you open your own topic? Because your issue might seem the same, but it could have a completely different cause.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Does not cache my custom endpoint’ is closed to new replies.