I’m looking for a way to create a new item that can be added to a menu.

Here are the details of my problem:
I use WPML. WPML have that nice feature that you can add the switcher to a menu, automatically. It adds it at the end of the menu, no control on that.

Thing is, I want my language switcher to be element 4 out of 6. That feature to automatically add the element at the end doesn’t fit my needs.

So I want to create a new element that can be used in apparence->menu to put my language switcher exactly at the spot I want it.

Is there any way to do that?

TLDR: I want to be able to push custom HTML/PHP code in a menu element (Apparence->Menu). Any functions to do so?

1 Answer
1

Following code snippet replaces placeholder menu item from ‘your-menu’ menu.

    add_filter( 'wp_nav_menu_items', 'add_item_to_menu', 10, 2 );
    function add_item_to_menu( $items, $args ) {
        if ( $args->theme_location == 'your-menu' ) {
                $link_text = "My Replaced Link Text";
                $link_url = "my-replaced-link-url.com";

                $items = str_replace( 'lang_placeholder_text', $link_text, $items);
                $items = str_replace( 'lang_placeholder_url', $link_url, $items);
        }

        return $items;
    }

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *