Hi @seanpaulfx
Thank you for using our plugin!
Well you are seeing caches, so it is working. But having said that I do have a remark about your code:
You are mixing namespace and endpoint, or actually you only have a namespace ( example1/v2 ). When registering your endpoint using register_rest_route (https://developer.ww.wp.xz.cn/reference/functions/register_rest_route/) example1/v2 would go into the first parameter (namespace) and you would need to provide an endpoint. Since I am not sure what is in your endpoint, let’s assume you have products in it, then the second parameter (endpoint) would be something like /products/(?P<id>[\d]+). Which would make your call to the endpoint not /wp-json/example1/v2/9 but /wp-json/example1/v2/products/9.
Now the filter for the allowed endpoints would become:
function wprc_add_acf_posts_endpoint( $allowed_endpoints ) {
if ( ! isset( $allowed_endpoints[ 'example1/v2' ] ) || ! in_array( 'products', $allowed_endpoints[ 'example1/v2' ] ) ) {
$allowed_endpoints[ 'example1/v2' ][] = 'products';
}
return $allowed_endpoints;
}
In your Endpoint API Caches I can see the Object Type is still unknown which means our plugin can not detect the object type (i.e. post type or taxonomy type) for your endpoints. So in order for the automatic clearing of the caches to work correctly you would have to add an extra filter, see: https://ww.wp.xz.cn/plugins/wp-rest-cache/#on%20the%20cache%20overview%20page%20i%20see%20the%20object%20type%20is%20%27unknown%27.%20can%20i%20help%20the%20wp%20rest%20cache%20plugin%20to%20detect%20the%20object%20type%20correctly%3F
Here is my code
public function api_route_example1(){
register_rest_route('example1/v2','/spost/(?P<id>\d+)/(?P<page>\d+)',array(
'methods' => WP_REST_Server::READABLE,
'callback' => array($this,'api_action_example1'),
'permission_callback' => '__return_true',
));
}
here is my code, added ‘spost’
Do I need to edit anything further?
And what do I need to do with this object type filter.
function wprc_determine_object_type( $object_type, $cache_key, $data, $uri ) {
if ( $object_type !== 'unknown' || strpos( $uri, $this->namespace . '/' . $this->rest_base ) === false ) {
return $object_type;
}
// Do your magic here
$object_type = 'website';
// Do your magic here
return $object_type;
}
add_filter( 'wp_rest_cache/determine_object_type', 'wprc_determine_object_type', 10, 4 );
Thanks
The code seems to work, but how do I add two versions of api.
I add this code but it doesn’t work.
If I remove this fragment the code works.
strpos( $uri, 'example1/v2/stype' ) === false ) {
return $object_type;
}
function wprc_determine_object_type( $object_type, $cache_key, $data, $uri ) {
if ( $object_type !== 'unknown' || strpos( $uri, 'example1/v2/spost' ) === false || strpos( $uri, 'example1/v2/stype' ) === false ) {
return $object_type;
}
return 'products';
}
add_filter( 'wp_rest_cache/determine_object_type', 'wprc_determine_object_type', 10, 4 );
Thanks
Hi @seanpaulfx
Use this:
function wprc_determine_object_type( $object_type, $cache_key, $data, $uri ) {
if ( $object_type !== 'unknown' || ( strpos( $uri, 'example1/v2/spost' ) === false && strpos( $uri, 'example1/v2/stype' ) === false ) ) {
return $object_type;
}
return 'products';
}
add_filter( 'wp_rest_cache/determine_object_type', 'wprc_determine_object_type', 10, 4 );