The get_search_form function is used in Wordpress to extract the preset search form or the default search form.Because the official function does not have Chinese,So I wrote it briefly.

descriptionThe get_search_form function is used in wordpress to extract a custom search form or the default search form.

Whether to display a custom form or a default form,It all depends on whether you have a search.php file in your theme,

If there is this file,The file is called automatically,If not, the default search form is displayed.


  get_search_form ($echo=true)

parameter$echo Boolean, used to choose whether to display or return a variable.


ExamplesIt's not as complicated as you think.It's really that simple.

  get_search_form ();

Mention here,If you need to integrate those with Google Custom Search,

You just need to put some custom code in your search.php file.Of course you need to set the style.

Function source code

 / **
 * display search form.
 * will first attempt to locate the searchform.php file in either the child or
 * the parent, then load it. if it doesn "t exist, then the default search form
 * will be displayed. the default search form is html, which will be displayed.
 * there is a filter applied to the search form html in order to edit or replace
 * it. the filter is "get_search_form".
 * this function is primarily used by themes which want to hardcode the search
 * form into the sidebar and also by the search widget in wordpress.
 * there is also an action that is called whenever the function is run called, * "get_search_form". this can be useful for outputting javascript that the
 * search relies on or various formatting that applies to the beginning of the
 * search. to give a few examples of what it can be used for.
 * @since 2.7.0
 * @param boolean $echo default to echo and not return the form.
 * /
function get_search_form ($echo=true) {
 do_action ("get_search_form");
 $search_form_template=locate_template ("searchform.php");
 if (""!=$search_form_template) {
 require ($search_form_template);
 $form="<form role =" search "method =" get "action =" ". esc_url (home_url ("/"))." ">
 <div><label for="s">". __ (" search for:")."&/ Label>
 <input type="text" value="". Get_search_query (). "" Name="s" />
 <input type="submit" value="". Esc_attr __ ("search"). "" />
 if ($echo)
 echo apply_filters ("get_search_form", $form);
 return apply_filters ("get_search_form", $form);
  • Previous JavaScript to dynamically create div and other element instances
  • Next Js judge whether the current page is open on mobile device or PC
  • Trends