How to create an API to fetch menu items from wordpress

Creating an API to fetch menu items from WordPress involves leveraging the WordPress REST API to expose menu data as JSON endpoints. By defining custom routes and callback functions, developers can retrieve information about menus, including their names and associated items. This allows for dynamic integration of WordPress menus into external applications, providing a flexible and efficient way to access and display navigation data.

Add the following code in functions.php to retrieve menu items based on location of menu.

It involves two parts:

  • Register the Custom REST API Route
add_action('rest_api_init', function () {
    register_rest_route('custom', '/primary-menu/', array(
        'methods' => 'GET',
        'callback' => 'get_primary_menu_items',
    ));
});

add_action: Hooks a function on to a specific action.

rest_api_init: The action triggered when the REST API is initialized.

register_rest_route: Registers a custom REST API route.

‘custom’: The namespace for the custom route.

‘/primary-menu/’: The endpoint route that will be appended to the namespace.

‘methods’ => ‘GET’: Specifies that this endpoint supports HTTP GET requests.

‘callback’ => ‘get_primary_menu_items’: Specifies the callback function that will handle the API request.

  • Callback Function to Retrieve Primary Menu Items:
function get_primary_menu_items() {
    // Get the location of the primary menu
    $primary_menu_location = 'primary';


    // Get the menu ID associated with the primary menu location
    $menu_location_id = get_nav_menu_locations()[$primary_menu_location];


    // Check if a menu is assigned to the primary location
    if (!$menu_location_id) {
        return new WP_Error('no_primary_menu', 'Primary menu not found', array('status' => 404));
    }


    // Get the menu object
    $menu_object = wp_get_nav_menu_object($menu_location_id);


    // Get the menu name
    $menu_name = $menu_object->name;


    // Get the menu items
    $menu_items = wp_get_nav_menu_items($menu_location_id);


    // Prepare an array with menu information
    $menu_data = array(
        'menuName' => $menu_name,
        'menuItems' => array(),
    );


    // Iterate through menu items and retrieve their names
    foreach ($menu_items as $menu_item) {
        $menu_data['menuItems'][] = $menu_item->title;
    }


    return $menu_data;
}

get_primary_menu_items: The callback function that is executed when the custom API endpoint is accessed.

Retrieves the location and associated ID of the primary menu.

Checks if a menu is assigned to the primary location; if not, returns a WP_Error with a 404 status.

Gets the menu object and extracts the menu name.

Retrieves the menu items and constructs an array ($menu_data) containing the menu name and an array of menu item titles.

Returns the $menu_data array as the API response.

Note that the menu location can be changed according to your specification.

Leave a comment

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