Skip to content

Instantly share code, notes, and snippets.

@patric-boehner
Last active December 1, 2017 08:25
Show Gist options
  • Save patric-boehner/07e7fcf956029268a7dff273a19a219c to your computer and use it in GitHub Desktop.
Save patric-boehner/07e7fcf956029268a7dff273a19a219c to your computer and use it in GitHub Desktop.
Custom Genesis Search Form, replace input with button
<?php
/**
* You can find the source function for the genesis search form
* within the Genesis theme under > /lib/strucutre/search.php
*
* This updated search from has replaced the <input type="button">
* with a <button> element. This makes it easier to add an icon
* in replacement of the text with the approtraite attribute roles
* that doesn't cause an html validation error.
*
* This version of the search from assumes you have declared supprot for html
* and acessability supprot.
* This update removes the filters for the button, label and search text
* as they are redundent.
*/
// Replace the default genesis search form
add_filter( 'get_search_form', 'pb_custom_search_form', 10, 4 );
function pb_custom_search_form() {
$onfocus = "if ('" . esc_js( $search_text ) . "' === this.value) {this.value = '';}";
$onblur = "if ('' === this.value) {this.value = '" . esc_js( $search_text ) . "';}";
$value_or_placeholder = ( get_search_query() == '' ) ? 'placeholder' : 'value';
$form = sprintf( '<form %s>', genesis_attr( 'search-form' ) );
$form_id = uniqid( 'searchform-', true );
$form .= sprintf(
'<meta itemprop="target" content="%s"/><label class="search-form-label screen-reader-text" for="%s">%s</label><input itemprop="query-input" type="search" name="s" id="%s" %s="%s" /><button class="search-button" type="submit">%s</button></form>',
home_url( '/?s={s}' ),
esc_attr( $form_id ),
esc_html( 'Search this website', 'genesis' ),
esc_attr( $form_id ),
$value_or_placeholder,
esc_attr( 'Search...', 'genesis' ),
esc_attr( 'Search', 'genesis' )
);
return apply_filters( 'genesis_search_form', $form );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment