Skip to Content

menuSearchReplacement()

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

JavaScript February 22, 2017

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.

Source File

Located in /assets/js/nebula.js on line 2030.

JavaScript
nebula.menuSearchReplacement = function(){
    if ( jQuery('.nebula-search').length ){
        var randomMenuSearchID = Math.floor((Math.random()*100)+1);
        jQuery('.menu .nebula-search').html('<form class="wp-menu-nebula-search nebula-search search footer-search" method="get" action="' + nebula.site.home_url + '/"><div class="input-group"><i class="fas fa-search"></i><label class="sr-only" 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.
}