There are several solutions on the WordPress Customizr forum for adding search forms. Here’s a way to add a minimal search form to the Customizr main menu.
Two methods are given here:
- The first method uses the in-built Genericons magnifying-glass icon. This method is fast, because the Genericons font is already loaded in the page. (Credit to Rocco for helping me get this right.)
- The second method allows you to choose your own image for your the search icon. This method is very slightly slower, as an extra http request is required to get the image, but it gives you more flexibility to choose a search icon that you like.
First add the search form to the menu
Add the following code to your functions.php:
// As of 3.1.10, Customizr doesn't output an html5 form. add_theme_support( 'html5', array( 'search-form' ) ); add_filter('wp_nav_menu_items', 'add_search_form_to_menu', 10, 2); function add_search_form_to_menu($items, $args) { // If this isn't the main navbar menu, do nothing if( !($args->theme_location == 'main') ) // with Customizr Pro 1.2+ and Cusomizr 3.4+ you can chose to display the saerch box to the secondary menu, just replacing 'main' with 'secondary' return $items; // On main menu: put styling around search and append it to the menu items return $items . '<li class="my-nav-menu-search">' . get_search_form(false) . '</li>'; }
Where to copy/paste this code? => in your functions.php file. We strongly recommend you create a child theme. Download a start-up child theme here.
Remember: you shouldn’t edit the theme’s functions.php.
This adds a search form to your WordPress main menu. Once you’ve added this code, you can refresh your page and you’ll see a search form. But it’s still very messy. The pieces are all there, but they need to be styled.
Next, style your search form
In the php code above, we added a class of my-nav-menu-search
to the new menu item. We did this so that we can style it differently to all the other searches on your site. For example, you might have added a search in a widget on your site. And then there is a search on Customizr’s “404” page, which is the page where visitors are taken if they try to access a page that doesn’t exist (see it in action here). The my-nav-menu-search
class will allow us to style only our new menu item, without affecting any other searches on the site.
We need to do the following:
- Move the search form to the right of the menu.
- Stop the display of the Search button.
- Move the “Search for” label off screen. (It’s better to move it off-screen than to not display it at all, because automatic screen readers need to be able to tell people what the input field is for.)
- Style the search input field so that all you see is the search icon.
- Make the search field expand when you click the icon (when it’s active or gets focus), and make its expansion gradual.
The styling is slightly different for each method:
Method 1: Using the Genericons magnifying glass icon
To style the search form, add the following to your child theme‘s CSS:
/* my-nav-menu-search menu item created in functions.php. Move it way over to the right */ .navbar .nav .my-nav-menu-search { float: right; } .navbar .nav { width: 100%; } .my-nav-menu-search .search-form { position: relative; margin: 0; } /*Stop the display of the Search button*/ .my-nav-menu-search .search-submit { display: none; } /* The "Search for" text is needed for screen readers, but we move it off screen, so we won't see it */ .my-nav-menu-search .search-form .screen-reader-text { position: absolute; left: -9999px; overflow: hidden; } /* Style the search input textbox */ .my-nav-menu-search .search-field { background: transparent; border: none; -webkit-box-shadow: none; -moz-box-shadow: none; box-shadow: none; cursor: pointer; height: 26px; margin: 2px 0 2px 0; padding: 0 0 0 36px; position: relative; -webkit-transition: width 400ms ease; -moz-transition: width 400ms ease; -o-transition: width 400ms ease; transition: width 400ms ease; width: 0px; } /* Expand the search box when you click it */ .my-nav-menu-search .search-field:active, .my-nav-menu-search .search-field:focus { color: #5a5a5a; /* change the colour above if you are working with a dark navbar background */ border: 2px solid #c3c0ab; cursor: text; outline: 0; width: 70px; -webkit-box-shadow: none; -moz-box-shadow: none; box-shadow: none; margin: 0; } /* Add a magnifying glass background */ .my-nav-menu-search .search-form:before { font-family: 'Font Awesome\ 5 Free'; content: '\f002'; position: absolute; /* this is the key to put it visually inside the search field */ font-size: 19px; font-weight: bold; top: 5px; /* tune this vertical alignment inside the search field, as needed */ left: 5px; /* tune this horizontal alignment inside the search field, as needed */ } /* Reset nav width and search floating for mobile menu */ @media (max-width: 979px){ .navbar .nav .my-nav-menu-search { float: left; } .navbar .nav { width: auto; } }
Method 2: Using your own image as a search icon
Get yourself a search image
First things first: you’ll need a search icon image—a small magnifying glass, for example. You can find these by googling. I found these black icons and these white icons, which are free to use. The 24px x 24px PNG files work well. There’s also a black icon in the Twenty Thirteen theme; and here’s a grey icon Alternatively, you can make your own image with an image editor.
Place the icon image file in your child theme’s folder (using ftp) or in your media library (using the WordPress “Add new” button in the media library). My preference is to store the icon in your child theme, so that the child theme is self-sufficient and all the files are in the same place.
In either case, you will need to know the image file’s URL. The file’s URL will be something like this:
- If the image is in your child theme: http://example.com/wp-content/themes/customizr-child/search.png
- If the image is in your media library: http://example.com/wp-content/uploads/2014/04/search.png In the media library, you can see the file’s URL in the panel on the right when you edit an image.
Now you’ve got your image, let’s style your search form.
Styling the search form
To style the search form, add the following to your child theme‘s CSS:
/* Styling a search form in the menu -- see accompanying php*/ /* my-nav-menu-search menu item created in functions.php. Move it way over to the right */ .navbar .nav .my-nav-menu-search { float: right; } .navbar .nav { width: 100%; } /*Stop the display of the Search button*/ .my-nav-menu-search .search-submit { display: none; } /* The "Search for" text is needed for screen readers, but we move it off screen, so we won't see it */ .my-nav-menu-search .search-form .screen-reader-text { position: absolute; left: -9999px; overflow: hidden; } /* Add a magnifying glass background and style the search input textbox */ .my-nav-menu-search .search-field { background-color: transparent; /* CHANGE THIS LINK TO POINT TO YOUR SEARCH ICON */ background-image: url(/wp-content/themes/customizr-child/search.png); background-repeat: no-repeat; background-position: 5px center; -webkit-background-size: 18px 18px; background-size: 18px 18px; border: none; -webkit-box-shadow: none; -moz-box-shadow: none; box-shadow: none; cursor: pointer; height: 26px; margin: 2px 0 2px 0; padding: 0 0 0 36px; position: relative; -webkit-transition: width 400ms ease, background 400ms ease; -moz-transition: width 400ms ease, background 400ms ease; -o-transition: width 400ms ease, background 400ms ease; transition: width 400ms ease, background 400ms ease; width: 0px; } .my-nav-menu-search .search-field:active, .my-nav-menu-search .search-field:focus { background-color: #fafafa; color: #5a5a5a; /* swap the two colours above if you are working with a dark navbar background */ border: 2px solid #c3c0ab; cursor: text; outline: 0; width: 70px; -webkit-box-shadow: none; -moz-box-shadow: none; box-shadow: none; margin: 0; } /* Reset nav width and search floating for mobile menu */ @media (max-width: 979px){ .navbar .nav .my-nav-menu-search { float: left; } .navbar .nav { width: auto; } }
Caveats
- This approach works well on sites with sufficient space to the right of the menu items. Change the ” width: 70px;” declaration above to make the search field narrower or wider.
- The search icon is not clickable after you enter your search—it’s not a button or a toggle. Getting an effect where you can click once to open the search field and click again to search would require javascript. The method above uses only php and CSS.
- This solution adds html5 support to the WordPress search forms on your site. This allows us to add the “Search …” text inside the search field (this functionality was added in WordPress 3.6).While it’s good to enable html5 support in the search field this way, if you have styled any other search boxes on your site you may need to revisit their CSS, as they will also now output with html5-enabled elements.
Need help?
If you’ve never used a functions.php file, check out this article. If you need more help, post your question on the WordPress Customizr forum.
77 thoughts on “Adding an HTML5 search form in your WordPress menu”
Hi there,
I just used the above code in my website and it was infected with a trojan!
I get a message from Kaspersky that I have “Trojan-Downloader.JS.FakejQuery.b”
I have removed that code and css, but it’s still infected!
I had to delete EVERYTHING!
🙁
nils
Hi,
I am sorry about your site getting infected.
This code has been used by numerous users in the past and I don’t think this introduces a trojan in any site.
You can try to secure your site against attacks in future.
Yes, I can imagine that, and still, the website was fine one minute before and then next minute after putting in this code I got a warning from Kaspersky.
I don’t understand it either, I just know that I can’t use this code. I can’t understand HOW this has happend, but that was the only thing I changed and the effect was dramatic.
Just wanted you to be aware of that.
Thanks!
Nili
Hello,
is there a way to add the search box to the menu bar or header without having to change the functions.php file? i’d rather avoid making a child theme, since that’s pretty much the only change i need to apply to the site(…so far..). 🙂
thx
Hi alessandro,
You could check if there are any plugins for the same. Or you could add Google Search, again using a plugin.
hi,
I really like this implementation, but I’m looking to add the search bar after the social media links OR before/after site description !?
thk
@+++MasterSlash
Hi,
This can be better diagnosed at the support forums.
The developer team handles support for Customizr at https://wordpress.org/support/theme/customizr and for Customizr-Pro at http://presscustomizr.com/support/. Please post your query there with a link to your site.
Thanks 🙂
Hi! The search generic icon stopped working and its been a while now. The search bar is fine, but i can’t see the icon, it just shows me a F400 kind of code. Can someone help?
Please read my comment below with the posted solution. Cheers
Thanks! I had already tried your solution before posting but it wasn’t working, but now it works! Thanks again 😉
Firstly, I sincerely want to thank Team Customizr for such an awesome theme and extended support. My question, is it possible to use Font Awesome icons instead of Genericons (for adding the Search bar in Nav). If yes, I am certain it can be used in many other situations, such as for Social icons, etc. Please share some general insights or share a helpful link.
Icons used in theCustomizr WordPress Theme
Hi Menaka, thanks for the prompt response. I checked that link and tried many times, but still unable to use these Awesome icons. My website is seowebup.com and I want to use the Skype icon in the Footer Widget area one, instead of the “(Skype ID)”. Please help. Thanks in advance.
Hi Ravi,
Add this code where you want the icon to appear
Add the following to your child theme’s style.css
Hi Menaka, Thanks for sharing the code, I am not sure if this is the right place to discuss about this topic. But I am using the icon in footer (Widget one) and there are few issues that I am facing. First the icon is too small, and second the icon is going to the next line (I tried using span, but still the same problem).
Hi,
The developer team handles support for Customizr at https://wordpress.org/support/theme/customizr and for Customizr-Pro at http://presscustomizr.com/support/. Please post your query there with a link to your site.
already posted above:
switch to Font Awesome, in Method-1 css, I just changed lines 59 and 60 into this:
Thanks Giorgio, I noticed your response after posting the comment.
Hi,
Create two different menus, one with search form and one without. Place the first one in header and the second one in footer.
Thanks for your reply. I was wondering if there is a way to add menu items restricted by the context (placeholder) so you can make it only to act for header (for example).
Thanks,
Alexis
I see this works. But what happen If I place the same menu on footer and I do not want the search form there. In other words, this search form will be placed anywhere that menu exists. Hide it via css is not an elegant option.
Hello, thank you for the tutorial, I managed to do it but would prefer to have google custom search in the menu or anywhere on top of the shop because Wordpress search is too slow, can you help me ? Thank you.
Thanks, great for Chrome and IE (ok in Edge too), but does not show up at all in Firefox (ver 46.0.1) – anyone confirm this?
Hi,
following the switch to Font Awesome, in Method-1 css, I just changed lines 59 and 60 into this:
Hope it helps
Thanks Giorgio. It sure will help some.
Hi Giorgio, I was just looking how to do this so thank you! My icon hasn’t updated although I’m on the most recent Customizr (I updated the CSS in my Child Theme). Did you need to updated the Font Awesome somewhere else? Hope you can help! Sarah
Hi Sarah,
did you enable the FA in the customize options?
To use additional Font Awesome icons we need to enable them through the customize option panel.
Select Customize, Advanced option, Front-end Icons (Font Awesome) and enable Load Font Awesome CSS
To use additional Font Awesome icons we need to enable them through the customize option panel.
Select Customize, Advanced option, Front-end Icons (Font Awesome) and enable Load Font Awesome CSS.
This works great! I have one small issue though, on an android mobile device, search input field in the collapsible menu is blocked by the keyboard that pops up. Is there a way to fix this? The content is supposed to be pushed up by the keyboard but for some reason in this instance it is not.
Hi,
The developer team handles support for Customizr at https://wordpress.org/support/theme/customizr and for Customizr-Pro at http://presscustomizr.com/support/. Post your query there with a link to your site.
Special thanks for Nicolas that it works perfectly in my theme also thanks for great and quick responsive support.
Works perfectly thanks Nicolas.
Since the newer versions of Customizr ‘main’ and ‘secondary’ menus are enforced, here is the same snippet in case somebody has the ‘secondary’ menu set as horizontal:
add_theme_support( 'html5', array( 'search-form' ) );
add_filter('wp_nav_menu_items', 'add_search_form_to_menu', 10, 2);
function add_search_form_to_menu($items, $args) {
// If this isn't the main navbar menu, do nothing
if( !($args->theme_location == 'secondary') ) // or set to 'main' if the option in Customizr is enabled
return $items;
// On main menu: put styling around search and append it to the menu items
return $items . '' . get_search_form(false) . '';
}
Hello and thank you for this amazing snippet. It works perfectly, but I
have a question: What do I need to change in the php when I want the
search in social media block (search icon shown like the last social
media icon). Thank you for any help.
Hi,
I have implemented the snippet and it has worked well. But I still not found a solution how to change placeholder text in Search Form (eg from “Search ..” to “Search & Hit Enter”)? And how to change position of the Image from the left to the right in Search Form (I use Method 2: Using your own image as a search icon)?
Thanks!
This snippet works perfect for search form in menubar!
Is it possible to add search form in navbar but not in menubar as a menu item? Now while viewing in mobile search form is displayed once tapped on menu. It would be great if search form is displayed between menu and social icons on mobile.
Hi!
After adding the search box to the navigation bar, the “Search for” text appeared above the search box in the sidebar. Does anybody know how to move it off screen in this case, too?
Thank you in advance!
http://bookshelfreflections.com
Hi Karolina !
Would you mind posting your help request in the support forum ? This is where support and debug is provided in priority.
Thanks 🙂
Works perfectly. Thanks for sharing your knowledge.
Just to follow up:
1. Absolutely positioned element should be positioned with ‘top:6px; left:6px;’, rather than with padding.
2. For an absolutely positioned element to stay within it’s Parent, the Parent needs to have a position, such as position:relative. For a :before or :after pseudo-element, the Parent is the main element: in this case, .search-form
Doing this works fine for me, and I think this is the preferred way to position Absolute elements,and to keep them within their Parent. Thanks again for your tutorial.
Hello Bill,
I agree 😀
Thank you for your contribution on this!
Snippet updated.
Hello,
First of all, thanks so much for this great and easy to customise theme!
I have just implemented the search bar in the menu. Now I would like it to be without the ‘Search for’ tekst and without the ‘Search’ button.
Any ideas on how to do this?
Thanks!
It’s fixed… idk how.. but it is 😀
When I copy the provided code into my theme’s child .css document, nothing happens to the search menu. Any idea why this is? Thanks!
I figured out the problem– I was importing from the parent .css file, so the code was not applying to the child’s function.php code. Thanks!
Hi,
Last question now.. I really like this implementation, but I’m looking to add the search bar after the social media links.
Is there an easy way to add this search bar after the social media icons instead of the navigation bar?
Thanks,
Sean
hello, have you the solution? i’ve the same interrogation..
thanks
@+++ MasterSlash
Hi all,
This sounds like a great solution, thanks for providing it!
I’ve managed to get the search bar to appear, however the styling of the bar doesn’t seem to be working.
Has anyone else had any issues with this? Perhaps WordPress/Customizer have changed the core functionality for the search?
Any help would be much appreciated!
Thanks,
Sean
Hi there,
Not to worry I have managed to figure this out now! Thank you again
I tried changing the color of the search box border from
.my-nav-menu-search .search-field:focus {
color: #5a5a5a;
/* change the colour above if you are working with a dark navbar background */
border: 2px solid #c3c0ab;
TO
.my-nav-menu-search .search-field:focus {
color: #5a5a5a;
/* change the colour above if you are working with a dark navbar background */
border: 2px solid #f78c40;
but it stays the same…. am I missing something here??
Thank you so much for this quick solution! I just added it to my theme and it seems to work perfectly with the current version of Customizr (3.2.10).
I am glad it helped!
Thanks! Worked perfectly and looks great!
Thank you so much for this snippet. For some reason I can see the magnifying glass icon when I add CSS in the ‘customize’ but, when I save and go to the website, the icon doesn’t show. Anyone happen to have suggestions? Thanks.
Yotum, did you ever get an answer for this?
Hi Todd and Yokam, as stated in the snippet:
So that code has to be put in the child-theme style.css
Todd, you can’t use anymore quotes and double quotes (which previously could be escaped with the backslash ‘\’) in the custom css-box due to new wordpress theme guidelines.
Hope this helps.
Hello and thank you for this amazing snippet. It works perfectly, but I have a question: What do I need to change in the php when I want the search in social media block (search icon shown like the last social media icon). Thank you for any help.
Hi Beth,
You’ll want to open a thread in the Customizr support forum about this.
Thanks
Cheers
Hello, great piece of code, although I do not have enough knowlwdge of CSS/php, I know it used to work before in older version, but as said here in new version for search-form, I have a few things to ask,
1. Can I have the search form at the top of Nav bar
2. How can I get the search box extend animation to open from right to left in-stead of left to right.
Hello, what if i need this search bar to search only some image folder in my directory ?
Hi karim. This is a question that is beyond the scope of this snippet. I don’t think that Wordpress can distinguish a search by a folder. It would need coding. (But I may be wrong.) Another approach would be to tag the images and then limit the search to the tag using an approach similar to the one shown here.
I am not using this theme but it causes errors when i try to implement the functions.php changes just curious if it the position i put the code as copy and pasted from above or is it another reason… Thanks NB
Parse error: syntax error, unexpected ‘;’ in /home/nebcrte1/public_html/afh/wp-content/themes/afhparts/functions.php on line 37
– Parse error: syntax error, unexpected T_STRING in /home/nebcrte1/public_html/afh/wp-content/themes/afhparts/functions.php on line 39
Hi Nathan,
This looks like a common error in php / WordPress :
http://codex.wordpress.org/Common_WordPress_Errors
http://codex.wordpress.org/User:Skippy/Common_PHP_Errors
Hope this helps!
Nicolas, thank you for your reply that being said what is missing from the code above in this tutrorial that goes in the functions.php file as that is what i figured but i am not as proficient in php to know what that is…?
Hi Nathan,
this snippet is composed of two parts : the php part has to be pasted in the functions.php file, the CSS part in the style.css.
There are some basic rules to follow when you implement those kind of customizations (like for example, to include the php code opening tag in functions.php if it’s not included yet) . It is partly explained here :
http://www.presscustomizr.com/customizr/how-to-customize-customizr-wordpress-theme/
http://www.presscustomizr.com/customizr/diagnosing-problems-customizr/
Take care! 🙂
Hi Nathan. If you’re not using Customizr, then this snippet won’t work 100% anyway, as the CSS in your theme will be different.
this code makes the menu nav bar much fatter with a lot white space below the menu line than before, any idea how to fix it? thx
You can set height from 26px to 0px. That should help.
In this example i already changed the height from 26 to 0px.
Sorry. Mistaken… now it does not open search box.
Nevermind, looks like I solved the problem. I changed
to
Thanks for the awesome snippet!
Is there an easy way to make it stop from autohiding the search box altogether, but still hide the search button?
opsi !Notify me of followup comments via e-mail
could you help me if i want this search bar on the top right corner