wppaste
WordPress

get_search_form( array $args = array() ): void|string

Since
2.7.0, 5.2.0
Source
wp-includes/general-template.php:241
Displays search form.

Description

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, 'pre_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.

Compatibility

WordPress
since 5.2.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$argsarrayoptional
Array of display arguments.Default: array()
  • $echobooldefault: true

    Whether to echo or return the form.
  • $aria_labelstringdefault: empty

    ARIA label for the search form. Useful to distinguish multiple search forms on the same page and improve accessibility.

Return value

void|string
Void if 'echo' argument is true, search form HTML if 'echo' is false.

Performance profile

How much work a call to get_search_form() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Trivial

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
63–109

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 146.

Plugin surface
4 hooks

Third-party callbacks on 'pre_get_search_form', 'search_form_args', 'search_form_format' run inside this call, and their cost is not bounded by anything here.

Called by
1

1 place in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksdo_action()called directly

Further down the call graph this can also reach option, cache, serialize, transient and query. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 5 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost get_search_form() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
$search_form_template !== ""63–69do_action(), wp_parse_args(), apply_filters(), array_merge(), current_theme_supports(), apply_filters(), locate_template(), ob_start(), ob_get_clean(), apply_filters()
$search_form_template === "" && !$args && $format !== "html5"88–94do_action(), wp_parse_args(), apply_filters(), array_merge(), current_theme_supports(), apply_filters(), locate_template(), home_url(), esc_url(), _x(), get_search_query(), esc_attr_x(), apply_filters()
$search_form_template === "" && !$args && $format === "html5"95–101do_action(), wp_parse_args(), apply_filters(), array_merge(), current_theme_supports(), apply_filters(), locate_template(), home_url(), esc_url(), _x(), esc_attr_x(), get_search_query(), esc_attr_x(), apply_filters()
$search_form_template === "" && $args && $format !== "html5"96–102do_action(), wp_parse_args(), apply_filters(), array_merge(), current_theme_supports(), apply_filters(), locate_template(), esc_attr(), home_url(), esc_url(), _x(), get_search_query(), esc_attr_x(), apply_filters()
$search_form_template === "" && $args && $format === "html5"103–109do_action(), wp_parse_args(), apply_filters(), array_merge(), current_theme_supports(), apply_filters(), locate_template(), esc_attr(), home_url(), esc_url(), _x(), esc_attr_x(), get_search_query(), esc_attr_x(), apply_filters()

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 146 instructions, 63–109 executed per call, 7 branches. The work does not change between versions.

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 4

4 hooks fire while get_search_form() runs, in this order:

  1. do_action( pre_get_search_form )actionline 254 (+13 into the body)

    Fires before the search form is retrieved, at the start of get_search_form().

  2. apply_filters( search_form_args )filterline 286 (+45 into the body)

    Filters the array of arguments used when generating the search form.

  3. apply_filters( search_form_format )filterline 304 (+63 into the body)

    Filters the HTML format of the search form.

  4. apply_filters( get_search_form )filterline 359 (+118 into the body)

    Filters the HTML output of the search form.

Uses · 11

  • do_action()Calls the callback functions that have been added to an action hook.
  • wp_parse_args()Merges user defined arguments into defaults array.
  • apply_filters()Calls the callback functions that have been added to a filter hook.
  • current_theme_supports()Checks a theme's support for a given feature.
  • locate_template()Retrieves the name of the highest priority template file that exists.
  • esc_attr()Escaping for HTML attributes.
  • esc_url()Checks and cleans a URL.
  • home_url()Retrieves the URL for the current site where the front end is accessible.
  • _x()Retrieves translated string with gettext context.
  • esc_attr_x()Translates string with gettext context, and escapes it for safe use in an attribute.
  • get_search_query()Retrieves the contents of the search WordPress query variable.

Used by · 1

Source code

function get_search_form( $args = array() ) {	/**	 * Fires before the search form is retrieved, at the start of get_search_form().	 *	 * @since 2.7.0 as 'get_search_form' action.	 * @since 3.6.0	 * @since 5.5.0 The `$args` parameter was added.	 *	 * @link https://core.trac.wordpress.org/ticket/19321	 *	 * @param array $args The array of arguments for building the search form.	 *                    See get_search_form() for information on accepted arguments.	 */	do_action( 'pre_get_search_form', $args ); 	$echo = true; 	if ( ! is_array( $args ) ) {		/*		 * Back compat: to ensure previous uses of get_search_form() continue to		 * function as expected, we handle a value for the boolean $echo param removed		 * in 5.2.0. Then we deal with the $args array and cast its defaults.		 */		$echo = (bool) $args; 		// Set an empty array and allow default arguments to take over.		$args = array();	} 	// Defaults are to echo and to output no custom label on the form.	$defaults = array(		'echo'       => $echo,		'aria_label' => '',	); 	$args = wp_parse_args( $args, $defaults ); 	/**	 * Filters the array of arguments used when generating the search form.	 *	 * @since 5.2.0	 *	 * @param array $args The array of arguments for building the search form.	 *                    See get_search_form() for information on accepted arguments.	 */	$args = apply_filters( 'search_form_args', $args ); 	// Ensure that the filtered arguments contain all required default values.	$args = array_merge( $defaults, $args ); 	$format = current_theme_supports( 'html5', 'search-form' ) ? 'html5' : 'xhtml'; 	/**	 * Filters the HTML format of the search form.	 *	 * @since 3.6.0	 * @since 5.5.0 The `$args` parameter was added.	 *	 * @param string $format The type of markup to use in the search form.	 *                       Accepts 'html5', 'xhtml'.	 * @param array  $args   The array of arguments for building the search form.	 *                       See get_search_form() for information on accepted arguments.	 */	$format = apply_filters( 'search_form_format', $format, $args ); 	$search_form_template = locate_template( 'searchform.php' ); 	if ( '' !== $search_form_template ) {		ob_start();		require $search_form_template;		$form = ob_get_clean();	} else {		// Build a string containing an aria-label to use for the search form.		if ( $args['aria_label'] ) {			$aria_label = 'aria-label="' . esc_attr( $args['aria_label'] ) . '" ';		} else {			/*			 * If there's no custom aria-label, we can set a default here. At the			 * moment it's empty as there's uncertainty about what the default should be.			 */

Changelog

Introduced in 2.7.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

5.2.0
The $args array parameter was added in place of an $echo boolean flag.from the docblock
2.7.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 6.7.7 tag, from src/wp-includes/general-template.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.