3

I’m building an app which uses data from a WordPress backend. Most of the data is cached in JSON files on the server, but the app allows comments to be placed, so the API has to be called from within the app. I’m worried that when WordPress decides to change the URL from /wp-json/wp/v2/ to /wp-json/wp/v3/ I have to update the apps that use the URL. That is why I was hoping to rewrite this URL to something more generic like /api/.

Is this possible? My first attempt failed and simply shows the index.php from the theme directory:

RewriteRule ^api/(.*)$ /wp-json/wp/v2/$1 [NC,L]

1 Answer 1

3

You can rewrite your WordPress REST API url in your wordpress theme function.php

function changeRestPrefix() {
return "wpjsoncustom"; //become yourwebsite/wpjsoncustom/wp/v2/
}
add_filter( 'rest_url_prefix', 'changeRestPrefix');
Sign up to request clarification or add additional context in comments.

2 Comments

Yes, but this still leaves me with the /wp/v2/ part which seems to me can change with every update.
From what I read from here developer.wordpress.org/rest-api/extending-the-rest-api/… It is extremely important to add namespaces to your routes. The “core” endpoints use the wp/v2 namespace. So I think for now it's can't be rewritten

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.