• Resolved Anderson So

    (@andersonso)


    Hello,

    I have this url – https://www.bentahero.com/wp-json/adforest/v1/home

    I wanted to cache the home page of the app.

    I added the code below to functions.php but its not working…

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

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

    (@rockfire)

    Hi @andersonso

    Thank you for using our plugin!

    The correct structure of the $allowed_endpoints is:
    $allowed_endpoint[ <namespace> ] => array of endpoints

    So in your case the correct code should be:

    function wprc_add_home_endpoint( $allowed_endpoints ) {
        if ( ! isset( $allowed_endpoints[ 'adforest/v1' ] ) || ! in_array( 'home', $allowed_endpoints[ 'adforest/v1' ] ) ) {
            $allowed_endpoints[ 'adforest/v1' ][] = 'home';
        }
        
        return $allowed_endpoints;
    }
    add_filter('wp_rest_cache/allowed_endpoints', 'wprc_add_home_endpoint', 10, 1);
    Thread Starter Anderson So

    (@andersonso)

    Hi @rockfire, it works! Thank you very much!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Custom Endpoint’ is closed to new replies.