Welcome To Snippets.Cloud - The Biggest Code Snippets Library For WordPress
Public REST API Access for WordPress Menus
Why it’s new and useful:
Starting with WordPress 6.8, menus are not publicly accessible via the REST API by default for privacy and security. However, many headless WordPress sites, mobile apps, or custom front-ends need to fetch menu data. This snippet enables public REST API access for your menus—no plugin required, just a few lines of code!
1. Add the Snippet to Your Theme’s functions.php or a Custom Plugin
// Make WordPress menus publicly accessible via the REST API (WordPress 6.8+)
add_filter('rest_menu_item_collection_params', function($params) {
$params['context']['default'] = 'view';
return $params;
});
add_filter('rest_endpoints', function($endpoints) {
if (isset($endpoints['/wp/v2/menus'])) {
foreach ($endpoints['/wp/v2/menus'] as &$endpoint) {
if (isset($endpoint['permission_callback'])) {
$endpoint['permission_callback'] = '__return_true';
}
}
}
return $endpoints;
});
PHP2. What Does This Do?
- Enables public access to your WordPress menus through the REST API endpoint
/wp/v2/menus. - Perfect for headless WordPress, mobile apps, or static site generators that need to fetch menu data without authentication.
- No plugin needed—just a lightweight snippet.
3. How to Use
- Add the snippet above to your theme’s
functions.phpor a custom functionality plugin. - Access your menus at:
https://your-site.com/wp-json/wp/v2/menus
