Created
December 13, 2011 01:29
-
-
Save chipbennett/1469965 to your computer and use it in GitHub Desktop.
Draft proposed update for get_search_form() - rev 2 of Trac-submitted patch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Display search form. | |
* | |
* This function is primarily used by themes that want to hardcode the search | |
* form into the sidebar and also by the search widget in WordPress. | |
* | |
* 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. | |
* | |
* The 'get_search_form' action is fired when the function is called, which can | |
* be useful for outputting search-form dependent JavaScript or various formatting | |
* that applies to the beginning of the search form. | |
* | |
* The 'search_form_defaults' filter is applied to the parsed argument array, | |
* which can be useful for filtering the defaults in lieu of passing an argument | |
* array directly to the function. | |
* | |
* The 'get_search_form' filter applied to the search form HTML in order to edit | |
* or replace it. | |
* | |
* For consistency with other template-loading functions, the function will also | |
* accept a string $name argument, that will be used to locate template | |
* searchform-$name.php. | |
* | |
* For backward compatibility, the function will also accept a boolean $echo | |
* argument, to determine if the search form is echoed (true) or returned (false). | |
* | |
* @since 2.7.0 | |
* @param array $args Array of arguments passed to the search form | |
*/ | |
function get_search_form( $args = array() ) { | |
do_action( 'get_search_form' ); | |
// Support searchform-$name.php | |
$form_template = false; | |
if ( isset( $args['template'] ) { | |
$form_template = $args['template']; | |
} else if ( is_string( $args ) ) { | |
$form_template = $args; | |
} | |
// If a search form template is found, use it | |
$templates = array(); | |
if ( $form_template ) { | |
$templates[] = 'searchform-' . $form_template . '.php'; | |
} | |
$templates[] = 'searchform.php'; | |
$search_form_template = locate_template( $templates ); | |
if ( '' != $search_form_template ) { | |
require( $search_form_template ); | |
return; | |
} | |
// Define default arguments | |
$defaults = array( | |
'template' => null, | |
'id' => 's' | |
'container' => 'div', | |
'form_id' => 'searchform', | |
'label_atts' => array( | |
'class' => 'screen-reader-text' | |
), | |
'label_text' => __( 'Search for:' ), | |
'input_atts' => array( | |
'type' => 'text', | |
'value' => get_search_query(), | |
'placeholder' => 'search' | |
), | |
'submit_atts' => array( | |
'type' => 'submit', | |
'value' => __( 'Search' ), | |
), | |
'echo' => true | |
); | |
// Maintain backward compatibility with $echo = true | |
$echo = ( is_bool( $args ) ? $args : $args['echo'] ); | |
$args = ( is_array( $args ) ? wp_parse_args( $args, $defaults ) : $defaults ); | |
// Allow search form arguments to be filtered | |
$args = apply_filters( 'search_form_defaults', $args ); | |
// Label attributes | |
$label_atts = ''; | |
foreach ( $args['label_atts'] as $att => $value ) { | |
$label_atts .= $att . '="' . esc_attr( $value ) . '"'; | |
} | |
$label_atts = implode( ' ', $label_atts ); | |
// Input attributes | |
$input_atts = ''; | |
foreach ( $args['input_atts'] as $att => $value ) { | |
$input_atts .= $att . '="' . esc_attr( $value ) . '"'; | |
} | |
$input_atts = implode( ' ', $input_atts ); | |
// Submit attributes | |
$submit_atts = ''; | |
foreach ( $args['submit_atts'] as $att => $value ) { | |
$submit_atts .= $att . '="' . esc_attr( $value ) . '"'; | |
} | |
$submit_atts = implode( ' ', $submit_atts ); | |
// Construct the form markup | |
$form = '<form role="search" method="get" id="' . esc_attr( $args['form_id'] ) . '" action="' . esc_url( home_url( '/' ) ) . '" >'; | |
if ( false != $args['container'] ) { | |
$form .= '<' . esc_attr( $args['container'] ) . '>'; | |
} | |
$form .= '<label for="' . esc_attr( $args['id'] ) . '" ' . $label_atts . '>' . esc_attr( $args['label_text'] ) . '</label>'; | |
$form .= '<input name="' . esc_attr( $args['id'] ) . '" id="' . esc_attr( $args['id'] ) . '" ' . $input_atts . ' />'; | |
$form .= '<input id="' . esc_attr( $args['form_id'] ) . '" ' . $submit_atts . ' />'; | |
if ( false != $args['container'] ) { | |
$form .= '</' . esc_attr( $args['container'] ) . '>'; | |
} | |
$form .= '</form>'; | |
// Echo or return | |
if ( $echo ) { | |
echo apply_filters( 'get_search_form', $form ); | |
} else { | |
return apply_filters( 'get_search_form', $form ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment