How To: Add custom settings to Drupal 7 theme

Public

Step One: Declare the new setting in the your theme's info file (yourtheme.info)

Get raw version
text
  1. settings[do_something] = 1

Step Two: Expose the setting in your theme by adding the following to your theme-settings.php file

Get raw version
php
  1. <?php
  2.  
  3. function yourtheme_form_system_theme_settings_alter(&$form, $form_state) {
  4. $form['yourtheme_settings'] = array(
  5. '#type' => 'fieldset',
  6. '#title' => t('Your Theme settings')
  7. );
  8. $form['yourtheme_settings']['do_something'] = array(
  9. '#type' => 'checkbox',
  10. '#title' => t('Do Something'),
  11. '#default_value' => theme_get_setting('do_something'),
  12. '#description' => t("Do some code processing."),
  13. );
  14. }

Step Three: Check for the new setting in your template.php file

Get raw version
php
  1. <?php
  2.  
  3. function yourtheme_preprocess_node(&$vars) {
  4.  
  5. $node = $vars['node'];
  6. // Let's do something if new setting is set
  7. $vars['something'] = theme_get_setting('do_something','yourtheme');
  8. if ($vars['something'] == '1'):
  9. // do whatever you like
  10. endif;
  11. }