Customize Search Form Text Input Field Name

有些话、适合烂在心里 提交于 2020-01-07 00:33:12

问题


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' );


来源:https://stackoverflow.com/questions/8900535/customize-search-form-text-input-field-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!