Getting a menu name in WordPress

Categories 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 🙂

7 Comments

  • E. Serrano
    July 30, 2011

    You definitely saved my life. I was experiencing the same issue since I needed the name of the registered theme, and I had no luck with WordPress built in functions.

    I think there are a couple of minor typos on your second function. My guess is that you need to remove the first semicolon on $menu_obj->;name; and that you intended to call gm_get_theme_menu_name instead of gm_get_theme_menu.

    Anyway, your third function did exactly what I needed. get_term was the key to get the menu object I was looking for, as for some reason, I had no luck with the default wp_get_nav_menu_object as it kept returning empty objects.

    Thank you, Andrew!

  • Alberto
    June 13, 2012

    Great solution! Thanks!

  • Amelie
    October 5, 2012

    So would I call the second function like so:

    echo gm_get_theme_menu_name( $theme_location ) ?

    and can you give an example of a $theme_location ? I’m not sure what to put for the theme location..

  • Katja
    October 19, 2012

    Not working for me…

  • Carl
    November 23, 2012

    Thanks Andrew i had the same need but fortunately for me my googling brought me to your post and Saved me a bit of time.

    Much appreciated.

  • Getting a menu name in WordPress : Post Status
    February 5, 2013

    […] Getting a menu name in WordPress » I had the hardest time figuring out how to return the saved name for a given menu. After spending way too much time in Google, I stumbled across this simple function that does the trick. Submitted by erikford 1 min ago | Development | Source: http://www.andrewgail.com | Comment | Permalink […]

  • Karoline
    July 3, 2014

    It’s actually helped me. Thank you!!!!

Leave a Reply

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