Running WP-API 1.2 & 2.0 beta side by side

While working on a pet project where I’m using the WordPress JSON REST API (WP-API) plugin I stumbled upon an unexpected behaviour.

On the introduction of the first beta version for release 2.0 in the announcement there was a mention about running both version side by side. So… I gave it a try and installed both versions in separate directories in my WordPress application. Version 1.2.2 under json-rest-api and version 2.0 beta 1.1 under wp-rest-api. I could enable them both perfectly fine.

So far so good… As advised I set up a custom base for version 2.0 using the `rest_url_prefix` filter. I could even create a new custom endpoint using version 2.0 quite easily. I thought all was well, but then all endpoints version 1.2 turned out to be not working anymore. Both my custom defined ones as the internal ones.

All were returning the error:

[{"code":"json_invalid_handler","message":"The handler for the route is invalid"}]

After digging through the code of version 2.0 I finally found a `compatibility-v1.php` file which gave me the answer why this was occurring.

I don’t know the reasons for the existence of this file, but by removing the defined filters that are added there, I can run both version side by side perfectly!

remove_filter('json_endpoints', 'json_v1_compatible_routes', 1000);
remove_filter('json_dispatch_request', 'json_v1_compatible_dispatch', 10);

Version 1.2 endpoints are still handled by 1.2 code and new endpoints via 2.0 beta after this change!

Tagged as: , ,

4 Comments