admin管理员组文章数量:1289866
I’m trying to auto-populate a form using url parameters (query strings). The problem is when I use the “name” query string (eg mysite/?name=xyz), it is referring to the pagename instead of the Name form field and showing a 404 error.
For eg, if I type mysite/?name=david, it shows 404 but if I type mysite/?name=about it goes to mysite/about.
To try and override this on the page where I have the form, I tried the following code:
function del_name ($qvar) {
if ( is_page( 'page-slug' ) ) {
function is_name( $var ) {
return $var !== 'name';
}
$qvar = array_filter($qvar, 'is_name', ARRAY_FILTER_USE_KEY);
}
return $qvar;
}
add_action( 'query_vars', 'del_name' );
But this isn't working. I'd really appreciate if someone can fix my code.
PS: I need the name query string for an api integration and I understand that this might break the permalink structure but I want to try it out and see if the trade off is worth it
I’m trying to auto-populate a form using url parameters (query strings). The problem is when I use the “name” query string (eg mysite/?name=xyz), it is referring to the pagename instead of the Name form field and showing a 404 error.
For eg, if I type mysite/?name=david, it shows 404 but if I type mysite/?name=about it goes to mysite/about.
To try and override this on the page where I have the form, I tried the following code:
function del_name ($qvar) {
if ( is_page( 'page-slug' ) ) {
function is_name( $var ) {
return $var !== 'name';
}
$qvar = array_filter($qvar, 'is_name', ARRAY_FILTER_USE_KEY);
}
return $qvar;
}
add_action( 'query_vars', 'del_name' );
But this isn't working. I'd really appreciate if someone can fix my code.
PS: I need the name query string for an api integration and I understand that this might break the permalink structure but I want to try it out and see if the trade off is worth it
Share Improve this question asked Jun 26, 2021 at 20:39 davidvishaldavidvishal 31 bronze badge 3 |1 Answer
Reset to default 0When handling form submissions, it would generally be advisable to POST your form to a custom AJAX handler or wp-admin/admin-post.php
form handler. If an external API integration requires you to accept the name
parameter, setting up this functionality as a REST API endpoint or controller would give you fairly complete control over the query vars.
That said, you can use the request
filter to remap incoming querystring variables. In this case, I would recommend reassigning name
's value to something else internally so as to not break anything else or require any other hacky work-arounds.
Most "pretty permalink" permastructs will map the URI /myslug
to the pagename=myslug
query var key/value. That in mind, we can re-assign name
for your page as such:
function wpse391078_remap_myslug_page_name_var( $qvs ) {
if( ! empty( $qvs['pagename'] ) && $qvs['pagename'] === 'myslug' ) {
$qvs['wpse391078_name'] = $qvs['name'];
unset( $qvs['name'] );
}
return $qvs;
}
add_filter( 'request', 'wpse391078_remap_myslug_page_name_var' );
本文标签: Use “name” query string to refer to form field instead of pagename
版权声明:本文标题:Use “name” query string to refer to form field instead of pagename 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741482033a2381224.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
mywebsite/wp-json/david/v1/webhook-thingy?name=john&phone=123&product=abc
– bosco Commented Jun 26, 2021 at 21:12