• Resolved stepantwnty

    (@stepantwnty)


    I have custom endpoint which looks as follow:
    product/v1/get/(?P<id>[0-9-]+)

    I try to add this to wp_rest_cache/allowed_endpoints filter but it doesn’t work. Could you help?

    Here is my full code:

    function wprc_add_acf_posts_endpoint( $allowed_endpoints ) {
    if ( !isset( $allowed_endpoints[ 'product/v1' ] ) || !in_array( '/get/(?P<id>[0-9-]+)', $allowed_endpoints[ 'product/v1' ] ) ) {
    $allowed_endpoints[ 'product/v1' ][] = '/get/(?P<id>[0-9-]+)';
    }
    return $allowed_endpoints;
    }
    add_filter( 'wp_rest_cache/allowed_endpoints', 'wprc_add_acf_posts_endpoint', 10, 1);

    • This topic was modified 1 year, 7 months ago by stepantwnty.
Viewing 1 replies (of 1 total)
  • Plugin Author Richard Korthuis

    (@rockfire)

    Hi @stepantwnty

    Thank you for using our plugin!

    I see you marked your topic already as resolved, but just for anyone who reeds it I will still answer it.

    You should omit the regex part for the id from the allowed endpoints array. So your code should look like this:

    function wprc_add_acf_posts_endpoint( $allowed_endpoints ) {
    	if ( !isset( $allowed_endpoints[ 'product/v1' ] ) || !in_array( 'get', $allowed_endpoints[ 'product/v1' ] ) ) {
    		$allowed_endpoints[ 'product/v1' ][] = 'get';
    	}
    	return $allowed_endpoints;
    }
    add_filter( 'wp_rest_cache/allowed_endpoints', 'wprc_add_acf_posts_endpoint', 10, 1);
Viewing 1 replies (of 1 total)
  • The topic ‘How to add custom endpoint for single post’ is closed to new replies.