Getting a menu name in WordPress

Seems like it would be a simple thing to achieve, but surprisingly… no.  I was expecting to find a built-in function like wp_get_nav_menu_name but after a fair amount of codex hunting and some (admittedly brief) Googling I came up short.

Time to write my own function

Instead of reinventing the wheel I had a wee peek at what WordPress was doing in it menu functions (nav-menu.php) and it became clear that my idea of a menu slug and WordPress’s was quite different.  I was expecting to pass the registered menu location slug and have a function return the assigned menu name.  In WordPress the menu slug is the URL friendly version of the menu name.

I can’t magically determine what somebody may call any given menu, but I can get the ID of the menu assigned to a menu location registered in a template.  I came up with two functions: ag_get_theme_menu and ag_get_theme_menu_name.

ag_get_theme_menu( $theme_location )

This function gets a list of registered theme menu locations and checks to see if your location slug exists.  If it does it returns a menu object for the menu assigned to that location.

function ag_get_theme_menu( $theme_location ) {
	if( ! $theme_location ) return false;
 
	$theme_locations = get_nav_menu_locations();
	if( ! isset( $theme_locations[$theme_location] ) ) return false;
 
	$menu_obj = get_term( $theme_locations[$theme_location], 'nav_menu' );
	if( ! $menu_obj ) $menu_obj = false;
 
	return $menu_obj;
}

The key in the locations array ($theme_locations) is the slug, and the value is the menu ID.  If you pass that ID to the built-in function get_term it will return a menu object.  It’s that menu object that my next function will build on.

ag_get_theme_menu_name( $theme_location )

Using the ag_get_theme_menu function we can get a menu object for the menu assigned to the theme location parameter ($theme_location).  This object stored the menu name in a property called name, and now it is simply a case of returning that text.

function ag_get_theme_menu_name( $theme_location ) {
	if( ! $theme_location ) return false;
 
	$menu_obj = ag_get_theme_menu( $theme_location );
	if( ! $menu_obj ) return false;
 
	if( ! isset( $menu_obj->name ) ) return false;
 
	return $menu_obj->name;
}

It may seem a little overkill creating two functions to solve a problem as small as this but I thought it would be handy to have a function that returned a menu object based on a theme location, not just a menu name.

If a menu name is all you’re after then stick this code in your theme’s functions.php file instead:

function gm_get_theme_menu_name( $theme_location ) {
	if( ! $theme_location ) return false;
 
	$theme_locations = get_nav_menu_locations();
	if( ! isset( $theme_locations[$theme_location] ) ) return false;
 
	$menu_obj = get_term( $theme_locations[$theme_location], 'nav_menu' );
	if( ! $menu_obj ) $menu_obj = false;
	if( ! isset( $menu_obj->name ) ) return false;
 
	return $menu_obj->name;
}

I hope that helps somebody :)

About Andrew Gail

Hi, I'm Andrew Gail. I'm a web developer and amatuer photographer from Scotland. I am also the owner of gailmedia.co.uk doing freelance web design when my full time job and family allows.
This entry was posted in Wordpress and tagged , , , , . Bookmark the permalink.

6 Responses to "Getting a menu name in WordPress"

Leave a reply