Search field displayed twice when added to nav menu

Hi everybody,

I tried to append a search input to the nav menu with the following code in function.php:

beans_add_smart_action( 'egn_r_nav_menu_search', 'egn_r_nav_search' );
function egn_r_nav_search() {
        beans_open_markup_e( 'egn-r-nav-search-container', 'li', array( 'id' => 'egn-r-nav-search-container', 'class' => 'menu-item' ) );
        beans_open_markup_e( 'egn-r-nav-search-form', 'form', array( 'class' => 'uk-form uk-form-icon uk-form-icon-flip', 'method' => 'get', 'action' => esc_url( home_url( '/' ) ), 'role' => 'search' ) );
        beans_selfclose_markup_e( 'egn-r-nav-search-input', 'input', array( 'placeholder' => __( 'Search' ), 'value' => esc_attr( get_search_query() ), 'name' => 's', 'type' => 'search' ) );
        beans_open_markup_e( 'egn-r-nav-search-icon', 'i', array( 'class' => 'uk-icon-search' ) );
        beans_close_markup_e( 'egn-r-nav-search-icon', 'i' );
        beans_close_markup_e( 'egn-r-nav-search-form', 'form' );
        beans_close_markup_e( 'egn-r-nav-search-container', 'li' );
beans_modify_action_hook( 'egn_r_nav_search', 'beans_menu[_navbar]_append_markup' );

It works on my local computer, but when I put it on prod, the search input is displayed twice.

Do you have an idea how to resolve it?

Hello Etienne, To add a search bar in your menu, maybe you could do something like that, a little more straightforward :

    beans_add_smart_action( 'beans_menu[_navbar][_primary]_append_markup', 'myprefix_primary_menu_search' );

    function myprefix_primary_menu_search() {
        <li class="tm-search uk-large-visible uk-margin-large-left">
            <?php get_search_form(); ?>

You can, of course, change the li class as you want. Do some tests. Hope it helps, Have a nice day, Mathieu

Thank you Mathieu,

Your code works perfectly! My problem was that on my local website I didn't select the Primary menu for my nav bar, but on prod it was selected and so with my code I was adding the input twice.

The secret is the [ _ primary ] you added to the call.

Write a reply

Login or register to write a reply, it's free!