• 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 15 replies - 1 through 15 (of 16 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.

    Thread Starter Jean R.

    (@efbi)

    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?

    Thread Starter Jean R.

    (@efbi)

    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 5 years, 3 months ago by Jean R..
    • This reply was modified 5 years, 3 months ago by Jean R..
    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.

    Thread Starter Jean R.

    (@efbi)

    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

    Thread Starter Jean R.

    (@efbi)

    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?

    Thread Starter Jean R.

    (@efbi)

    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!

    Thread Starter Jean R.

    (@efbi)

    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 5 years, 1 month ago by Jean R..
    • This reply was modified 5 years, 1 month ago by Jean R..
    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!

    Plugin Author Richard Korthuis

    (@rockfire)

    Hi @johancarleborn

    Thank you for using our plugin!

    I don’t see anything wrong with your example, it isn’t working?

Viewing 15 replies - 1 through 15 (of 16 total)
  • The topic ‘Use this plugin with custom rest_url_prefix’ is closed to new replies.