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!

4 thoughts on “Running WP-API 1.2 & 2.0 beta side by side”

  1. Great post!
    I’m experiencing the same behavior but v2 overrides the v1 endpoints anyway even removing those filters. Is there anything else you’ve done in order to have both plugins working?

    1. Plugin load order could be the issue of that. The removal of these filters are executed ones all plugins have been loaded by initiating this calls on the plugins_loaded hook.

  2. I have used Zapier for quite a while. The Rest API is part of pulling data to Zapier through a webhook.

    All of a sudden we get the same error:

    No route was found matching the URL and request method

    Trying to fix. Looking for this compatability-v1.php file but not in directory for Rest API v2 beta 7.

    What to do??

Leave a Reply

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