WordPress REST API Namespace Versioning: Run v1 and v2 Side-by-Side with Breaking Changes

Versioning a plugin’s REST API: v1 and v2 side by side

Once an app, a theme or another site uses a plugin’s REST routes, changing what they return breaks them. Core solves it with versions in the namespace (wp/v2), and a plugin can do the same: keep ha/v1 exactly as it is, add ha/v2 with the new format, and share the business logic between them.

Problem: A plugin’s /ha/v1/products route returns a flat list, and a new mobile app needs pagination data and different field names, while the old app must keep working.

Solution: Register the same path in two namespaces, ha/v1 and ha/v2, with different callbacks that format the result of one shared query function. Mark v1 as deprecated with a header, and remove it only after its clients have moved.

Shared logic, two formats

<?php
function ha_query_products( $page, $per_page ) {
    $query = new WP_Query( array(
        'post_type'      => 'product',
        'post_status'    => 'publish',
        'paged'          => $page,
        'posts_per_page' => $per_page,
    ) );

    return array( 'posts' => $query->posts, 'total' => (int) $query->found_posts, 'pages' => (int) $query->max_num_pages );
}

add_action( 'rest_api_init', function () {
    $args = array(
        'page'     => array( 'type' => 'integer', 'default' => 1, 'minimum' => 1 ),
        'per_page' => array( 'type' => 'integer', 'default' => 10, 'minimum' => 1, 'maximum' => 100 ),
    );

    // v1: unchanged, a flat array of products
    register_rest_route( 'ha/v1', '/products', array(
        'methods'             => WP_REST_Server::READABLE,
        'permission_callback' => '__return_true',
        'args'                => $args,
        'callback'            => function ( WP_REST_Request $request ) {
            $result = ha_query_products( $request['page'], $request['per_page'] );

            $response = rest_ensure_response( array_map( function ( $post ) {
                return array( 'id' => $post->ID, 'name' => $post->post_title );
            }, $result['posts'] ) );

            $response->header( 'Deprecation', 'true' );
            $response->header( 'Link', '<' . rest_url( 'ha/v2/products' ) . '>; rel="successor-version"' );
            return $response;
        },
    ) );

    // v2: items plus pagination data, renamed fields
    register_rest_route( 'ha/v2', '/products', array(
        'methods'             => WP_REST_Server::READABLE,
        'permission_callback' => '__return_true',
        'args'                => $args,
        'callback'            => function ( WP_REST_Request $request ) {
            $result = ha_query_products( $request['page'], $request['per_page'] );

            return rest_ensure_response( array(
                'items'      => array_map( function ( $post ) {
                    return array( 'id' => $post->ID, 'title' => get_the_title( $post ), 'url' => get_permalink( $post ) );
                }, $result['posts'] ),
                'pagination' => array( 'page' => (int) $request['page'], 'pages' => $result['pages'], 'total' => $result['total'] ),
            ) );
        },
    ) );
} );

Both versions run the same query, so a bug fixed in ha_query_products() is fixed for both. Only the shape of the answer differs.

What needs a new version

A new version is needed for breaking changes: removed or renamed fields, a different structure, changed meaning of a parameter, stricter permissions. Adding a field or an optional parameter is not breaking; it can go into the existing version, because well-behaved clients ignore fields they do not know.

Retiring v1

Keep v1 until you know its clients have moved. Log its use to find out:

<?php
add_filter( 'rest_pre_dispatch', function ( $result, $server, $request ) {
    if ( 0 === strpos( $request->get_route(), '/ha/v1/' ) ) {
        error_log( 'ha/v1 used: ' . $request->get_route() . ' by ' . ( $request->get_header( 'user_agent' ) ?: 'unknown' ) );
    }
    return $result;
}, 10, 3 );

When nobody calls it any more, remove the route, ideally in a major version of the plugin with a note in the changelog. Routes that are gone return 404 rest_no_route, which a client can recognise.

NOTE: Version the namespace, not each route. ha/v2 can reuse unchanged routes of v1 by registering them again with the same callback; mixing /products-v2 and /orders in one namespace makes it unclear which combination a client can rely on.