vespino
vespino

Reputation: 1940

Custom url WordPress REST API

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]

Upvotes: 3

Views: 3127

Answers (1)

rheeantz
rheeantz

Reputation: 1020

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');

Upvotes: 3

Related Questions