register_rest_route regex option for base64 or alternate

I have been trying to work with register_rest_route but running into regex issues. It seems that there is some filtering on the regex before its executed.

The function I am trying to create is grabbing a post by the permalink. Basically I have a client that is structuring its url’s similar to wordpress so it wants to pass the permalink to wordpress so I can get the post data

register_rest_route( $this->namespace, ‘/post_by_permalink/(?P<path>[w-]+)’, array(
‘methods’ => WP_REST_Server::READABLE,
‘callback’ => array( $this, ‘postByPermalink’ ),
‘permission_callback’ => array( $this, ‘permissions’ ),
‘show_in_rest’ => true

This is the function I am using and it works in some cases. However that is only because the ones that work do not produce any special characters.

Right now my plan was in the client to convert the permalink to base64 to make it easier to pass through the url, though I am open to suggestions if they work (I only thought of base64 since urlencode was even more of a nightmare).

Basically the only regex that work in wordpress seems to be (?P[w-]+) and (?P[d]+). Anything else, does not work, even if it was successful testing it in something like

For instance, according to the tester (?P[S]+) should work, but all I get from wordpress is rest_no_route

Is there anyway to get wordpress to handle regex normally? Or at least tell it to allow an expression that could catch base64? I have seen some options online, but none work, likely due to changes in wp rest over time (all say “just use regex”, yet it seems to filter more complex regex)

Read more here:: register_rest_route regex option for base64 or alternate

Leave a Reply

Your email address will not be published. Required fields are marked *