When adding a search form to your Wordpress blog you will want tohave control over what sort of form is displayed. It is possible tooverride the search form created by the widget function without havingto go into the /wp-includes/widget.php file and editing the wp_widget_search function. Here is the function that is present in Wordpress 2.6.
function wp_widget_search($args) {
extract($args);
$searchform_template = get_template_directory() . '/searchform.php';
echo $before_widget;
// Use current theme search form if it exists
if ( file_exists($searchform_template) ) {
include_once($searchform_template);
} else { ?>
<form id="searchform" method="get" action="<?php bloginfo('url'); ?>/"><div>
<label class="hidden" for="s"><?php _e('Search for:'); ?></label>
<input type="text" name="s" id="s" size="15" value="<?php the_search_query(); ?>" />
<input type="submit" value="<?php echo attribute_escape(__('Search')); ?>" />
</div></form>
<?php }
echo $after_widget;
}
Notice that the first thing the function tries to do is load a template file called searchform.php,and if this doesn’t exist the function prints out a standard searchform. That is it basically. If you want to override the search formcreated by this function then just create a file called searchform.php in your template directory and create a search form inside this file. The form must have the following:
There are many different ways to create a search form. I found thesetwo in two different templates, created by different people.
<h2>Search</h2>
<form method="get" id="searchform" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<div class="searchbox">
<label for="s">Find:</label>
<input type="text" value="<?php echo wp_specialchars($s, 1); ?>" name="s" id="s" size="14" />
<input type="submit" id="searchsubmit" value="Search" />
</div>
</form>
This one will print out a little bit of JavaScript in the text box.
<div id="search">
<form method="get" id="searchform" action="<?php bloginfo('home'); ?>/">
<div><input type="text" value="search..." name="s" id="s"onfocus="if (this.value == 'search...') {this.value = '';}" onblur="if(this.value == '') {this.value = 'search...';}" />
</div>
</form>
</div>
They both seem to work quite well, but I would advise that anysearch form you create should be copied from the original and modified.This is usually the best practice with Wordpress themes as theWordpress documentation can be a little thin on the ground (or hidden)and their developers tend to use the best functions available.
聯(lián)系客服