avatar Deluxe Blog Tips About Projects

How to get all registered menus in WordPress

It's been a long time since last time I wrote about technical thing on this blog! But now I come back with a small tip of getting all registered menus in WordPress. Just a note: get all registered menus, not menu locations (for menu locations, WordPress already has a built in function - get_nav_menu_locations()). This is quite useful in some cases when you want to assign a custom menu for a page (like for onepage template - similar to what I did for our music and band WordPress theme).

WordPress get registered menus

The code snippet is very simple as the following:

$menus = get_terms( 'nav_menu' );
$menus = array_combine( wp_list_pluck( $menus, 'term_id' ), wp_list_pluck( $menus, 'name' ) );

print_r( $menus );

// You'll get:
// array( 'menu_id' => 'Menu Name' );

There are one interesting in the code above: each menu is a term of a taxonomy nav_menu. So it will have a full list of attributes that a normal term has, like: term_id, slug, name, etc. In this code, I only get term_id and name and combine them in an associated array, and that's enough for use.

🔥 HOT: Interested in boosting your WordPress SEO? My Slim SEO plugin is a super lighweight and automated plugin that handles most the hard work for you: meta tags, sitemap, redirection, schema & link building.

👉 Have a look and you will love it: wpslimseo.com