Use “name” query string to refer to form field instead of pagename

Question

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.com/?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.com/?name=david, it shows 404 but if I type mysite.com/?name=about it goes to mysite.com/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

0
davidvishal 5 months 2021-06-26T15:39:57-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse