Home  >  Article  >  Backend Development  >  Analysis of the PHP function used to obtain the search form in WordPress, wordpressphp_PHP tutorial

Analysis of the PHP function used to obtain the search form in WordPress, wordpressphp_PHP tutorial

WBOY
WBOYOriginal
2016-07-12 09:01:41774browse

The PHP function used to get the search form in WordPress uses parsing. The wordpressphp

get_search_form function is used in WordPress to extract the preset search form or the default search form. Because this official function is not available in Chinese, I simply wrote it.

Description
The get_search_form function is used to extract a custom search form or the default search form in WordPress.
Displaying a custom form or displaying the default form completely depends on whether you have a search.php file in your theme,
If the file exists, the file is automatically called. If not, the default search form is displayed.

Use

<&#63;php
  get_search_form($echo = true) 
&#63;>

Parameters
$echo Boolean type, used to choose whether to display or return variables.
Default value: true

Example
It’s not as complicated as you think, it’s actually that simple.

<&#63;php
  get_search_form(); 
&#63;>

Mention here, if you need to integrate Google Custom Search,
You just need to put the customized part of the code in your search.php file. Of course, you need to set the style.

Function source code

<&#63;php
 /**
 * 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);
 return;
 }
 
 $form = '<form role="search" method="get" id="searchform" action="' . esc_url( home_url( '/' ) ) . '" >
 <div><label class="screen-reader-text" for="s">' . __('Search for:') . '</label>
 <input type="text" value="' . get_search_query() . '" name="s" id="s" />
 <input type="submit" id="searchsubmit" value="'. esc_attr__('Search') .'" />
 </div>
 </form>';
 
 if ( $echo )
 echo apply_filters('get_search_form', $form);
 else
 return apply_filters('get_search_form', $form);
}
&#63;>

Articles you may be interested in:

  • Analysis of related functions used for title display in WordPress development
  • Analysis of related PHP functions used for debugging thumbnails in WordPress
  • Configure to solve the problem of WordPress paths not automatically adding slashes in Nginx server
  • Use wp_count_posts function in WordPress to count the number of articles
  • Detailed explanation of calling comment templates and looping output comments in WordPress PHP functions
  • Detailed explanation of the use of classification function wp_list_categories in WordPress
  • WordPress restricts non-admin users to only comment once after an article
  • Detailed explanation of creating and adding filters in WordPress Related PHP functions
  • Detailed explanation of the usage of wp_title() function in WordPress development

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1088777.htmlTechArticleThe PHP function used to get the search form in WordPress uses parsing. The wordpressphp get_search_form function is used in WordPress to extract pre- Set search form or default search form. ...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn