WP: Register A New Menu Location

To register a menu location in our newly developed theme, we can include the following line in our functions.php file:

register_nav_menu('headerMenuLocation', 'Header Menu Location');

This again needs to be called on the action hook after_setup_theme (similar to the previous post where we added theme support for title).

Example:

function ob_theme_features() {
    register_nav_menu('headerMenuLocation', 'Header Menu Location');
    add_theme_support('title-tag');
}
add_action('after_setup_theme', 'ob_theme_features' );

Once this code is saved, we can see a “Menu” option under WP-Admin dashboard > Appearance page.

Trying to create a menu there will give us a prompt to select the menu location. We’ll be able to see a menu location that we just created (called “Header Menu Location” based on the code above).

Select that location and add some menu items like home page, blog page, etc.

Next we want to actually display this menu on the front-end. To do so, we’ll need to place the code below in one of the files responsible for outputting content on the front-end (e.g. index.php, single.php, etc).

We can put this in index.php to test it.

wp_nav_menu(array(
    'theme_location' => 'headerMenuLocation'
));

The result? You will start to see the menu you created by visiting the front page of your WordPress site.

You can read more about this by going to the official developer reference guide at WordPress.org 🙂