Customize Search Form Text Input Field Name

2019-09-02 07:56发布

问题:

I want to change the default name 's' to something else for the search widget of wordpress. So, basically I will need to change the widget template and the handler that reads the GET input. Can you please help me finding those two place quickly please? Thanks.

回答1:

This is the solution I've used, where 'q' is the name of the search field:

function my_query_vars( $public_query_vars ) {
    if ( isset( $_GET['q'] ) && ! empty( $_GET['q'] ) ) {
        $_GET['s'] = $_GET['q'];
    }

    return $public_query_vars;
}

add_filter( 'query_vars', 'my_query_vars' );


回答2:

Searched on google "get search form wordpress"

taken from here: http://codex.wordpress.org/Function_Reference/get_search_form

Drop this function in your theme's functions.php

function my_search_form( $form ) {

        $form = '<form role="search" method="get" id="searchform" action="' . home_url( '/' ) . '" >
        <div><label class="screen-reader-text" for="s">' . __('Search for:') . '</label>
        <input type="text" value="' . get_search_query() . '" name="s" id="s" />
        <input type="submit" id="searchsubmit" value="'. esc_attr__('Search') .'" />
        </div>
        </form>';

        return $form;
    }

    add_filter( 'get_search_form', 'my_search_form' );