Menu’s in WordPress are quite customizable, items can be added and removed. Imagine you have a menu that must always include certain options, but you want to prevent them from being accidentally removed by the user. This can be done by using the wp_nav_menu_items filter. All the code for this goes into your theme’s functions.php.

First, make sure your menu is registered and included in the page:

register_nav_menus(array(
    'main-menu' => __('Main Navigation')
));

wp_nav_menu(array(
    'theme_location' => 'main-menu',
    'menu_id' => 'main-menu'
));

Next we’re going to add a filter:

add_filter('wp_nav_menu_items', 'custom_nav_menu_items', 10, 2);

The filter is executed for each menu on the page. The first argument is the name of the filter, the second the name of our function that will add the menu item. The third is the priority, ten by default, and the final one is the number of arguments our custom function expects.

The custom_nav_menu_items function is implemented like this:

function custom_nav_menu_items($items, $args) {
    if ($args->theme_location == 'main-menu') {
        $home = '<li><a href="' . home_url() . '">Home</a></li>';
        return $home . $items;
    }
    return $items;
}

We’re expecting two arguments: $items is the HTML for the other menu items, and $args contains information about the menu. $args->theme_location is used to make sure the item is added to a specific menu on the page. Note how $home is prepended to $items, if it were appended it would show as the last option in the menu.

That’s it! Now refresh the page and you’ll see the ‘Home’ option appearing.