1

Not sure if I titled it correctly but here is what I'm trying to do.

What it's doing: User comes to my website with a campaign query string appended to the url like www.mywebsite.com?name=myName. I wrote a script to make sure that QS follows them if they click around the website but when they use the search function the url strips the QS and ends up looking something like this: www.mywebsite.com?s=searchTerm.

What I want: Keep QS even when searched so that it looks like something like this: www.mywebsite.com?s=searchTerm&name=myName

  • 1
    How are you outputting your search form? Is it using the searchform.php theme template or some other method? – Nick Young Jul 1 '18 at 5:22
  • @NickYoung Using searchform.php file – feedmeimhungry Jul 3 '18 at 3:45
0

It's hard to say how to do it exactly in your case, because we don't know how your form is generated... But in general you have to add a hidden input to that form.

So if you use searchform.php in your theme, then add something like this to it:

<input type="hidden" name="name" value="<?php echo esc_attr( $_GET['name'] ); ?>" />

If there is no searchform.php template in your theme and you're using get_search_form function, then you'll have to use a filter like this:

function add_custom_name_to_search_form( $form ) {
    $field = '<input type="hidden" name="name" value="'. esc_attr( $_GET['name'] ) .'" />'
    return str_replace( '</form>', $field . '</form>', $form );
}
add_filter( 'get_search_form', 'add_custom_name_to_search_form' );

But... My approach would be a little bit different... If user comes with some param in URL and I want to use this param during his visit on page, I store that value in a cookie...

function process_name_cookie() {
    if ( isset($_GET['name']) ) {
        setcookie( 'myname', $_GET['name'], 0, '/' );
    }
}
add_action( 'wp', 'process_name_cookie' );

And then you can use that cookie, so you don't have to modify any link or forms.

  • Going off the searchform.php example how would you handle multiple params? e.g. www.mywebsite.com/?name=myName&gender=myGender – feedmeimhungry Jul 3 '18 at 3:56
  • @feedmeimhungry just add multiple hidden inputs - one for param. – Krzysiek Dróżdż Jul 3 '18 at 5:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.