Add empty menu link in WordPress

January 5, 2021Snippets, WordPress

To add an empty menu link, you need to add a menu item with # as URL ( Custom Links ) to add an empty menu link.

Then you can simply open functions.php file in your parent or child theme folder. Then add the below code at the end of the file.

// add javascript:void(0) to menu items with # url
add_filter('walker_nav_menu_start_el', 'mswa_replace_hash', 999);
function mswa_replace_hash($menu_item) {
    if (strpos($menu_item, 'href="#"') !== false) {
        $menu_item = str_replace('href="#"', 'href="javascript:void(0);"', $menu_item);
    }
    return $menu_item;
}

You can find a detailed explanation of this code in our blog post about adding menu without link

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Subscribe Now to get 70% Off launch coupon!

MS Web Arts will be launching on 1st May 2021. You will get up to 70% off and the latest product updates when we launch. This is subscribers only. Don’t miss out!