WordPress keeps the identification of all records by its ID. Similarly, all menu has their unique ID stored in the database. To perform functions on a menu, its ID is required to fetch it. WordPress allows us to fetch menu ID by menu name.
Table of Contents
Understanding the Importance of Menu IDs
Menu IDs serve as unique identifiers for menus in WordPress. These IDs become essential when you need to perform specific operations, such as customization or manipulation, on a particular menu within your WordPress theme or plugin. By fetching the Menu ID based on its name, you gain the ability to precisely target and interact with a specific menu in your WordPress site.
How to fetch Menu ID by menu name in WordPress
In this article, we will see 4 different methods to fetch menu ID by menu name.
To create or access a menu, navigate to appearance > menu in the admin panel:
We have created a menu “My Menu” and used it as a reference:
If you wish to learn about creating dynamic or conditional menus in WordPress, please check here.
Method 1: Built-in function
In the first method, we will use WordPress’s built-in function get_term_by.
The get_term_by() is a WordPress built-in function. This function is used to get the term data from the database from its term field and its data.
Let’s see an example below:
<?php // use get_term_by function $term = get_term_by('name', 'My Menu', 'nav_menu'); // get menu id $menu_id = $term->term_id; ?>
The variable $menu_id
stores the menu ID.
Method 2: SQL method
In the second method, we will write an SQL query that will help to get the menu ID from the database.
First of all, we will write the menu name. In this example, we have used “My Menu”:
<?php $menu_name = 'My Menu'; ?>
Please note that you should write the exact menu name. “My Menu” is different from “My Menu” and “my menu”. It is case-sensitive and should be the same as defined in the WordPress admin panel.
Now we will be creating the table name along with the prefix. WordPress usually stores menu ID in its “terms” table. If you know your table prefix then you can use the prefix along with the “terms” and if you don’t know the prefix then not to worry at all you can use $wpdb->prefix
to get the table prefix.
<?php $table_name = $wpdb->prefix.'terms'; ?>
The $table_name
variable now has the table name along with the prefix and it will be used to create SQL query.
<?php $menu_id = $wpdb->get_results(" SELECT term_id FROM ".$table_name." WHERE name= '".$menu_name."' "); ?>
The $menu_id
will get the data received by the execution of the SQL query.
Now we will be fetching the menu ID as below:
<?php foreach($menu_id as $menu): echo $menu->term_id; endforeach; ?>
The $menu->term_id
will return the menu ID, now you can use that menu ID to fetch further details and objects for the particular menu.
The full PHP script:
<?php global $wpdb; // menu name $menu_name = 'My Menu'; // table name $table_name = $wpdb->prefix.'terms'; // SQL to get menu by menu name $menu_id = $wpdb->get_results(" SELECT term_id FROM ".$table_name." WHERE name= '".$menu_name."' "); // extract menu ID foreach($menu_id as $menu): echo $menu->term_id; endforeach; ?>
Method 3: Custom function
Here comes the third method. We will create a custom function that will take the menu name as a parameter and return its ID.
<?php // define function to get menu ID by name function wp_menu_id_by_name( $name ) { $menus = get_terms( 'nav_menu' ); foreach ( $menus as $menu ) { if( $name === $menu->name ) { return $menu->term_id; } } return false; } // use the function echo 'The Menu id is ' . wp_menu_id_by_name( 'My Menu' ); ?>
Method 4: Nav Menu Object method
In WordPress, you can use the wp_get_nav_menu_object()
function to fetch the menu ID by its name. This function takes the menu name as a parameter and returns an object containing information about the menu, including its ID.
$menu_name = 'My Menu'; // Replace with the name of your menu $menu_object = wp_get_nav_menu_object($menu_name); if ($menu_object) { $menu_id = $menu_object->term_id; echo "Menu ID for '$menu_name' is $menu_id."; } else { echo "Menu '$menu_name' not found."; }
Make sure to replace ‘My Menu’ with the actual name of the menu you want to fetch. The $menu_id
variable will contain the ID of the menu if it is found, and the information about the menu is stored in the $menu_object
.
You can use this menu ID for further operations or customizations related to the specific menu in your WordPress theme or plugin.
Conclusion
Understanding how to fetch Menu ID by menu name in WordPress provides a valuable tool for developers and website administrators. Whether you’re customizing themes, developing plugins, or implementing dynamic features, having the ability to target specific menus enhances the precision and effectiveness of your WordPress projects.