• Resolved lucas8124

    (@lucas8124)


    Hello!

    First of all, thank-you for this awesome plugin. I love it. I am using JSON API Plus plugin to have additional endpoints, because this is a plugin I have a custom route for this so it doesn’t interfere with the default wordpress routes. Here is what my custom routes look like:

    http://mysite/api/${endpoint}

    http://mysite/api/get_recent_posts/ -> returns recent posts

    I have NO intention on using any of the default WordPress routes (wp-json/…), I am a bit confused on how I can add http://mysite/api/get_recent_posts/ to the rest api cache. I have tried changing route prefix and other things with no avail. Is there a simple way I can do this with a callback / function?

    Any help would be very much appreciated.

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

    (@rockfire)

    Hi @lucas8124

    Thank you for using our plugin! And sorry for not replying any sooner.

    Our plugin by default checks if the call is to the REST URL, which should start with the REST URL prefix which by default is wp-json. You said you have tried changing the route prefix, did you use the WordPress filter rest_url_prefix (https://developer.wordpress.org/reference/hooks/rest_url_prefix/) to change the prefix from wp-json into api? I am not sure if this might result in a conflict between the WP REST API and your custom route. If it doesn’t then the next step would be to register your endpoint for caching like explained here: https://wordpress.org/plugins/wp-rest-cache/#can%20i%20register%20my%20own%20endpoint%20for%20caching%3F But we run into a small problem here since the allowed endpoints array expects a namespace and a route. But since we have defined api as the rest url prefix all we have left is get_recent_posts. So let’s try a little ‘hack’:

    function wprc_add_custom_endpoint( $allowed_endpoints ) {
        if ( ! isset( $allowed_endpoints[ 'get_recent_posts' ] ) ) {
            $allowed_endpoints[ 'get_recent_posts' ][] = '';
        }
        return $allowed_endpoints;
    }
    add_filter( 'wp_rest_cache/allowed_endpoints', 'wprc_add_custom_endpoint', 10, 1);

    I have not tried this, since I am not sure which plugin you are refering to, but I guess this should work.

    Please let us know if this works for you, if not could you please supply a link to the plugin you are using for your endpoint?

Viewing 1 replies (of 1 total)
  • The topic ‘Custom rest API prefix’ is closed to new replies.