• Resolved David Molnar

    (@dave82-1)


    Hi,

    I want to use restricted access for REST API, but want to set an exclude, for one specific REST API endpoint, for public read-only. How can I do this?

    I tried this but not work:

    <?php
    function allow_specific_rest_endpoint( $result, $request, $route, $handler ) {

    $route = $request->get_route();

    // Allow access if the route is /wp/v2/pages
    if ( stripos( $route, 'wp/v2/pages' ) !== false ) {
    return $result; // Allow the request to proceed
    }

    return $result; // Allow all other requests (default behavior)

    }

    add_filter( 'rest_dispatch_request', 'allow_specific_rest_endpoint', 99, 4 );
    ?>
Viewing 2 replies - 1 through 2 (of 2 total)
  • Hi @dave82-1,

    Since you are hooking into the rest_dispatch_request filter with priority 99 your callback will be executed after the callback hooked (at priority 10) by the SolSec plugin. This means the $result argument will contain a WP_Error by the time it’s value is passed into your callback. So all you are doing is returning the WP_Error.

    Instead hook 2 callbacks into the filter. One at a lower priority than 10 that only saves the value of $result into a global (and returns the $result value untouched), and one at a higher priority that returns the global value when the rest route contains /wp/v2/pages (Tested and it seems to work).

    +++ To prevent any confusion, I’m not SolidWP +++

    Plugin Support chandelierrr

    (@shanedelierrr)

    Thank you for the swift assist @nlpro, we appreciate you!

    @dave82-1, checking in here to see if you were able to give nlpro’s recommendation a try.

    If you’re all set and since we haven’t received a response, I’ll mark this post resolved. If you still require further assistance, please let us know.

    Thank you!

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