10

The WP REST API exposes a lot of information so I filter endpoints that aren't needed to expose.

I can't filter everything: The location of needed media files are exposed for example.

As an extra protection I'd like to mystify the default uri.

I would like to change for example: http://example.com/wp-json/wp/v2/ to http://example.com/mistified/wp/v2/

Is this rather easy possible?

3
  • For protection? The URL will still be visible in your HEAD which means anyone can see it in your source. And the namespace should automatically show all your available endpoints. See v2.wp-api.org/guide/discovery
    – jgraup
    Commented Nov 30, 2016 at 12:17
  • Non-api requests will be redirected to an external url where the frontend app lives. So I don't think it will show in the HEAD there. I filtered out all default endpoints including /wp/v2. Only the custom post types are exposed now. And of course the `wp-json' prefix.
    – violacase
    Commented Nov 30, 2016 at 14:14
  • Did you ever find a solution to this not working @violacase ? Commented Jan 19, 2017 at 10:26

2 Answers 2

12

Please note that for current versions of WordPress, using the json_url_prefix filter no longer works.

On WordPress 4.7 (and using the REST API from the core instead of a plugin), this is what I needed to change the API prefix.

add_filter( 'rest_url_prefix', 'my_theme_api_slug'); 
function my_theme_api_slug( $slug ) { return 'api'; }

If this doesn't work straight away, you'll need to flush the rewrite rules. You can run this piece of code once to do so (don't leave it in your code so it runs everytime):

flush_rewrite_rules(true);
1
  • Additionally, you may create a procedure by using php to curl to a certain old endpoint, check to see if that is still working, then that's the time you flush_rewrite_rules(true); but if the curl response says that the api is now working, you no longer need to flush Commented Mar 19, 2020 at 23:33
0

You could use the json_url_prefix hook to remove 'wp-json' across all API routes. The below example will do the example in your question:

add_filter( 'json_url_prefix', 'my_theme_api_slug'); 
function my_theme_api_slug( $slug ) { 
    return 'mistified';
}
1
  • I can't get this to work. Perhaps a reflushing problem?
    – violacase
    Commented Dec 1, 2016 at 14:00

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.