Make WordPress Core

Opened 6 years ago

Closed 6 years ago

#39925 closed defect (bug) (invalid)

Using unset to remove menu items, unset's other than those to be unset

Reported by: patriknilsson's profile patriknilsson Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.7.2
Component: Menus Keywords: close
Focuses: ui Cc:

Description

I have in my Avada child's theme put a functions.php to hide sub-menus other than that I currently is navigating. See function below.

This function is working as long as I don't use 'unset': Everywhere "Remove!!" is should be removed.

When 'unset' is active (not commented) other than those with attribute '$item->attr_title == "swp"' also get removed. It is always the same menu items that get removed.

http://www.fia.se/products/pump-systems/syringe-pumps/ hides main menu "Services" even though it shouldn't.

add_filter( 'wp_get_nav_menu_items','nav_items', 65535, 3 );

function nav_items( $items, $menu, $args ) 
{
    if( is_admin() )
        return $items;

        global $wp;
        $current_http_request_url = home_url(add_query_arg(array(),$wp->request));
        $current_http_request_path = parse_url($current_http_request_url, PHP_URL_PATH);
        $current_http_request_segments = explode('/', ltrim(rtrim($current_http_request_path, '/'),'/'));

        $bremove = 0;
        foreach( $items as $key => $item ) 
        {
                $currentprocessing_url=$item->url;
                $currentprocessing_path = parse_url($currentprocessing_url, PHP_URL_PATH);

                $currentprocessing_segments = explode('/', ltrim(rtrim($currentprocessing_path, '/'),'/'));
                
                if ( $item->attr_title == "swp" )
                {
                        $buse = 1;
                        
                        if ( count ( $current_http_request_segments ) >= count ( $currentprocessing_segments ) - 1 )
                        {
                                for ($i=0; $i < count ( $currentprocessing_segments ) - 1; $i++)
                                {
                                        if ( $current_http_request_segments [ $i ] != $currentprocessing_segments [ $i ] )
                                        {
                                                $buse = 0;
                                                break;
                                        }
                                }
                        }
                        else
                        {
                                $buse = 0;
                        }

                        if ( $buse == 0 )
                        {
                                $item->title = "Remove!!"; 
                                $bremove = 1;
                        }
                }
        }
        
        if ( $bremove == 1 )
        {
                foreach( $items as $key => $item ) 
                {
                        if ( $items [ $key ]->title == "Remove!!" )
                        {
                                unset ( $items [ $key ] );
                        }
                }
        }
        
    return $items;
}

Change History (4)

#1 @patriknilsson
6 years ago

  • Focuses ui javascript added

#2 @patriknilsson
6 years ago

  • Focuses javascript removed

#3 @patriknilsson
6 years ago

  • Component changed from General to Menus

#4 @welcher
6 years ago

  • Keywords close added
  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Status changed from new to closed

@patriknilsson thanks for the report and welcome to Core!

I was unable to reproduce the error using the code provided. I was able to remove menu items using unset without issue using the following:

add_filter( 'wp_get_nav_menu_items','nav_items', 10, 3 );
function nav_items( $items, $menu, $args ) {
    unset( $items[1] );
    return $items;
}

Unless this can reproduce using TwentySeventeen this may be better suited for an Avada or WordPress support forum.

Note: See TracTickets for help on using tickets.