Updated on May 16, 2018
In Genesis Facebook group a user asked:
How do you add a search form to your Genesis theme (in your nav bar)? (I googled the same question and a whole lot of different answers came up). I read that this used to be included and now you can add some custom code but has anyone added a search form recently? I’m running the Infinity-pro theme.
Any idea on how to just add a magnifying glass to the navigation which expands into a search form when clicked?
This tutorial provides the steps to add a magnifying glass icon at the end of primary nav bar menu items in Infinity Pro, which when clicked will expand to show a search form. Typing a search query and hitting return will display the search results.
Clicking anywhere on the page when the search box is visible will fade it away.
Screenshots:
Upon clicking:
Screencast:
Tested in Infinity Pro
Step 1
Add the following in Infinity Pro’s functions.php:
add_filter( 'wp_nav_menu_items', 'custom_nav_search', 10, 2 );
/**
* Add search box to nav menu.
*/
function custom_nav_search( $items, $args ) {
if ( 'primary' === $args->theme_location ) { // affect only Primary Navigation Menu.
$items .= '<li class="menu-item search">' . get_search_form( false ) . '</li>';
}
return $items;
}
Step 2
Add the following in Infinity Pro’s style.css:
.menu-item.search {
margin-left: 30px;
}
.nav-primary .genesis-nav-menu {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
align-items: center;
-webkit-box-align: center;
-webkit-box-pack: center;
-ms-flex-align: center;
-ms-flex-pack: center;
justify-content: center;
}
.search .search-form {
text-align: center;
}
.search .search-form input[type="search"] {
position: relative;
width: 1px;
padding: 8px 15px;
border: none;
border-radius: 4px;
background-color: transparent;
font-family: "Open Sans", sans-serif;
font-size: 13px;
cursor: pointer;
-webkit-transition: width 500ms ease, background 400ms ease;
transition: width 500ms ease, background 400ms ease;
}
.search .search-form input[type="search"]:focus {
width: 250px;
outline: 0;
background-color: #fff;
cursor: text;
}
.genesis-nav-menu .search input[type="submit"] {
clip: rect(0, 0, 0, 0);
position: absolute;
width: 1px;
height: 1px;
margin: -1px;
padding: 0;
border: 0;
}
.search .search-form:before {
top: 50%;
left: 50%;
width: 20px;
height: 20px;
-webkit-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
}
.home .search .search-form:before {
color: #fff;
}
.site-container.white .search .search-form:before {
color: #999;
}
@media only screen and (max-width: 862px) {
.nav-primary .genesis-nav-menu {
display: block;
}
}
@media only screen and (max-width: 800px) {
.menu-item.search {
margin-bottom: 10px;
margin-left: 20px;
}
.search .search-form:before {
position: absolute;
top: 10px;
left: 0;
color: #999;
-webkit-transform: none;
transform: none;
}
.search .search-form {
text-align: left;
}
.search .search-form input[type="search"] {
padding-right: 0;
padding-left: 0;
}
}
Great tutorial, but I have a couple of problems with it:
1) Using Edge browser, the ‘search’ magnifying glass icon also displays a couple of characters from the ‘Search this site’ text. Looks fine in Chrome, but I would like to cater for users with Edge as well.
2) When the menu collapses for small screens, the search icon is not displayed.
Any suggestions how I can fix these issues?
Thanks.
Could you share the code for Authority-Pro? I tried above but it didn’t work properly in Authority Pro.
Questions for you as I try this out on a site with infinity pro…
I don’t want it on the footer menu…what should I change to make that distinction?
The icon doesn’t show up when scrolled all the way to the top on the front page? Any thought’s on why it dissapears? faberinstitute.com
What do adjust the setting of how the search results are displayed?
Updated the code in Step 1 to ensure that the icon gets added only to the primary menu.
I do not see the icon on your site. Have you removed it?
How do you want them to be displayed?
Hi Sridhar! Sorry, I didn’t get a notification of your response. Thanks for adjusting the code to be only in the header. I got it working, but I did have to change the color to show up in the white navigation bar.
What I’m wondering is how to get the search results to show up in a simple list. Right now I have some extra CSS applied to format the blog page into a tile format and I think it’s messing with the search results. Any ideas?
You can copy search.php from the parent directory to your child theme and edit it to add the code that removes unwanted elements like content, entry-footer etc.
I published a tutorial with detailed steps on this but it’s for members-only.
https://sridharkatakam.com/custom-genesis-search-template-to-show-results-in-a-list/
[…] navigation-search.scss: styling for adding a search input to a navigation menu (based on this tutorial from Sridhar Katakam) […]
Hi Sridhar,
How would you integrate this in to the genesis sample theme? I’ve copied the code and sort of works but the you can’t see the magnify glass icon? Thank you.
You need to add the
font-family: "dashicons"; content: "\f179";
to the .search .search-form:before CSS class.Hi Sri,
Can you share the code of eleven 40 pro search icon for mobile only.
Hi Sridhar,
Should this be able to work with other Genesis themes like Parallax Pro?
Hi Sridhar,
Thank you for this. I’m using Magazine Pro. At first, it was not showing the magnifying glass icon, then I followed your instructions to add the font-family: “dashicons”; content: “f179”; to the .search .search-form:before CSS class.
I have tweaked a little bit on css to float my navigation menu list to left and the magnifying glass icon to the right.
You can see it here: https://androidscrib.com
Again, thank you for this great tutorial.
Thanks so much, Sridhar, worked perfectly!
How to add hover so when I click on the magnifier it opens the search box? I followed your code but doesn’t work as a hover it’s just a search box. how can I add this effect like yours?
Newbie question here, I’m afraid. I used your excellent tutorial, including the dashicon font family hint in the comments. I did a lot of experimenting on function.php and style.css (after backing up the originals, of course). I got it working very quickly, but then went in and tried to make the search box’s border visible when it replaced the magnifying glass icon. That led me down a slippery slope.
Here’s the question: the’s tutorial says what to insert, but it doesn’t specify where. I found out the hard way that it makes a difference. Can anyone give me some pointers about where to insert the code in function.php, and where the two css insertions should go? (Told you it was a newbie question!)
Another variable: Do I need the Search widget installed on the home page to make this work, or is the code above independent of the widget?
Replacing my previous question: Apparently the icon has been showing up fine — everywhere but the home page, where I’ve been looking for it. How do I make the code work on the home page header menu, too? It’s driving me crazy.
FWIW, I also added a border that appears only on the edit box:
.search .search-form input[type=”search”]:focus {
width: 250px;
outline: 0;
background-color: #fff;
cursor: text;
/* START BRIAN CHANGES /
border: 1px solid #bbbbbb;
border-radius: 7px;
/ END BRIAN CHANGES */
}
Many thanks for the excellent resource and help!
UPDATE: Using Sridhar’s tutorial and a bunch of experimentation, I managed to get a search box working. It turns out that something has changed since the tutorial was written, and inserting the “dashicon” line suggested in the comments has no effect. The search icon appeared beautifully everywhere but on the home page, where it was present but invisible.
Instead the comment fix, I modified Sridhar’s code by adding three lines to style.css:
.home .search .search-form:before {
color: #fff;
/* Add three lines copied from .search-form:before at ~line 823 in the
SEARCH FORM section. /
color: #999;
content: “f4a5”;
font-family: “ionicons”;
/ End addition */
}
I also added a slight modification to make the search field’s borders visible so it looks more like a place for text entry:
.search .search-form input[type=”search”]:focus {
width: 250px;
outline: 0;
background-color: #fff;
cursor: text;
/* Add to make search box borders visible, and with rounded corners /
border: 1px solid #bbbbbb;
border-radius: 7px;
/ End addition */
}
I hope this helps somebody.
Got this working on Understrap with minimal effort! Thanks!