Mustafa Uysal

I'm traveling light, it's au revoir…

WP-REST-API: non-namespaced route

We are using old WP-REST-API aka (JSON-REST-API)  for years and time to move on WordPress’ core REST API.

But there is a problem, new API requires namespace that we haven’t used it before. There is no problem with splitting long endpoints. I mean ,if you used ‘/foo/bar/’ as endpoint before, you can use ‘foo’ as namespace and ‘/bar’ as endpoint. But if you were using ‘/foo’ , how to migrate new REST API?

The answer is non-namespaced route.


<?php
add_action( 'rest_api_init', function ( $server ) {
$server->register_route( 'foo', '/foo', array(
'methods' => 'GET',
'callback' => function () {
return 'baz';
},
) );
} );

will register ‘/foo’ endpoint.

As a reminder, please ensure before registering non-namespaced routes. They can be useful for backward compatibility only.

  1. kadir yiğit Avatar

    Hocam, Türkçe makale’de yazsanız keşke..

    1. Mustafa Uysal Avatar

      blog.uysalmustafa.com ve wpnotlari.com ‘da yaziyorum aslinda, tabii makale degil blog yazisi 🙂

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.