2 min read

Drupal 7: custom theme settings form

Creating your own theme is an integral part of developing almost any website, and often you need to extend the default theme settings provided by the Drupal core. To do this, create a theme-settings.php file in your theme folder and override the hook hook_form_system_theme_settings_alter.

For example, to save a phone number that will be displayed in the website header:

function THEMENAME_form_system_theme_settings_alter(&$form, &$form_state) {
  $form['THEMENAME_phone'] = array(
    '#type' => 'textfield',
    '#title' => t('Phone'),
    '#default_value' => theme_get_setting('THEMENAME_phone'),
    '#description' => t('Enter company phone'),
  );
}

It is important that the variable name starts with the theme name to avoid namespace conflicts. The form values are saved automatically.

To access this variable in a template, you can use preprocess functions in the theme’s template.php file. Retrieve the value using the theme_get_setting function. Example of adding the variable to the page.tpl.php template:

function THEMENAME_preprocess_page(&$variables) {
  $variables['phone'] = theme_get_setting('THEMENAME_phone');
}

More detailed information about creating custom theme settings forms is available on drupal.org.