D7: Adding custom settings / select options list to your theme

Par jbfelix | 08 Mars 2018

How to add this select list options to your D7 theme and display the selected option in PHP :

 

Result:

top --> Menu position = 0

right -->  Menu position = 1

 


  • Create a file called theme-settings.php in YOURTHEME's root directory

  • Add the select list in theme-settings.php 
<?php
function YOURTHEME_form_system_theme_settings_alter(&$form, $form_state) {
$form['YOURTHEME_menuposition'] = array(
'#type' => 'select',
'#title' => t('Menu Position'),
'#options' => array(
  0 => t('top'),
  1 => t('right'),
),
'#default_value' => theme_get_setting('YOURTHEME_menuposition'),
'#description' => t('Choose menu position.'),
);
}
?>
  • Flush cache
drush cc all
  • OPTION: define this setting in YOURTHEME.info file 
;;;;;;;;;;;;;;;;;;;;;
;; Settings
;;;;;;;;;;;;;;;;;;;;;
settings[YOURTHEME_menuposition] = 0
  • Display or use the result in a template file 
<?php print theme_get_setting('YOURTHEME_menuposition'); ?>
  • EXAMPLE: add a class "menutop" or "menuright" to the <body> tag.
    • edit template.php and add this function
<?php

/**
 * @file
 * template.php
 */
 function YOURTHEME_preprocess_html(&$variables) {
  switch (theme_get_setting('YOURTHEME_menuposition')) {
    case '0':
      $variables['classes_array'][] = 'menutop';
      break;

    case '1':
      $variables['classes_array'][] = 'menuright';
      break;
  }
}
  • Flush cache 
drush cc all
  • Smile !

WTFPL