• Resolved pepsiftd

    (@pepsiftd)


    Hi! Great plugin! Great job you’ve done!
    I need a bit of help in using it with custom endpoints though..
    Can I use custom endpoints like /products/(?P<id>[\d]+) ? How do I do that correctly?
    Here’s how I register my rest route:

    function cc_register_rest_routes() {
        register_rest_route( 'cc-rest/v1', '/products/(?P<id>[\d]+)', array(
            'methods'  => WP_REST_Server::READABLE,
            'callback' => 'cc_rest_get_product',
        ) );
    }

    Now when I use this:

    function my_add_products_endpoints_to_cache( $allowed_endpoints ) {
        if ( ! isset( $allowed_endpoints[ 'cc-rest/v1' ] ) || ! in_array( 'products', $allowed_endpoints[ 'cc-rest/v1' ] ) ) {
            $allowed_endpoints[ 'cc-rest/v1' ][] = 'products';
        }
        return $allowed_endpoints;
    }
    add_filter( 'wp_rest_cache/allowed_endpoints', 'my_add_products_endpoints_to_cache', 10, 1);

    I only get the first record cached, eg. /cc-rest/v1/products/176.
    All other requests ‘…/175’ , ‘…/181’ etc. are not.
    And even if I delete the only cache record, the new ones don’t appear (or the old one reappear btw).
    Thanks.

    P.S. So far I’ve been trying this on a local server only so can’t provide a link

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Richard Korthuis

    (@rockfire)

    Hi @pepsiftd

    Thank you for using our plugin!

    As far as I can see from your example code, you are registering your custom endpoint just fine. I don’t see any reason why it would only cache the first record. Are you sure other endpoints have been requested after adding this code? So was there a call to /cc-rest/v1/products/175? And a call to /cc-rest/v1/products/181? Remember it can only cache a record after it has been requested at least once.

    Thread Starter pepsiftd

    (@pepsiftd)

    Hi @rockfire

    Thank you for your reply!

    You’ve verified that it is not the above code that creates the problem for me. So it became clear that it was my fiddling with the code that made the trouble. So I just reinstalled the plugin and it worked!

    Thanks for the support.

    p.s. to somehow atone for the waste of your time I’ll tell about a small issue I had with the same custom route:

    The first time the request went through your plugin, the response contained php warnings which ruined the json string. Warnings pointed to the includes/caching/class-caching.php line 909 saying the $data['data'] was not an array. But 2 lines above it explicitly says it has to be

    // Force data to be an array.
    $data['data'] = json_decode( wp_json_encode( $data['data'] ), true );

    Somehow this line didn’t work for me returning a string.
    This fixed the issue

    // Force data to be an array.
    $data['data'] = (array)json_decode( wp_json_encode( $data['data'] ), true );

    Hope it may help somebody

    • This reply was modified 4 years, 11 months ago by pepsiftd.
    Thread Starter pepsiftd

    (@pepsiftd)

    UPD:
    actually it didn’t fix the issue, just removed the warnings
    I’m not sure why but without wp_json_encode it returns the proper array in my case
    $data['data'] = json_decode( $data['data'] , true );
    forget it 🙂

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Custom endpoint – Only get one entry’ is closed to new replies.