Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialCharlie Guan
7,284 PointsNo Need to Use "add_theme_support('menus') According to WordPress Reference
In the video, Zac said we need to enable menu option in admin dashboard by adding this line in our functions.php
:
<?php add_theme_support('menus'); ?>
However, we probably don't need that line, according to the function reference for register_nav_menus($locations)
:
"This function automatically registers custom menu support for the theme, therefore you do not need to call add_theme_support( 'menus' );"
And here is the weird thing:
I commented out the add_theme_support('menus')
line, and the menu option seems to still work. However I then commented out everything about menus in functions.php, but the menu option in admin dashboard still seems to work. Here is the code I commented out:
<?php
add_theme_support( 'menus' );
function register_theme_menus () {
register_nav_menus( [
'primary-menu' => _( 'Primary Menu' )
] );
}
add_action( 'init', 'register_theme_menus' );
?>
That's a bit weird. Can anyone explain what happened? My best guess is that when we tell WordPress to enable theme support for menus, it remembers it somewhere in database, so deleting the code later on won't remove the support, since we didn't touch the database. If I really want to remove any feature, I can do
<?php remove_theme_support( 'menu' ); ?>
1 Answer
Craig Watson
27,930 PointsHi Charlie,
I have dug around on the codex and got here, under the parameters you will see 'menus' (internal registrations not used directly by themes).
I googled that and came across the this, which in your last line you mentioned.
The only thing that seems to concern me is that in no place can I find text to suggest we dont have to register menus with theme support :S
Most thing seem to be leading to removing theme support through a child theme...
For my bit of digging I will still be registering theme support for menus in all custom themes. If you wanted to on way to have a good look to see if it is needed would be to open the code of the 2016 theme from WordPress in your text editor and do a search for add_theme_support and see what the guys at automatic feel they need to do because they will know :)
Craig
Faddah Wolf
12,811 PointsFaddah Wolf
12,811 Pointsperhaps this is in issue that can be clarified by posting on the WordPress developer support forums?
this would be helpful to them also, as a resolution might help WordPress.org to clarify their on-line docs.
best,
— faddah portland, oregon, u.s.a.