When researching and reading about the WP REST API utilization I’ve seen many topics mentioned about how another version should be rolled out but nothing in the documentation regarding a return for a deprecated version. Planning the life span of an API and how I should develop for each version I was unsure how I should fire a return indicating a newer version is pushed.

For example:

add_action( 'rest_api_init', function () {
  register_rest_route( 'cpt/v1', '/all_posts/', array(
    'methods' => 'GET',
    'callback' => 'some_cpt',
  ) );
} );

if I were to change the route to:

add_action( 'rest_api_init', function () {
  register_rest_route( 'cpt/v2', '/all_posts/', array(
    'methods' => 'GET',
    'callback' => 'some_cpt',
  ) );
} );

what should I properly pass through v1 to indicate there is a new version? Was thinking of using WP_Error as it is shown but I’m unsure the code return and where to implement the WP_Error:

return new WP_Error( 'new_version_needed', 'Deprecated version', array( 'status' => 303 ) );

or:

return new WP_Error( 'new_version_needed', 'Deprecated version', array( 'status' => 410 ) );

0

Tags:

Leave a Reply

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