I've got a template that has a search form in several locations. Each search form is identical, containing a text input, two radio buttons, two labels associated to the radio buttons, and a submit button. This works fine for the first call to get_search_form(), but on subsequent calls clicking on the labels change the first input instead of the one for the current form.

The input are essentially like so:

<input type="radio" name="example" id="firstOption" />
<label for="firstOption">First Option</label>

<input type="radio" name="example" id="secondOption" />
<label for="secondOption">Second Option</label>

As you may have already figured out, the issue is that the labels are targeting specific IDs, and as those IDs are appearing more than once on a page, it's causing issues.

The solution would be to change it so that on every call to get_search_form(), a number gets tacked on to the IDs, like so:

<!-- first call... -->
<input type="radio" name="example" id="firstOption0" />
<label for="firstOption0">First Option</label>

<input type="radio" name="example" id="secondOption0" />
<label for="secondOption0">Second Option</label>

<!-- second call... -->
<input type="radio" name="example" id="firstOption1" />
<label for="firstOption1">First Option</label>

<input type="radio" name="example" id="secondOption1" />
<label for="secondOption1">Second Option</label>

I currently have this working via a session variable, but I don't like that the number changes as the user browses around the page. I have it set to add one to the session variable every time the form gets called, so as the user navigates around the site, the input IDs continually increase, until the session ends.

I'd much rather have them be the same number on every page. Is there another way to store a variable that persists to included templates, that doesn't persist between page navigation?

  • Is there a specific reason why you're not adding your forms directly to template with unique values?
    – N00b
    May 8, 2016 at 17:00

1 Answer 1


Just few simple ideas:

A) Here's one suggestion using the get_search_form filter with a static variable to handle the instance counting:

add_filter( 'get_search_form', function( $form )
    static $instance = 0;

    // Setup your form here with the $instance variable as needed:
    $form = '<form>...</form>';

    return $form;
} );

B) Another workaround would be to add

$instance = apply_filters( 'wpse_search_instance', 0 );

to your searchform.php file, where you add the following to your functions.php file:

add_filter( 'wpse_search_instance', function( $count )
    static $instance = 0;
    return ++$instance; 
} );

but then again this kind of instance counting could be modified through another filter callback ;-)

C) Then we could also do it like this within your searchform.php file:

do_action( 'wpse_search_instance' );

$instance = did_action( 'wpse_search_instance' );

to handle the instance counting. This assumes you don't fire up the wpse_search_counting action elsewhere.

D) We can find the following hook inside the get_search_form() function:

 do_action( 'pre_get_search_form' );

so we would only need to call:

$instance = did_action( 'pre_get_search_form' );

within the searchform.php. I think this is the easiest workaround so far.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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