Getting subthemes to inherit custom theme settings

Getting subthemes to inherit custom theme settings

Posted 04/14/2010 - 19:44 by State68

One of the great things about the Drupal 6 theme system is that it's possible to make sub-themes of themes that already exist. This means that if you like a particular theme but want to make a few tweaks you don't have to hack around in the theme itself: just create a sub-theme, make the tweaks there and you're done.

Whenever the parent theme is updated, these updates will automatically apply to the subtheme. You can make multiple subthemes of the parent theme (useful for client meetings). And you can make subthemes of subthemes, allowing for an incremental design process.

The problem with Drupal 6 sub-theming is that not everything is inherited from the parent theme to the subtheme. These things aren't inherited:

  • The logo.png that is set for the theme. This does not include uploaded logos, since they will always be used.
  • Some of the settings set within the .info file, including the regions defined in the master theme.
  • Anything set within the "theme-settings.php" file for the advanced theme settings.
  • Anything set within the "color" directory for color.module support.

What I'm concerned with here are the advanced theme settings as defined in theme-settings.php. These are custom settings which are defined by the master theme itself. For example, 960 Robots defines the following custom settings:

But if you create a subtheme of 960 Robots, these settings won't appear in the configuration screen for your subtheme, even though the elements that are inherited from the master theme will still appear: in other words, you won't have any control over the breadcrumb or the custom Twitter feed that are defined in 960 Robots.

But this is easily fixed. Well, relatively easily. The first thing to do is to go into 960 Robots' (i.e., the master theme's) theme-settings.php file. In there you'll find a PHP function that looks a bit like this:

<?php
function ninesixtyrobots_settings($saved_settings) {
 
$defaults = array(
   
'breadcrumb_delimiter' => ' » ',
   
'use_twitter' => 1,
   
'twitter_search_term' => 'lullabot',
  );
 
$settings = array_merge($defaults, $saved_settings);
 
 
$form['breadcrumb_delimiter'] = array(
  
    ...
blah blah PHP blah blah ...

  );
 
$form['use_twitter'] = array(
   
    ...
blah blah PHP blah blah ...

  );
 
$form['twitter_search_term'] = array(
   
    ...
blah blah PHP blah blah ...

  );
 
  return
$form;
}
?>

If you don't know PHP, don't worry. Just copy the entire function and paste it into a new text file. Then change the text

<?php
function ninesixtyrobots_settings($saved_settings) {
?>

to

<?php
function whateveryoursubthemeiscalled_settings($saved_settings) {
?>

then save this file in your sub-theme's folder as template-settings.php.

But I'm afraid there's more. Even though you have now added your custom settings to your subtheme, you still need to tell your subtheme how to interpret these settings - e.g., you need to tell the subtheme to display Twitter posts in the its header only if the "Use Twitter for site slogan" box is checked.

This is where things get slightly more tricky, but it is still essentially a matter of copy and paste. Go back into your master theme, and find the template.php file. This will contain a number of PHP functions: you are only interested in the ones that contain the text,

<?php
theme_get_setting
('something_or_other')
?>

Go through each of the functions in turn (don't worry, there won't be too many) and copy any function that contains this text into a new text document, just like you did before with template-settings.php.

Once you've done this, change each function's name from

<?php
ninesixtyrobots_function_name
()
?>

to

<?php
whateveryoursubthemeiscalled_function_name
()
?>

Then save this file in your subtheme's folder as template-settings.php. Clear the theme registry and the master theme's custom settings should now show up in the subtheme's configuration screen, too. Done and done.