Support » Plugin: WP REST Cache » Use this plugin with custom rest_url_prefix

  • Hello, I change my prefix api from wp-json from app

    But it’s seem your plugin doesn’t work with that.
    How to use the cache on custom url prefix ?

    Thanks

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

    (@rockfire)

    Hi @efbi

    Thank you for using our plugin!

    How did you change the REST URL prefix? Because if you did use the WordPress rest_url_prefix filter it should work automatically.

    Here’s the code I use to change wordpress base url :

    /**
     * Modify url base from wp-json to 'app'
     */
    add_filter( 'rest_url_prefix', 'api_slug');
     
    function api_slug( $slug ) {
     
        return 'app';
    }
    Plugin Author Richard Korthuis

    (@rockfire)

    Hi @efbi

    I have done some testing with your code added to my environment and it is working as expected. The prefix is changed and the cache is working.

    Are you sure it isn’t working for you?

    Well maybe I need to give more explanations

    after renaming the base, I build custom endpoints. For example :

    add_action( 'rest_api_init', function () {
    	register_rest_route( 
    		'api', 
    		'/events', 
    		array(
    			'methods' => 'POST',
    			'callback' => 'get_events',
    		) 
    	);
    });

    Then I use a WP_Query and echo a json output for my mobile app.

    So I tried :

    function wprc_add_acf_posts_endpoint( $allowed_endpoints ) {
        if ( ! isset( $allowed_endpoints[ 'api' ] ) || 
             ! in_array( 'events', $allowed_endpoints[ 'api' ] ) ) {
            $allowed_endpoints[ 'api' ][] = 'events';
        }
        return $allowed_endpoints;
    }
    add_filter( 'wp_rest_cache/allowed_endpoints', 'wprc_add_acf_posts_endpoint', 10, 1);

    Maybe your plugin doesn’t handle that ?

    • This reply was modified 3 months, 4 weeks ago by abnelito.
    • This reply was modified 3 months, 4 weeks ago by abnelito.
    Plugin Author Richard Korthuis

    (@rockfire)

    Hi @efbi

    The problem lies with:
    'methods' => 'POST',
    Our plugin only caches GET requests, since all other requests (POST, PUT, PATCH, DELETE) are meant to change things. Only GET is for retrieving data.

    Arf.. can’t put my requests in GET for security issues

    Too bad, your plugin seemed awesome !

    Plugin Author Richard Korthuis

    (@rockfire)

    Hi @efbi

    But you are only returning results using the POST? (So not changing any data?)

    If so, maybe we can think of a way to make this work for you 🙂

    Maybe we should consider adding a filter hook so you can tell the plugin to also cache POST-requests?
    Could you try if it works correctly for you if you change the line
    if ( 'GET' !== filter_var( $_SERVER['REQUEST_METHOD'], FILTER_SANITIZE_STRING ) ) {
    (in wp-rest-cache/includes/api/class-endpoint-api.php on line 233) to:
    if( in_array( filter_var( $_SERVER['REQUEST_METHOD'], FILTER_SANITIZE_STRING ), [ 'GET', 'POST' ], true ) ) {
    If so, I will discuss it internally if we can add a filter so you can tell the plugin to also cache POST-requests

    Yes my requests only returning results ! Cause wp api is restricted so I need some more complex queries

    It would be great !

    Plugin Author Richard Korthuis

    (@rockfire)

    Did you try the code change? Does that work for you?

    Well I just tried it, and it doesn’t appear in the “Endpoint Api Caches” tab
    Maybe I do wrong :

    I change the base app from wp-json to app

    My api full url is like : https://domain.com/app/api/events

    I changed the line like you did, then I used this code :

    function posts_endpoint( $allowed_endpoints ) {
        if ( ! isset( $allowed_endpoints[ 'api' ] ) || 
             ! in_array( 'events', $allowed_endpoints[ 'api' ] ) ) {
            $allowed_endpoints[ 'api' ][] = 'events';
        }
        return $allowed_endpoints;
    }
    add_filter( 'wp_rest_cache/allowed_endpoints', 'posts_endpoint', 10, 1);

    It’s good ?

    Plugin Author Richard Korthuis

    (@rockfire)

    Hi @efbi

    Sorry for not responding any sooner. We just released a new version of our plugin, which includes a filter to filter te allowed request methods. You can use it like this:

    function wprc_filter_allowed_request_methods( $allowed_request_methods ) {
    	$allowed_request_methods[] = 'POST';
    	
    	return $allowed_request_methods;
    }
    add_filter( 'wp_rest_cache/allowed_request_methods', 'wprc_filter_allowed_request_methods', 10, 1 );

    Please let us know if this works for you!

    Hi @rockfire , thanks for this update !

    How should I use this now ?

    I put both code in my functions.php :

    function wprc_filter_allowed_request_methods( $allowed_request_methods ) {
    	$allowed_request_methods[] = 'POST';
    	
    	return $allowed_request_methods;
    }
    add_filter( 'wp_rest_cache/allowed_request_methods', 'wprc_filter_allowed_request_methods', 10, 1 );
    
    function wprc_add_acf_posts_endpoint( $allowed_endpoints ) {
        if ( ! isset( $allowed_endpoints[ 'api' ] ) || 
             ! in_array( 'events', $allowed_endpoints[ 'api' ] ) ) {
            $allowed_endpoints[ 'api' ][] = 'events';
        }
        return $allowed_endpoints;
    }
    add_filter( 'wp_rest_cache/allowed_endpoints', 'wprc_add_acf_posts_endpoint', 10, 1);
    • This reply was modified 2 months, 1 week ago by abnelito.
    • This reply was modified 2 months, 1 week ago by abnelito.
    Plugin Author Richard Korthuis

    (@rockfire)

    Hi @efbi

    Yes, that should work. Or are you still having problems?

    Hi @rockfire
    I have a similar question to this. Can’t get this to work, how should I set it up?

    Full link:
    https://supermiljobloggen.se/wp-json/lightweb/doSearch

    The register:

    register_rest_route(
      'lightweb',
      'doSearch',
      array(
        'methods' => 'GET',
        'callback' => 'doSearch'
      ) 
    );

    And:

    function wprc_add_acf_posts_endpoint( $allowed_endpoints ) {
        if ( ! isset( $allowed_endpoints[ 'lightweb' ] ) || ! in_array( 'doSearch', $allowed_endpoints[ 'lightweb' ] ) ) {
            $allowed_endpoints[ 'lightweb' ][] = 'doSearch';
        }
        return $allowed_endpoints;
    }
    add_filter( 'wp_rest_cache/allowed_endpoints', 'wprc_add_acf_posts_endpoint', 10, 1);

    Help would be much appreciated.
    Thank you!

Viewing 14 replies - 1 through 14 (of 14 total)
  • You must be logged in to reply to this topic.