Skip to content

Instantly share code, notes, and snippets.

@knolaust
Last active January 30, 2024 15:06
Show Gist options
  • Save knolaust/4cae2f4938808875904aa98c5100b186 to your computer and use it in GitHub Desktop.
Save knolaust/4cae2f4938808875904aa98c5100b186 to your computer and use it in GitHub Desktop.
Function for WordPress that creates custom endpoint to return menu using WP-Rest-API
<?php
/**
* Custom REST API Endpoint: Get Menu
* Gist Keywords: wordpress, rest, api
*
* @category WordPress
* @author Knol Aust
* @version 1.0.0
* @description Retrieves a WordPress menu using a custom REST API endpoint.
*/
function custom_rest_get_menu() {
# Change 'menu' to your own navigation slug.
return wp_get_nav_menu_items('menu');
}
add_action( 'rest_api_init', function () {
// Registers a custom route to retrieve the menu via the REST API
register_rest_route( 'myroutes', '/menu', array(
'methods' => 'GET',
'callback' => 'custom_rest_get_menu',
) );
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment