Search form

Customize the Drupal Search Form

Here's an easy one: you want to customize the Drupal search form provided by the Search module in your Drupal 7 site. Fortunately, Drupal provides an easy way to do this, via hook_form_alter(). Put something like this in your theme's template.php:

 * Alter the search block form.
function YOUR-THEME-NAME_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'search_block_form') { 
    $form['search_block_form']['#title'] = t('Search');
    $form['search_block_form']['#title_display'] = 'invisible';
    $form['search_block_form']['#size'] = 30;
    $form['search_block_form']['#attributes']['placeholder'] = t('Search');
    $form['actions']['submit']['#value'] = t('Go');

This defines the appearance of the search block form, which you can then place anywhere on your web site via the Blocks UI. Alternatively, if you just want to display the search form in another template, like region--header.tpl.php or page.tpl.php, call it by render():

<?php print render(drupal_get_form('search_block_form')); ?>

Depending on your PHP version, and Strict setting, you may get a warning like "Only variables should be passed by reference in include()". This happens because render() expects a reference while drupal_get_form() returns the form array. To fix it, assign drupal_get_form() to a variable and then pass the variable to render():

    $form_array = drupal_get_form('search_block_form');
    print render($form_array);