Custom Search Form With Wordpress Search Widget | Talk In Code

来源:百度文库 编辑:神马文学网 时间:2024/04/30 04:59:48

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 { ?>
  


  
  
  
  

  
 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:

  • The action of the form should go to the home of the blog. So if your blog is located at domain.com/blog then the action should go to there.
  • The method of the form should be get, but the form will also work with a post method.
  • A text input box must be present and this must have the name of s.
  • For good search form usability you should print out the search query in the text field of the form. This can be done with the function the_search_query(), which will print out the search query, if there is one.

There are many different ways to create a search form. I found thesetwo in two different templates, created by different people.

Search




This one will print out a little bit of JavaScript in the text box.

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.