REST route with single quote in path expression
-
I’m trying to implement a custom route for the REST API which will take a member’s surname and membership number and validate if they are really a member. My custom endpoint uses a path expression and ends …/status/(?P<surname>[\w-]+)/(?P<memno>[\w-]{1,7}). This worked well until I tested it with the name O’Malley. The path expression didn’t match the apostrophe (single quote) so I got a “no route was found” response.
So I changed the path expression to (?P<surname>[\w’-]+)/(?P<memno>[\w-]{1,7}) but this didn’t work, I tried a number of variations on the expression without success. Digging into the code I found that it was because WordPress had added a backslash in front of the apostrophe in the path from the URL. I could get it to work if I changed the expression to (?P<surname>[\w\\’-]+)/(?P<memno>[\w-]{1,7})a, but this matches any number of backslashes in the name, which feels a bit of a hack to me. So then I wondered if I could filter the path and remove the backslash, but the best I could come up with was this:
add_filter( 'rest_pre_dispatch', array( $this, 'rest_pre_dispatch' ), 10, 3 );
public function rest_pre_dispatch( $result, $instance, $request ) {
$route = $request->get_route();
// is the request for our namespace?
if ( preg_match( '/musa-member-display/', $route ) ) {
$request->set_route( wp_unslash( $route ) );
}
return $result;
}
This now works, but there’s a further complication. I’m using the Disable REST API plugin so that only logged in users can access the API, but I need to tell it to whitelist my custom route and the single quote in the expression corrupts the plugin’s settings page. So I can’t add my route to the whitelist.
I have the feeling that my original approach is wrong. Is there any better way I could define my custom route to achieve what I need?
(I’ve done a number of searches to see if anyone else has had this problem and solved it, but without any luck).
The topic ‘REST route with single quote in path expression’ is closed to new replies.