Custom search form

Community Forums Forums General Discussion Custom search form

This topic is: not resolved

This topic contains 2 replies, has 2 voices, and was last updated by  webmaster@uthsc.edu 10 months ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #69893

    webmaster@uthsc.edu
    Participant
    Post count: 13

    Using: Genesis with Generate Child Theme
    Site: On local server

    Hi, I’m trying to do something really simple but I’m pretty sure I’m
    doing it completely wrong. Basically, all I really need to do is add
    some custom html. I’m calling the form in my theme with <?php get_search_form(); ?>

    To modify the search form, I’ve added the code below to functions.php.

    As you can see, I’ve actually unset the genesis_search_form and
    basically just copied the genesis_search_form code in to my own
    function and made some changes (This is totally hacky).

    I think I should be creating a custom search form and calling it in my theme
    differently but I’m not sure how. I’ve done this sort of thing in a
    couple of other cases too so I think if I can do this correctly I will be on a roll. Thanks!

    //Custom search form
    remove_filter( 'get_search_form', 'genesis_search_form' );
    add_filter( 'get_search_form', 'uthsc_search_form' );
    function uthsc_search_form() {
    	$search_text = get_search_query() ? apply_filters( 'the_search_query', get_search_query() ) : apply_filters( 'genesis_search_text', __( 'Search ', 'genesis' ) . '…' );
    
    	$button_text = apply_filters( 'genesis_search_button_text', esc_attr__( '', 'genesis' ) );
    
    	$onfocus = "if ('" . esc_js( $search_text ) . "' === this.value) {this.value = '';}";
    	$onblur  = "if ('' === this.value) {this.value = '" . esc_js( $search_text ) . "';}";
    
    	//* Empty label, by default. Filterable.
    	$label = apply_filters( 'genesis_search_form_label', '' );
    
    	if ( genesis_html5() )
    		$form = sprintf( '
    		<form method="get" class="search-form" action="%s" role="search">%s
    			<input type="search" name="s" placeholder="%s" />
    			<input type="submit" value="%s" />
    		</form>', 
    		home_url( '/' ), esc_html( $label ), esc_attr( $search_text ), esc_attr( $button_text ) );
    	else
    		$form = sprintf( '
    		<form method="get" class="searchform search-form" action="%s" role="search" >%s
    			<input type="text" value="%s" name="s" class="s search-input" onfocus="%s" onblur="%s" />
    			<input type="submit" class="uthsc-header-search searchsubmit search-submit" value="" />
    		</form>', 
    		home_url( '/' ), esc_html( $label ), esc_attr( $search_text ), esc_attr( $onfocus ), esc_attr( $onblur ), esc_attr( $button_text ) );
    
    	return apply_filters( 'genesis_search_form', $form, $search_text, $button_text, $label );
    }
    http://localhost
    #73703

    Robert Neu
    Participant
    Post count: 90

    It looks like all you’re doing is changing the class of the submit button, but I could be missing something.

    If that’s all you’re doing, you probably can achieve what you’re trying to do another way, such as targeting the selector surrounding the form itself.

    If you’re set on doing it this way but without unhooking and rehooking the entire search form, you would want to write a filter targeting genesis_search_form and return a new version of $form.

    It would be easier to give you a good course of action if we could see the site you’re working on…


    Co-Founder of Audit WP, a WordPress SEO audit service and Flagship, a new WordPress theme company launching soon!

    #73943

    webmaster@uthsc.edu
    Participant
    Post count: 13

    Hey, Thanks for the response.

    The site is under development with a private IP so I can’t share it. However, I’m the answer I’m looking for shouldn’t be site specific. I was just having some trouble finding documentation about the proper way to create custom search forms. I’m sure this is something genesis makes easy. Any tips or links?

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.