Skip to main content

Ivan Teoh

Something personal yet public

WordPress Theme Development 8 - Search Form

There is a new template tag for search form, get_search_form();, in WordPress version 2.7. However, the template tag will look for search form, searchform.php, first. If the file does exists, it will use that file. If not, it will generate the HTML. We decided to use this new template tag. Hence, we will replace the code as following in sidebar file, sidebar.php.

58-sidebar-before.php (Source)

    <label for="s"><?php _e('Search'); ?></label>
    <form id="searchform" method="get" action="<?php bloginfo('home'); ?>">
        <input type="text" name="s" id="s" size="15" /><br />
        <input type="submit" value="<?php _e('Search'); ?>" />

Now, new search code in sidebar file, sidebar.php, will look as following.

58-sidebar-after.php (Source)

    <?php get_search_form(); ?>

After using the new search template tag in sidebar file, sidebar.php, we move the old search code to a search form file called searchform.php. There is a example of search form file as following.

58-searchform.php (Source)

 * @package WordPress
 * @subpackage Your_Theme
<form id="searchform" method="get" action="<?php bloginfo('home'); ?>">
    <label for="s"><?php _e('Search'); ?></label><br />
    <input id="s" type="text" name="s" value="<?php the_search_query(); ?>" />
    <input id="searchsubmit" type="submit" value="<?php _e('Search'); ?>" />


Comments powered by Disqus