wp_nav_menu filter, removing a specific link

I’m trying to remove a specific link from my wordpress menu:

<li class="ubermenu-item ubermenu-item-type-post_type ubermenu-item-object-page ubermenu-item-1 ubermenu-item-level-0 ubermenu-column ubermenu-column-auto" ><a class="ubermenu-target ubermenu-item-layout-default ubermenu-item-layout-text_only" href="/first/" tabindex="0"><span class="ubermenu-target-title ubermenu-target-text">First</span></a></li>

<li class="ubermenu-item ubermenu-item-type-post_type ubermenu-item-object-page ubermenu-item-2 ubermenu-item-level-0 ubermenu-column ubermenu-column-auto" ><a class="ubermenu-target ubermenu-item-layout-default ubermenu-item-layout-text_only" href="/second/" tabindex="0"><span class="ubermenu-target-title ubermenu-target-text">Second</span></a></li>

<li class="ubermenu-item ubermenu-item-type-post_type ubermenu-item-object-page ubermenu-item-3 ubermenu-item-level-0 ubermenu-column ubermenu-column-auto" ><a class="ubermenu-target ubermenu-item-layout-default ubermenu-item-layout-text_only" href="/third/" tabindex="0"><span class="ubermenu-target-title ubermenu-target-text">Third</span></a></li>


function remove_link( $menu ) {
    return str_replace( '<a href="', '<a>', $menu );
}
add_filter( 'wp_nav_menu', 'remove_link' );

The link that I’m trying to remove has the .ubermenu-item-3 attached to the li element. How may I do this?

Related posts

3 comments

  1. You should use the wp_nav_menu_objects filter instead. There’s no need to use potentially messy string replacement or DOM parsing because you can just filter the array of menu items before their markup is generated.

    Here’s some skeleton code from http://hookr.io/filters/wp_nav_menu_objects/

    // define the wp_nav_menu_objects callback 
    function filter_wp_nav_menu_objects( $sorted_menu_items, $args ) { 
        // make filter magic happen here... 
        return $sorted_menu_items; 
    }; 
    
    // add the filter 
    add_filter( 'wp_nav_menu_objects', 'filter_wp_nav_menu_objects', 10, 2 ); 
    

    Just unset the appropriate index in the array.

    Or, simply use the UberMenu Conditionals Extension, which is designed for exactly that purpose. http://wpmegamenu.com/conditionals

  2. Both versions below. read the comments! This depends on domdocument, most php installations will have it, but some custom set ups don’t.

    function remove_link( $html ) {
        $dom->loadHTML($html);
        $links= $dom->getElementsByTagName('li');
    
        foreach($links as $data){
    
            if(strpos($data->getAttribute('class'), 'ubermenu-item-3') !== false){
    
                //remove the entire li - comment out if just removing the href
                $data->parentNode->removeChild($data);
    
                //alternatively change the href as per your code (comment out if using remove the entire li) 
                //foreach($data->getElementsByTagName('a') as $as){
                //  $as->setAttribute('href', '');
                //}     
            }
    
        }
    
        $html= $dom->saveHTML();
    
        return $html;
    
    }
    
    add_filter( 'wp_nav_menu', 'remove_link' );
    
  3. Try this one if you’re excluding a page:

    
    function exclude_menu_for_visitors($exclude_array){
        $page_id = 1; //id of page you want to exclude
        $exclude_array[]= $page_id;
        return $exclude_array;
    }
    
    add_filter('wp_list_pages_excludes', 'exclude_menu_for_visitors');
    

Comments are closed.