WordPress: W3C Duplicate ID error beheben

Manchmal kann es sein, dass man auf einer WordPress-Seite mehrere Suchformulare einbinden muss oder möchte. Das hat zur Folge, dass der W3C-Validator meckert, weil diese Formulare standardmäßig dieselbe ID haben. Und der Sinn einer ID ist ja, dass sie irgendwas eindeutig kennzeichnet.

Mit diesem Workaround kann man sich behelfen. Auch wenn das sicher noch nicht die elgeanteste Möglichkeit ist – sie scheint ganz brauchbar zu funktionieren:

If there’s a searchform.php inside your theme folder, WordPress uses this file instead of the WP search form.

  1. Check if there’s a searchform.php inside your theme folder
  2. If not, create a file named searchform.php inside your theme folder
  3. Now you can customize your search form as you like. And you can give the form an unique ID using something like id=“<?php echo uniqid() ;?>“
  4. If there’s already a searchform.php inside your theme folder, you can add this to the <form> tag as well.
  5. Get yourself a cup of coffee and enjoy W3C checker turning green 🙂

The searchform.php could look like this:


<form role="search" method="get" class="searchform group" id="<?php echo uniqid() ;?>" action="<?php echo home_url( '/' ); ?>">
 <label>
 <span class="offscreen"><?php echo _x( 'Suche nach:', 'label' ) ?></span>
 <input type="search" class="search-field"
 placeholder="<?php echo esc_attr_x( 'Suchbegriff', 'placeholder' ) ?>"
 value="<?php echo get_search_query() ?>" name="s"
 title="<?php echo esc_attr_x( 'Suche nach:	', 'label' ) ?>" />
 </label>
 <input type="image" alt="Submit search query"
 src="<?php echo get_template_directory_uri(); ?>/images/search.png">
</form>