admin管理员组文章数量:1130349
I was doing a search form when I realized that if I change the value of the name attribute to something other than "s", the search.php page will not be displayed.
How can I change and still display the search.php page?
NOTE: I used the translator if there are any errors. I ask you to please edit for me and sorry for my ignorance
I was doing a search form when I realized that if I change the value of the name attribute to something other than "s", the search.php page will not be displayed.
How can I change and still display the search.php page?
NOTE: I used the translator if there are any errors. I ask you to please edit for me and sorry for my ignorance
Share Improve this question edited Jan 2, 2019 at 15:53 RiddleMeThis 3,8078 gold badges22 silver badges30 bronze badges asked Jan 2, 2019 at 11:57 ProgramadorWProgramadorW 1 1 |1 Answer
Reset to default 1The name attribute of the input element becomes the query parameter in the URL when the form is submitted. So for an input with the name s the URL will look like:
http://example/?s=my+search+query
WordPress is built so that the s parameter indicates a search, so it then queries based on that value and loads the search.php template.
There isn't a way to change the name of the query parameter used for search per se, but you could use an early hook to manually set the value of $_GET['s'] to the value of your own parameter name.
For example, if you wanted to use q as the input name:
function wpse_324429_search_parameter() {
if ( isset( $_GET['q'] ) ) {
$_GET['s'] = $_GET['q'];
}
}
add_action( 'init', 'wpse_324429_search_parameter' );
Manually setting a value in $_GET feels wrong to me, but I can't see why it wouldn't work.
I was doing a search form when I realized that if I change the value of the name attribute to something other than "s", the search.php page will not be displayed.
How can I change and still display the search.php page?
NOTE: I used the translator if there are any errors. I ask you to please edit for me and sorry for my ignorance
I was doing a search form when I realized that if I change the value of the name attribute to something other than "s", the search.php page will not be displayed.
How can I change and still display the search.php page?
NOTE: I used the translator if there are any errors. I ask you to please edit for me and sorry for my ignorance
Share Improve this question edited Jan 2, 2019 at 15:53 RiddleMeThis 3,8078 gold badges22 silver badges30 bronze badges asked Jan 2, 2019 at 11:57 ProgramadorWProgramadorW 1 1-
1
Welcome to the site. The
sinput name in the form, will become a GET parameter, if the form's method is set to GET and WordPress will treat it as a search query. I wonder why you need to change it? – birgire Commented Jan 2, 2019 at 12:36
1 Answer
Reset to default 1The name attribute of the input element becomes the query parameter in the URL when the form is submitted. So for an input with the name s the URL will look like:
http://example/?s=my+search+query
WordPress is built so that the s parameter indicates a search, so it then queries based on that value and loads the search.php template.
There isn't a way to change the name of the query parameter used for search per se, but you could use an early hook to manually set the value of $_GET['s'] to the value of your own parameter name.
For example, if you wanted to use q as the input name:
function wpse_324429_search_parameter() {
if ( isset( $_GET['q'] ) ) {
$_GET['s'] = $_GET['q'];
}
}
add_action( 'init', 'wpse_324429_search_parameter' );
Manually setting a value in $_GET feels wrong to me, but I can't see why it wouldn't work.
本文标签: Why should we use the name attribute with the value quotsquot in the creation of the search form
版权声明:本文标题:Why should we use the name attribute with the value "s" in the creation of the search form? 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749050407a2308526.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


sinput name in the form, will become a GET parameter, if the form's method is set to GET and WordPress will treat it as a search query. I wonder why you need to change it? – birgire Commented Jan 2, 2019 at 12:36