Insert custom menu items within main nav

I want to insert additional li items into the main nav so that I can create something similar to the split menu that uses.

I attempted to create a filter for wp_nav_menu_objects that checked if the menu item was a dropdown and inserted another menu item. It didn’t work as expected and instead added several empty menu items.

function add_split_to_nav($items, $args) {
foreach ($items as $item) {
foreach($item->classes as $key=>$class) {
if ($class == “menu-item-has-children”) {
$link_args = (object) array (
‘title’ => ‘TEST’,
‘menu_item_parent’ => 0
$link = new WP_Post($link_args);
array_splice($items, $key, 0, $link);
return $items;
add_filter(‘wp_nav_menu_objects’, ‘add_split_to_nav’, 10, 2);

Any advice?

Edit: Fixed link

Have you tried a lower number priority for the filter? 10 is the default. Lower than 10 would run earlier and before Roots tidies up the classes.

I’ve tried that and I still get the same result. A bunch of li tags with empty links.

The PHP error I get is: Creating default object from empty value in /srv/www/wp/wp-content/themes/roots-master/lib/nav.php on line 42

I think the link in your OP may have a typo, but if you’re talking about then why not just add your custom link in the Menus admin interface and apply some CSS classes right there? Maybe I don’t understand what you’re trying to accomplish…

Does your code work in one of the default themes or did you copy it from a demo on a blog? If not, then this isn’t really a Roots and would be better served on WordPress answers, Stack Overflow or the blog (if applicable).

Generally speaking, accessing the WordPress classes directly instead of using helper functions is a bad idea.

Wow. Thank you!

I feel like an idiot. I’ve spend hours trying to figure out a PHP hack. Adding the extra menu item from the admin interface worked exactly how I wanted it to.

Great! I’ve definitely been in a similar position. Sometimes when you’re so focused on a project you just need a fresh set of eyes to de-clutter what you’re chasing. Glad you got it sorted :wink: