Advertisement
raselmahmud24

How to Add a Search Toggle Effect in WordPress

Jul 14th, 2014
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.85 KB | None | 0 0
  1. http://www.wpbeginner.com/wp-themes/how-to-add-a-search-toggle-effect-in-wordpress/
  2.  
  3. Displaying WordPress Search Form
  4.  
  5. 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.
  6.  
  7. Another way to find out what form your theme generates, is to look at the search form source code.
  8.  
  9. This is the form get_search_form() template tag will display when your theme does not have HTML5 support:
  10. 1   <form role="search" method="get" id="searchform" action="<?php echo home_url( '/' ); ?>">
  11. 2       <div><label class="screen-reader-text" for="s">Search for:</label>
  12. 3           <input type="text" value="" name="s" id="s" />
  13. 4           <input type="submit" id="searchsubmit" value="Search" />
  14. 5       </div>
  15. 6   </form>
  16.  
  17. And this is the form it will generate for a theme with HTML5 support.
  18. 1   <form role="search" method="get" class="search-form" action="<?php echo home_url( '/' ); ?>">
  19. 2       <label>
  20. 3           <span class="screen-reader-text">Search for:</span>
  21. 4           <input type="search" class="search-field" placeholder="Search …" value="" name="s" title="Search for:" />
  22. 5       </label>
  23. 6       <input type="submit" class="search-submit" value="Search" />
  24. 7   </form>
  25.  
  26. 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:
  27. 1   add_theme_support('html5', array('search-form'));
  28.  
  29. 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.
  30. Adding the Toggle Effect to the WordPress Search Form
  31.  
  32. 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.
  33.  
  34. 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.
  35.  
  36. Now this is the final and most crucial step. You need to add this CSS to your theme’s stylesheet:
  37. 01  .site-header .search-form {
  38. 02      position: absolute;
  39. 03      right: 200px;
  40. 04      top: 200px;
  41. 05  }
  42. 06   
  43. 07  .site-header .search-field {
  44. 08      background-color: transparent;
  45. 09      background-image: url(images/search-icon.png);
  46. 10      background-position: 5px center;
  47. 11      background-repeat: no-repeat;
  48. 12      background-size: 24px 24px;
  49. 13      border: none;
  50. 14      cursor: pointer;
  51. 15      height: 37px;
  52. 16      margin: 3px 0;
  53. 17      padding: 0 0 0 34px;
  54. 18      position: relative;
  55. 19      -webkit-transition: width 400ms ease, background 400ms ease;
  56. 20      transition:         width 400ms ease, background 400ms ease;
  57. 21      width: 0;
  58. 22  }
  59. 23   
  60. 24  .site-header .search-field:focus {
  61. 25      background-color: #fff;
  62. 26      border: 2px solid #c3c0ab;
  63. 27      cursor: text;
  64. 28      outline: 0;
  65. 29      width: 230px;
  66. 30  }
  67. 31  .search-form
  68. 32  .search-submit {
  69. 33  display:none;
  70. 34  }
  71.  
  72. 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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement