0

I have those lines in my functions.php to modify some menus in the front office of my site :

add_filter( 'wp_setup_nav_menu_item','decorate_mobile_menu' );
function decorate_mobile_menu($item) {
    if ( strpos($item->title, 'SHIPPING') !== false) {  
        $split = explode(" IN ", $item->title);     
        $item->title = '<span class="shipping-menu-item">' . $split[0] . ' IN </span>';
        $item->title .= '<br/>';
        $item->title .= '<span class="shipping-menu-item">' . $split[1] . '</span>';
    }   
    return $item;
}

but everytime I "save" the corresponding menu in the back office, it runs this function again and the html gets crazy in the menu title :

<span class="shipping-menu-item"><span class="shipping-menu-item"><span class="shipping-menu-item"><span class="shipping-menu-item"><span class="shipping-menu-item">FREE SHIPPING IN </span><br/><span class="shipping-menu-item"></span><br/><span class="shipping-menu-item"></span><br/><span class="shipping-menu-item"></span><br/><span class="shipping-menu-item"></span><br/><span class="shipping-menu-item"><br/>EUROPE FROM 150€</span></span></span></span></span>

Can you help me clean this function to only act on front office ?

Thanks

1

Just wrap it all in an ! is_admin() check:

add_filter( 'wp_setup_nav_menu_item','decorate_mobile_menu' );
function decorate_mobile_menu($item) {
    if ( ! is_admin() ) {
        if ( strpos($item->title, 'SHIPPING') !== false) {  
            $split = explode(" IN ", $item->title);     
            $item->title = '<span class="shipping-menu-item">' . $split[0] . ' IN </span>';
            $item->title .= '<br/>';
            $item->title .= '<span class="shipping-menu-item">' . $split[1] . '</span>';
        }
    }

    return $item;
}

is_admin() returns false if you're viewing the front-end and true if you're viewing the admin area.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.