This tutorial provides the steps to add a magnifying glass search icon at the right of Primary nav menu in Genesis, which when clicked, will show a search box covering the entire site header based on the code from Monochrome Pro.
Screenshots:
While the tutorial has been written for Genesis Sample 2.6.0, it should work with a few adjustments in any Genesis theme
Step 1
Replace the entire content of js/genesis-sample.js with:
/**
* Genesis Sample entry point.
*
* @package GenesisSample\JS
* @author StudioPress
* @license GPL-2.0+
*/
var genesisSample = ( function( $ ) {
'use strict';
/**
* Adjust site inner margin top to compensate for sticky header height.
*
* @since 2.6.0
*/
var moveContentBelowFixedHeader = function() {
var siteInnerMarginTop = 0;
if( $('.site-header').css('position') === 'fixed' ) {
siteInnerMarginTop = $('.site-header').outerHeight();
}
$('.site-inner').css('margin-top', siteInnerMarginTop);
},
/**
* Initialize Genesis Sample.
*
* Internal functions to execute on document load can be called here.
*
* @since 2.6.0
*/
init = function() {
// Run on first load.
moveContentBelowFixedHeader();
// Run after window resize.
$( window ).resize(function() {
moveContentBelowFixedHeader();
});
// Run after the Customizer updates.
// 1.5s delay is to allow logo area reflow.
if (typeof wp.customize != "undefined") {
wp.customize.bind( 'change', function ( setting ) {
setTimeout(function() {
moveContentBelowFixedHeader();
}, 1500);
});
}
// Make sure JS class is added.
$('body').addClass('js');
// set the variables.
var $header = $('.site-header'),
$hsToggle = $('.toggle-header-search'),
$hsWrap = $('#header-search-wrap'),
$hsInput = $hsWrap.find('input[type="search"]');
// Handler for click a show/hide button.
$hsToggle.on('click', function (event) {
event.preventDefault();
if ($(this).hasClass('close')) {
hideSearch();
} else {
showSearch();
}
});
// Handler for pressing show/hide button.
$hsToggle.on('keydown', function (event) {
// If tabbing from toggle button, and search is hidden, exit early.
if (event.keyCode === 9 && !$header.hasClass('search-visible')) {
return;
}
event.preventDefault();
handleKeyDown(event);
});
// Handler for tabbing out of the search bar when focused.
$hsInput.on('keydown', function (event) {
if (event.keyCode === 9) {
hideSearch(event.target);
}
});
// Helper function to show the search form.
function showSearch() {
$header.addClass('search-visible');
$hsWrap.fadeIn('fast').find('input[type="search"]').focus();
$hsToggle.attr('aria-expanded', true);
}
// Helper function to hide the search form.
function hideSearch() {
$hsWrap.fadeOut('fast').parents('.site-header').removeClass('search-visible');
$hsToggle.attr('aria-expanded', false);
}
// Keydown handler function for toggling search field visibility.
function handleKeyDown(event) {
// Enter/Space, respectively.
if (event.keyCode === 13 || event.keyCode === 32) {
event.preventDefault();
if ($(event.target).hasClass('close')) {
hideSearch();
} else {
showSearch();
}
}
}
};
// Expose the init function only.
return {
init: init
};
})( jQuery );
jQuery( window ).on( 'load', genesisSample.init );
Step 2
Edit functions.php.
a) Inside genesis_sample_enqueue_scripts_styles(), add at the end
wp_enqueue_style(
'monochrome-ionicons', '//code.ionicframework.com/ionicons/2.0.1/css/ionicons.min.css',
array(),
CHILD_THEME_VERSION
);
b) At the end of functions.php, add
add_action( 'genesis_header', 'custom_get_header_search_toggle' );
/**
* Outputs the header search form toggle button.
*/
function custom_get_header_search_toggle() {
printf(
'<a href="#header-search-wrap" aria-controls="header-search-wrap" aria-expanded="false" role="button" class="toggle-header-search"><span class="screen-reader-text">%s</span><span class="ionicons ion-ios-search"></span></a>',
__( 'Show Search', 'genesis-sample' )
);
}
add_action( 'genesis_header', 'custom_do_header_search_form' );
/**
* Outputs the header search form.
*/
function custom_do_header_search_form() {
$button = sprintf(
'<a href="#" role="button" aria-expanded="false" aria-controls="header-search-wrap" class="toggle-header-search close"><span class="screen-reader-text">%s</span><span class="ionicons ion-ios-close-empty"></span></a>',
__( 'Hide Search', 'genesis-sample' )
);
printf(
'<div id="header-search-wrap" class="header-search-wrap">%s %s</div>',
get_search_form( false ),
$button
);
}
Step 3
Edit style.css.
a) Above the media queries, add
.site-header > .wrap {
position: relative;
}
.menu-toggle {
margin: 10px 30px 0 auto;
}
.header-search-wrap {
display: none;
}
.js .header-search-wrap {
position: absolute;
z-index: 1001; /* Show above the menu toggle button */
top: 0;
left: 0;
width: 100%;
height: 100%;
-webkit-transition: none;
transition: none;
-webkit-transform: translate3d(0,-100%,0);
transform: translate3d(0,-100%,0);
}
.js .header-search-wrap .search-form,
.js .header-search-wrap input[type="search"] {
height: 100%;
}
.search-visible .header-search-wrap {
-webkit-transform: translate3d(0,0,0);
transform: translate3d(0,0,0);
}
.header-search-wrap:target {
display: block;
clear: both;
position: relative;
}
.header-search-wrap input[type="search"] {
padding: 0;
border: 0;
}
.header-search-wrap input[type="submit"]:focus {
right: 50px;
margin-top: 1px;
padding: 20px;
border-radius: 3px;
}
.js .toggle-header-search.close:focus {
outline: 0;
}
.toggle-header-search.close {
position: absolute;
z-index: 100;
top: 0;
right: 0;
width: 30px;
height: 100%;
padding: 0;
color: #000;
-webkit-transition: -webkit-transform 0.2s ease-in-out;
transition: -webkit-transform 0.2s ease-in-out;
transition: transform 0.2s ease-in-out;
transition: transform 0.2s ease-in-out, -webkit-transform 0.2s ease-in-out;
-webkit-transform: translate3d(-6px,0,0);
transform: translate3d(-6px,0,0);
}
.header-search-wrap:target .toggle-header-search.close {
-webkit-transform: none;
transform: none;
}
.toggle-header-search .ionicons {
font-size: 12px;
font-size: 1.2rem;
-webkit-transform: scale(2);
transform: scale(2);
}
.toggle-header-search.close .ionicons {
position: absolute;
top: calc(50% - 5px);
right: 0;
-webkit-transform: scale(3);
transform: scale(3);
}
.toggle-header-search {
position: absolute;
right: 5px;
top: 20px;
}
b) At the end of 960px min-width media query, add
/* Header Search
--------------------------------------------- */
.site-header > .wrap {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-pack: justify;
-ms-flex-pack: justify;
justify-content: space-between;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
position: relative;
}
.title-area,
.nav-primary {
float: none;
}
.nav-primary {
-webkit-box-flex: 1;
-ms-flex: 1;
flex: 1;
text-align: right;
}
.toggle-header-search {
position: static;
display: block;
padding: 12px 15px;
line-height: 1;
margin-left: 20px;
}
Great post! Just what I was looking for!
Unfortunately, it does not really work for me yet.
I want to use the fontawesome icons for close ans search but after clicking on search icon, this remains visible. How can I let icon get display: none?
How can I translate “Search this website”?
My search icon is offset on the mobile view.
See here: https://test.vergleich24.at/tagesgeld
Thanks!
Beste regards,
Alex
Follow https://sridharkatakam.com/header-search-in-business-pro/.
The same way you can translate other translatable text. Try https://lovelyconfetti.com/how-to-translate-your-genesis-child-theme/.
[…] paralizer on Header Search in Genesis […]
Hi Sridhar, thank you so much for this tutorial, this is what I was looking for.
I followed the tutorial in my localhost server and I can achieve the search icon, but next to the logo, am I missing something?
Here’s the screenshot – https://imgur.com/a/0cH1G
I made below changes and search icon is in the right place now…
.nav-primary {
-webkit-box-flex: 1;
-ms-flex: 1;
flex: 1;
text-align: right;
margin-right: 60px;
}
.toggle-header-search {
position: absolute;
display: block;
padding: 6px 0 15px;
line-height: 1;
margin-left: 20px;
}
In Monochrome Pro Theme, the icon is a menu-item… with the value “static”, it renders in order as it appears in the document flow.
Your fix worked. Thanks.
I forgot to say it:
In Monochrome Pro Theme, the search icon is not available in the mobile version.
Any reason?
Sorry for my English. I know it’s not so good.
Because of
in the 1023px media query.
Yes. But why to hide it?
people don’t search in mobile versions?
Your implementation is better.
I think i know why they have hidden the search icon in mobile version:
if you open menu in horizontal mode and, then, you do click in the search icon, see what happens…
if you have a lot of menu-items, the mobile keyboard will cover the search input box…
Could be that the reason? An aesthetic reason?
Tested in iPhone 8.
Siam, thank you for sharing that extra bit of code.
[…] This tutorial is by Sridhar Katakam – https://sridharkatakam.com/header-search-in-genesis/ […]
Hi,
I followed the steps given above, but I still can’t get the search icon in the header can you help me out.
Can you provide the URL of your site?
netsmush.com
Hi there,
I use the Genesis Sample theme, but the icon is displayed right of the logo image and not last in the menu.
Thx for you great tutorial.
HJ
Sorry, a refresh does help :-S.
Hi Sridhar,
The menu get all messed up after the search in Internet Explorer 11 on Windows 7, any solutions?
Thx,
HJ
Thank you, that works very well. How would you center the title area / site title and move the magnifying glass icon to the extreme left? Thanks.
Hi Sridhar,
Thank you for all that you do! I’m trying to accomplish this in Altitude Pro, but am not sure where to implement some of the code. For example, Altitude Pro does not have a file similar to js/genesis-sample.js. I tried following the tutorials for other themes that accomplish this but am having the same questions as to where to place the code specifically for Altitude Pro. Your expertise and guidance would be more than appreciated 🙂
Follow https://sridharkatakam.com/header-search-in-altitude-pro/.
[…] the comments section of Header Search in Genesis, a user […]
Thanks for the tutorial, after I follow the steps. the search icon is not on the right side after the menu, but on the right after the web title, can you help me? is there a step I miss?
Site URL?
Thanks for the reply, this work perfectly after add css from siam..
Add this css…
.nav-primary {
-webkit-box-flex: 1;
-ms-flex: 1;
flex: 1;
text-align: right;
margin-right: 60px;
}
.toggle-header-search {
position: absolute;
display: block;
padding: 6px 0 15px;
line-height: 1;
margin-left: 20px;
}
Thanks for sharing this code..
Hi,
I followed the steps but I am not getting the result as shown above can you help me out.
Deactivate/disable caching/minification and post back when done.
It is working after I disabled minification. Should I keep the same settings or change the settings.
I’ve been trying to replace the Ionicons font icon with the new Ionicons web component (SVG) but it is way beyond my abilities.
That would be a nice tutorial, it would be cool the replace everything, even dashicons from the responsive menu and from the Genesis faq plugin with Ionicons SVGs
thanks its working
Thank you very much, made it work for Magazine-pro. Just had to make a new js/genesis-sample.js file and add the following at the end of function magazine_enqueue_scripts() for it to load:
wp_enqueue_script(
‘genesis-sample’,
get_stylesheet_directory_uri() . ‘/js/genesis-sample.js’,
array( ‘jquery’ ),
CHILD_THEME_VERSION,
true
);
I am wanting to do this on my site which uses magazine pro as well. do you think you might be able to provide the code you used to make this work?
I followed the instructions twice but still does not give the same output.
Please assist.
Genesis Sample Theme
URL: http://www.singaporeolevelmaths.com
Hi,
I really love what you’ve done here so far.. your tutorial is amazing.. However, I do the steps you mentioned, and the result was great.. But one problem occur.. When i click the Search Icon.. there’s no result. it supposed to be display the search box/ form right?
can you take a look on my site pls..
Regards,
Marvin
thanks but good working in mobile version please check out my blog
Hello sridhar.
I have use your genesis-sample edit ( Inline menu header)
Can you share the free tutorial for showing the search box and icon at the primary menu header?
I really like your theme. but I need the search box feature.
Thanks
Thank you for the tuto. It worked in development environment but I get a weird error in production : “wp is not defined” in genesis-sample.js.
Any idea what’s wrong? Also in production the form doesn’t display properly but it’s maybe linked to this js error?
Here is the website: https://www.lalanguefrancaise.com
Thanks,
Nicolas.
Easy tutorial, thanks! I’m trying to solve one issue I’m having with it though… maybe you can help me. When the search bar is expanded, it pushes the navigation down but covers the site-title. I’d like for it to clear both the site-title and the navigation and show below it when expanded.
how to hide search text in mobile version
Hi Sridhar,
I have this up and running beautifully on all browsers but Internet Explorer and OF COURSE the client wants it compatible with IE, grrrrrr! Anyway, I thought I would ask if there is any way to make this compatible with IE or if I should just bag this header design for this client.
I appreciate any suggestions. 🙂
Caroline
Hi Sridhar,
I have this up and running beautifully on all browsers but Internet Explorer and OF COURSE the client wants it compatible with IE, grrrrrr! Anyway, I thought I would ask if there is any way to make this compatible with IE or if I should just bag this header design for this client.
I appreciate any suggestions. 🙂
Caroline
Hello,
I have dark background so I want the icon to be white.
How-to