Skip to Content
Menu

menuSearchReplacement()

Replace a search link in a WordPress menu with an actual search input field.

JavaScript February 7, 2021

Usage

JavaScript
.nebula-search

Parameters

This function does not accept any parameters. Is this incorrect?

Additional Notes

Here is where to add the class to the menu item. If this is not available, check the “Screen Options” dropdown at the very top of the screen to enable it.

Was this page helpful? Yes No


    A feedback message is required to submit this form.


    Please check that you have entered a valid email address.

    Enter your email address if you would like a response.

    Thank you for your feedback!

    Source File

    Located in /assets/js/modules/search.js on line 73.

    No Hooks

    This function does not have any filters or actions available. Request one?
    JavaScript
    nebula.menuSearchReplacement = async function(){
        if ( jQuery('.nebula-search').length ){
            jQuery('.menu .nebula-search').each(function(){
                let randomMenuSearchID = Math.floor((Math.random()*100)+1); //Why does it need this again? Add comment please.
                jQuery(this).html('<form class="wp-menu-nebula-search nebula-search search footer-search" method="get" action="' + nebula.site.home_url + '/"><div class="nebula-input-group"><i class="fas fa-search"></i><label class="visually-hidden" for="nebula-menu-search-' + randomMenuSearchID + '">Search</label><input type="search" id="nebula-menu-search-' + randomMenuSearchID + '" class="nebula-search input search" name="s" placeholder="Search" autocomplete="off" x-webkit-speech /></div></form>');
            });
    
            jQuery('.nebula-search input').on('focus', function(){
                jQuery(this).addClass('focus active');
            });
    
            jQuery('.nebula-search input').on('blur', function(){
                if ( jQuery(this).val() === '' || jQuery(this).val().trim().length === 0 ){
                    jQuery(this).removeClass('focus active focusError').attr('placeholder', jQuery(this).attr('placeholder'));
                } else {
                    jQuery(this).removeClass('active');
                }
            });
        }
    };
    

    Override

    To override or disable this JavaScript function, simply redeclare it with the exact same function name.

    JavaScript
    nebula.menuSearchReplacement = function(){
        //Write your own code here, leave it blank, or return false.
    }