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?
Advertisement
Answer
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' );