Wordpress - Get WP Navigation Menu from REST API V2

Since I don't like it myself when the top answer is "Install plugin X", here is how I solved it:

Menus are currently not available in the WP Rest. So what you need to do is register your own custom endpoint and then just call that route from your application that needs it.

So you would include something like this (in your functions.php, plugin, wherever):

function get_menu() {
    # Change 'menu' to your own navigation slug.
    return wp_get_nav_menu_items('menu');
}

add_action( 'rest_api_init', function () {
        register_rest_route( 'myroutes', '/menu', array(
        'methods' => 'GET',
        'callback' => 'get_menu',
    ) );
} );

For the example above you would access the data from:

http://your-domain.dev/wp-json/myroutes/menu

You can use the above method to create any routes you like to get any type of data that is not available in the WP Rest. Also good if you need to process some data before sending it to your application.


There is a navigation menu plugin extension for the REST API v2: https://wordpress.org/plugins/wp-api-menus/


@Liren answer works well. However few beginners may not able to adjust the route. Here is the code that works well with WordPress Rest API v2 with minimum modification.

Replace your menu name only in wp_get_nav_menu_items() function. If menu name and slug does not work (Return false), use Menu ID (visible in Dashboard while editing that Menu).

function get_my_menu() {
    // Replace your menu name, slug or ID carefully
    return wp_get_nav_menu_items('Main Navigation');
}

add_action( 'rest_api_init', function () {
    register_rest_route( 'wp/v2', 'menu', array(
        'methods' => 'GET',
        'callback' => 'get_my_menu',
    ) );
} );

Route URL:

https://website.com/wp-json/wp/v2/menu

More details covered in Tutorial: WordPress Rest API – Get Navigational Menu Items