| 137 | | * There is a filter applied to the search form HTML in order to edit or replace |
| 138 | | * it. The filter is 'get_search_form'. |
| | 140 | * |
| | 141 | * The 'get_search_form' action is fired when the function is called, which can |
| | 142 | * be useful for outputting search-form dependent JavaScript or various formatting |
| | 143 | * that applies to the beginning of the search form. |
| | 144 | * |
| | 145 | * The 'search_form_defaults' filter is applied to the parsed argument array, |
| | 146 | * which can be useful for filtering the defaults in lieu of passing an argument |
| | 147 | * array directly to the function. |
| | 148 | * |
| | 149 | * The 'get_search_form' filter applied to the search form HTML in order to edit |
| | 150 | * or replace it. |
| | 151 | * |
| | 152 | * For consistency with other template-loading functions, the function will also |
| | 153 | * accept a string $name argument, that will be used to locate template |
| | 154 | * searchform-$name.php. |
| | 155 | * |
| | 156 | * For backward compatibility, the function will also accept a boolean $echo |
| | 157 | * argument, to determine if the search form is echoed (true) or returned (false). |
| 140 | | * This function is primarily used by themes which want to hardcode the search |
| 141 | | * form into the sidebar and also by the search widget in WordPress. |
| 142 | | * |
| 143 | | * There is also an action that is called whenever the function is run called, |
| 144 | | * 'get_search_form'. This can be useful for outputting JavaScript that the |
| 145 | | * search relies on or various formatting that applies to the beginning of the |
| 146 | | * search. To give a few examples of what it can be used for. |
| 147 | | * |
| 148 | | * @since 2.7.0 |
| 149 | | * @param boolean $echo Default to echo and not return the form. |
| | 159 | * @since 2.7.0 |
| | 160 | * @param array $args Array of arguments passed to the search form |
| 153 | | |
| 154 | | $search_form_template = locate_template('searchform.php'); |
| | 164 | |
| | 165 | // Support searchform-$name.php |
| | 166 | $form_template = false; |
| | 167 | if ( isset( $args['template'] ) { |
| | 168 | $form_template = $args['template']; |
| | 169 | } else if ( is_string( $args ) ) { |
| | 170 | $form_template = $args; |
| | 171 | } |
| | 172 | |
| | 173 | // If a search form template is found, use it |
| | 174 | $templates = array(); |
| | 175 | if ( $form_template ) { |
| | 176 | $templates[] = 'searchform-' . $form_template . '.php'; |
| | 177 | } |
| | 178 | $templates[] = 'searchform.php'; |
| | 179 | |
| | 180 | $search_form_template = locate_template( $templates ); |
| | 181 | |
| 160 | | $form = '<form role="search" method="get" id="searchform" action="' . esc_url( home_url( '/' ) ) . '" > |
| 161 | | <div><label class="screen-reader-text" for="s">' . __('Search for:') . '</label> |
| 162 | | <input type="text" value="' . get_search_query() . '" name="s" id="s" /> |
| 163 | | <input type="submit" id="searchsubmit" value="'. esc_attr__('Search') .'" /> |
| 164 | | </div> |
| 165 | | </form>'; |
| | 187 | // Define default arguments |
| | 188 | $defaults = array( |
| | 189 | 'template' => null, |
| | 190 | 'id' => 's' |
| | 191 | 'container' => 'div', |
| | 192 | 'form_id' => 'searchform', |
| | 193 | 'label_atts' => array( |
| | 194 | 'class' => 'screen-reader-text' |
| | 195 | ), |
| | 196 | 'label_text' => __( 'Search for:' ), |
| | 197 | 'input_atts' => array( |
| | 198 | 'type' => 'text', |
| | 199 | 'value' => get_search_query(), |
| | 200 | 'placeholder' => 'search' |
| | 201 | ), |
| | 202 | 'submit_atts' => array( |
| | 203 | 'type' => 'submit', |
| | 204 | 'value' => __( 'Search' ), |
| | 205 | ), |
| | 206 | 'echo' => true |
| | 207 | ); |
| | 208 | |
| | 209 | // Maintain backward compatibility with $echo = true |
| | 210 | $echo = ( is_bool( $args ) ? $args : $args['echo'] ); |
| | 211 | $args = ( is_array( $args ) ? wp_parse_args( $args, $defaults ) : $defaults ); |
| 167 | | if ( $echo ) |
| 168 | | echo apply_filters('get_search_form', $form); |
| 169 | | else |
| 170 | | return apply_filters('get_search_form', $form); |
| | 213 | // Allow search form arguments to be filtered |
| | 214 | $args = apply_filters( 'search_form_defaults', $args ); |
| | 215 | |
| | 216 | // Label attributes |
| | 217 | $label_atts = ''; |
| | 218 | foreach ( $args['label_atts'] as $att => $value ) { |
| | 219 | $label_atts .= $att . '="' . esc_attr( $value ) . '"'; |
| | 220 | } |
| | 221 | $label_atts = implode( ' ', $label_atts ); |
| | 222 | |
| | 223 | // Input attributes |
| | 224 | $input_atts = ''; |
| | 225 | foreach ( $args['input_atts'] as $att => $value ) { |
| | 226 | $input_atts .= $att . '="' . esc_attr( $value ) . '"'; |
| | 227 | } |
| | 228 | $input_atts = implode( ' ', $input_atts ); |
| | 229 | |
| | 230 | // Submit attributes |
| | 231 | $submit_atts = ''; |
| | 232 | foreach ( $args['submit_atts'] as $att => $value ) { |
| | 233 | $submit_atts .= $att . '="' . esc_attr( $value ) . '"'; |
| | 234 | } |
| | 235 | $submit_atts = implode( ' ', $submit_atts ); |
| | 236 | |
| | 237 | // Construct the form markup |
| | 238 | $form = '<form role="search" method="get" id="' . esc_attr( $args['form_id'] ) . '" action="' . esc_url( home_url( '/' ) ) . '" >'; |
| | 239 | if ( false != $args['container'] ) { |
| | 240 | $form .= '<' . esc_attr( $args['container'] ) . '>'; |
| | 241 | } |
| | 242 | $form .= '<label for="' . esc_attr( $args['id'] ) . '" ' . $label_atts . '>' . esc_attr( $args['label_text'] ) . '</label>'; |
| | 243 | $form .= '<input name="' . esc_attr( $args['id'] ) . '" id="' . esc_attr( $args['id'] ) . '" ' . $input_atts . ' />'; |
| | 244 | $form .= '<input id="' . esc_attr( $args['form_id'] ) . '" ' . $submit_atts . ' />'; |
| | 245 | if ( false != $args['container'] ) { |
| | 246 | $form .= '</' . esc_attr( $args['container'] ) . '>'; |
| | 247 | } |
| | 248 | $form .= '</form>'; |
| | 249 | |
| | 250 | // Echo or return |
| | 251 | if ( $echo ) { |
| | 252 | echo apply_filters( 'get_search_form', $form ); |
| | 253 | } else { |
| | 254 | return apply_filters( 'get_search_form', $form ); |
| | 255 | } |