№ 34. Strict validating searchform (WordPress)

One of the biggest challenges one has to face when deciding to pass from the transitional xhtml validation to the strict one is certainly the recalcitrant searchform script. In this text, I present you a simple piece of code that will strict validate.

The most common searchform script

Currently, the most common searchform script is this one:

<form id="searchform" method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="text" value="Search…" name="s" id="searchbox" onfocus="if (this.value == ‘Search…’) {this.value = ”;}" onblur="if (this.value == ”) {this.value = ‘Search…’;}" />
<input type="submit" id="submitbutton" value="GO" />
</form>

I found it in the intensively used wordpress themes, and I bet it appears in many others. Its problem? It doesn’t validate.


The valid script

The simples solution is to nest the “input” tags in a “p” tag. The valid code will be:

<form id="searchform" method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<p><input type="text" value="Search…" name="s" id="searchbox" onfocus="if (this.value == ‘Search…’) {this.value = ”;}" onblur="if (this.value == ”) {this.value = ‘Search…’;}" />
<input type="submit" id="submitbutton" value="GO" /></p>
</form>

A strict validating and simplier script

If you wish to use a less fancy script, i.e. without the onfocus – onblur value, grab this piece of code:

<form method="get" id="searchform" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<p>
<input type="text" value="<?php echo wp_specialchars($s, 1); ?>" name="s" id="s" size="12" />
<input type="submit" id="ssubmit" value="SEARCH" />
</p>
</form>

Tags: , ,

Published on the 11th of January, 2009, in WordPress · Print

4 Comments on “Strict validating searchform (WordPress)”

  1. I searched for ages trying to find this answer – I’ve now bookmarked for next time I want to use it!

    Thanks

  2. Thanks a lot. When I used the WP widget search form, it didn’t validate for Strict. Don’t have enough on my site yet to warrant a search form, but your code is now in my sidebar–commented out and good to go!

  3. Nice! I have bookmarked this and will use this extensively. Is there a way to update the wordpress widget to use this code so that search is still a widget. I looked (briefly) for the code within the wordpress files but didn’t find it.

  4. Yes, there is a way, but I wouldn’t recommend it; you’d have to hack a core file, which is risky and not productive at all (the file will be overwritten with every WordPress update, so you’ll need to start over and over again). If you want to use this code and still have widgets enabled in the same sidebar, there’s a much more elegant solution: just paste the code outside of the dynamic area, above the line stating “if function exists dynamic sidebar” or below the closing “endif”.

Some Pings and Trackbacks Are Welcome

Leave a Reply

Recent comments

Colophon

This website was made by Delicia, who is deeply in love with WordPress. It is built on a 30x20px module, with Georgia and Helvetica/Arial. The design is the result of many years she spent reading Wittgenstein.