Have you seen the search icon with toggle effect on many popular websites? Take a look at our sister project List25 for an example. The idea is to display a simple search icon, and when the user clicks on it the search forms slides out also known as the toggle effect. It is a neat effect that also saves space and allows your users to focus on the content. Not to mention, this is great for mobile responsive themes. In this article, we will show you how to add a search toggle effect in WordPress Themes.
Note: This tutorial is for intermediate users with working knowledge of WordPress template tags, HTML, and CSS. Beginner level users are advised to practice on local server first.
Displaying WordPress Search Form
WordPress adds default CSS classes to HTML generated by various template tags inside a theme. WordPress themes use <?php get_search_form(); ?>
template tag to display search form. It can output two different search forms, one for HTML4 themes and one for themes with HTML5 support. If your theme has add_theme_support('html5', array('search-form'))
line in functions.php file, then this template tag will output an HTML5 search form. Otherwise, it will output HTML4 search form.
Another way to find out what form your theme generates, is to look at the search form source code.
This is the form get_search_form() template tag will display when your theme does not have HTML5 support:
1 2 3 4 5 6 | < form role = "search" method = "get" id = "searchform" action="<?php echo home_url( '/' ); ?>"> < div >< label class = "screen-reader-text" for = "s" >Search for:</ label > < input type = "text" value = "" name = "s" id = "s" /> < input type = "submit" id = "searchsubmit" value = "Search" /> </ div > </ form > |
And this is the form it will generate for a theme with HTML5 support.
1 2 3 4 5 6 7 | < form role = "search" method = "get" class = "search-form" action="<?php echo home_url( '/' ); ?>"> < label > < span class = "screen-reader-text" >Search for:</ span > < input type = "search" class = "search-field" placeholder = "Search β¦" value = "" name = "s" title = "Search for:" /> </ label > < input type = "submit" class = "search-submit" value = "Search" /> </ form > |
For the sake of this tutorial, we will use the HTML5 search form. If your theme generates HTML4 search form, then add this line of code in your themeβs functions.php file:
1 | add_theme_support( 'html5' , array ( 'search-form' )); |
Once you have made sure that your search form is generating HTML5 form, the next step is to place the search form where you want to display it with the toggle effect.
Adding the Toggle Effect to the WordPress Search Form
First thing you will need is a search icon. The default Twenty Thirteen theme in WordPress comes with a very nice little icon, and we will be using that in our tutorial. However, feel free to create your own in Photoshop or download one from the web. Just make sure that the file is named search-icon.png.
Now you need to upload this search icon to your themeβs images folder. Connect to your website using an FTP client like Filezilla, and open your theme directory.
Now this is the final and most crucial step. You need to add this CSS to your themeβs stylesheet:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | .site-header .search-form { position : absolute ; right : 200px ; top : 200px ; } .site-header .search-field { background-color : transparent ; background-image : url (images/search- icon .png); background-position : 5px center ; background-repeat : no-repeat ; background- size : 24px 24px ; border : none ; cursor : pointer ; height : 37px ; margin : 3px 0 ; padding : 0 0 0 34px ; position : relative ; -webkit-transition: width 400 ms ease, background 400 ms ease; transition: width 400 ms ease, background 400 ms ease; width : 0 ; } .site-header .search-field:focus { background-color : #fff ; border : 2px solid #c3c0ab ; cursor : text; outline : 0 ; width : 230px ; } .search-form .search-submit { display : none ; } |
The important thing to note about this CSS, is the CSS3 transition effects which allows us to create the toggle effect with ease. Also note that you will still have to adjust the positioning of the search icon and form according to your themeβs layout.
We hope this article helped you add search toggle effect in your WordPress theme. What are your thoughts on the toggle search form? Weβre seeing more and more sites using this effect. Leave your feedback and questions in the comments below or join us in the conversation at Google+.