• Resolved m777

    (@m777)


    Hello
    I’ve tried a lot of times this plugin with custom methods and added function below but cache doesn’t works anyway.(

    
    add_action( 'rest_api_init', function () {
        register_rest_route( 'places', 'all', array(
            'methods' => 'GET',
            'callback' => 'api_get_places_all',
        ) );
    } );
    

    and tried with version define like this:

    
    add_action( 'rest_api_init', function () {
        register_rest_route( 'v1/places', 'all', array(
            'methods' => 'GET',
            'callback' => 'api_get_places_all',
        ) );
    } );
    

    ——

    function wprc_add_places_list_endpoint( $allowed_endpoints ) {
        if ( ! isset( $allowed_endpoints[ 'places' ] ) || ! in_array( 'all', $allowed_endpoints[ 'places' ] ) ) {
            $allowed_endpoints[ 'places' ][] = 'all';
        }
        return $allowed_endpoints;
    }
    
    add_action( 'wp_rest_cache/allowed_endpoints', 'wprc_add_places_list_endpoint', 10, 1);

    Maybe plugin require any server settings for normal functionality?

    • This topic was modified 2 years, 9 months ago by m777.
    • This topic was modified 2 years, 9 months ago by m777.

    The page I need help with: [log in to see the link]

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

    (@rockfire)

    Hi @m777

    Sorry for the late reply.

    I don’t see anything wrong with your code. But just to be sure I recreated it on my local environment and this is working code:

    add_action( 'rest_api_init', function () {
    	register_rest_route( 'places/v1', 'all', array(
    		'methods' => 'GET',
    		'callback' => 'api_get_places_all',
    	) );
    } );
    
    function wprc_add_places_list_endpoint( $allowed_endpoints ) {
    	if ( ! isset( $allowed_endpoints[ 'places/v1' ] ) || ! in_array( 'all', $allowed_endpoints[ 'places/v1' ] ) ) {
    		$allowed_endpoints[ 'places/v1' ][] = 'all';
    	}
    	return $allowed_endpoints;
    }
    
    add_filter( 'wp_rest_cache/allowed_endpoints', 'wprc_add_places_list_endpoint', 10, 1);

    So calls to https://<yourwebsite>/wp-json/places/v1/all should now be cached.

    Please keep in mind that the first time you call the endpoint it might not be cached right away, but after a second call it should be cached (this has to do with the technical setup of the caching and the filters).

Viewing 1 replies (of 1 total)
  • The topic ‘Cache doesn’t work’ is closed to new replies.