Menu icon
RedCrackle
Menu icon
Services
01Design Transformation
About UsCase StudiesBlogContact Us

<

Blog post

How to add custom theme settings for Drupal 7

Neerav Mehta

Founder & CEO

Drupal gives us the ability to change theme settings from the administrative pages. Apart from the Global settings form, each theme has its own settings page.

Navigate to "Appearance" -> "Your theme" > "Settings"

This page has the "Logo Image Settings" and "Shortcut Icon Settings".

How to add custom theme settings for Drupal 7

You can also add your own fields to this form.

Drupal 7 provides a more easier and flexible way to add your custom settings in the theme settings.

You just need to create a "theme-settings.php" file in your theme folder and write a THEMENAME_form_system_theme_settings_alter(&$form, $form_state) function.

For example, let us add breadcrumb settings to the theme settings.

function MYTHEME_form_system_theme_settings_alter(&$form, $form_state) {
  $form['breadcrumb'] = array(
    '#type' => 'fieldset',
    '#title' => t('Breadcrumb Settings'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );

  // Breadcrumb elements
  $form['breadcrumb']['breadcrumb_title'] = array(
    '#type' => 'select',
    '#title' => t('Breadcrumb title'),
    '#description' => t('Do you want to show the title of the page in the breadcrumb?'),
    '#options' => array(
      0 => t('No'),
      1 => t('Yes'),
    ),
    '#default_value' => theme_get_setting('breadcrumb_title'),
  );
}

Check http://api.drupal.org/api/drupal/7 for complete API reference

theme_get_setting('breadcrumb_title');

function MYTHEME_breadcrumb($variables) {
  $breadcrumb = $variables['breadcrumb'];
  if (!empty($breadcrumb)) {
    $output = '<'.OUTTAG.' class="element-invisible">' . t('You are here') . '<!--{cke_protected}{C}%3C!%2D%2D'.OUTTAG.'%2D%2D%3E-->';

    if (theme_get_setting('breadcrumb_title') == 1) { // show the title setting
      $breadcrumb[] = drupal_get_title();
    }

    $output .= '. implode(' » ', $breadcrumb);
    return $output;
  }
}

That is it and you are done!

Note: For setting the default value, you need the following code in your MYTHEME.info file

settings['breadcrumb_title'] = 1
  1. Create a "theme-settings.php" in your theme folder
  2. Add the following code for the breadcrumb settings
  3. You can check the form in your theme settings page. It will look like this.
  4. You can write the PHP call in any of the .php files in your theme folder.

Related Articles:

Neerav Mehta

Neerav Mehta

Founder & CEO

Neerav Mehta is the Founder & CEO of Red Crackle. With sterling qualities, Neerav’s technological acumen is firing a generation of progressive companies on the digital path. With an undergraduate degree in Electrical Engineering from India's most prestigious institution IIT Bombay and having spent seven years developing and contributing to the launch of AMD's innovative line of computer products, Neerav founded Red Crackle where he is lauded for his dynamic and innovative genius.

View all posts

>

Read Next

10 Tips For Entrepreneurs In 2015

10 Tips For Entrepreneurs In 2015

Learn more

10 Ways To Increase Productivity At Work

10 Ways To Increase Productivity At Work

Learn more

30 best WordPress widgets for your site

30 best WordPress widgets for your site

Learn more

Let’s get you started!

Contact Us

>

RedCrackle

Explore

About Us

Services

Contact Us

Our address

5346 Gerine Blossom Dr,

San Jose, CA 95123

USA

Socials

Twitter
LinkedIn

© 2023 RedCrackle. All rights reserved.