How to add placeholder in Drupal search form

Drupal 7 gave us alters in the theme layer, so adding attributes to a form item is rather trivial in our themes, no need to build a separate “form alter” mini-module.
This code goes in your template.php file, note this is for the search block only, the advanced search form has a different form_id:

<?php// Add some cool text to the search block form
function THEMENAME_form_alter(&$form, &$form_state, $form_id) {
if ($form_id =
= ‘search_block_form’) {
// HTML5 placeholder attribute
$form[‘search_block_form’][‘#attributes’][‘placeholder’] = t(‘enter to search’);
}
}
?>

“enter to search” replace the text with your text.

Related Post


Latest Post


Recent Posts Widget

Make sure to never miss a thing...

Get the latest news from the creative industry along with other creative goodies, conveniently delivered to social media.