How to Add Theme Settings for Drupal 7

You know that list of checkboxes on the theme settings page that let you turn on and off parts of the theme like the logo or slogan? Well, you can add your own options to that list really easily in Drupal 7. In D6, this was kind of a pain, because you had to write all sorts of functions to save and load your settings to the database and handle everything properly. In D7, that’s all done through the Form API, so the heavy lifting is done for you. All you need to do is tell it to add some form fields, and what the new setting is called!

You’ll need to create a theme-settings.php file in your theme, and add this code to it:

<?php
function themename_form_system_theme_settings_alter(&$form, &$form_state) {
  $form['theme_settings']['your_option'] = array(
    '#type' => 'checkbox',
    '#title' => t('Your Option'),
    '#default_value' => theme_get_setting('your_option'),
  );
}
  • ['theme_settings'] is the existing fieldset to add your option to. You can leave it off, but by pointing it to the theme_settings group, it’ll be added to the existing list of checkbox display options for your theme. Handy!
  • ['your_option'] is the name of your new option.
  • #type tells Drupal what kind of form element to create.
  • #title is the title (or in this case, label) text for the form element.
  • #default_value tells Drupal where to find the initial setting for the form element.

We set #default_value to theme_get_setting('your_option'), which tells Drupal to look for this setting in the database. But here’s the brilliant part — if it can’t find that setting in the database, it will check in your theme’s .info file! So add this line:

settings[your_option] = 1

Now your theme will use the default setting unless the admin overrides it on the theme settings page.

You’ll want to actually do something with this setting, so here’s the PHP call to load it.

theme_get_setting('your_option');

You can call this in from any of your .tpl.php files. For a simple checkbox option like this, you can build an if() statement around it, like so:

<?php if (theme_get_setting('your_option')): ?>
  <!-- Your code here! -->
<?php endif; ?>

In this example, we added a checkbox, but you can add just about any form elements you can think of, including radio buttons and text input fields. What sort of options will you add to your theme?

References

Note: This was originally posted on my work blog, and I’m re-posting it here for archival purposes.

Dojo and WordPress 2.7

I’ve had a few inquiries about whether Dojo works under WordPress 2.7, and the answer is yes! You can drop it in, and it’ll work just fine. However, there are some new features to allow comment threading and paging which don’t work yet. It’s not broken, it just uses the old non-threaded, non-paged comment style. I’m in the process of adding these and a few other new features, and I should have a new version of Dojo ready to download within a week. Thanks for your interest, and check back soon!

Update: And, it’s done! Dojo 5.1.2 is released with full WordPress 2.7 support.

Dojo is Coming Together Nicely

I haven’t had a chance to have another coding marathon for Dojo yet, but I’ve been able to spend an hour or so here and there, and I’m really pleased with the way it’s coming together.

First and foremost, I’m proud of the code. I haven’t even gotten to the CSS yet, it’s just markup right now – but it’s some of the best markup I’ve ever written for a personal site. All the techniques and little touches I’ve picked up working on Sara Ryan‘s site and in the last few years at Pop Art are really paying off.

Even better than that, though, are the WordPress theme techniques I’m using. The last few versions of WordPress have added a lot of really nice bits for themers, like support for dynamic sidebars and custom widgets. Right now, I’ve got support built into the theme for several key plugins that I really like, and I’ve got three separate areas to customize with widgets.

My favorite bit, though, is the custom admin panel. I found a nice little tutorial explaining how to add one, and it’s pretty simple. As a result, I’ve added the ability to customize a couple of areas of the theme that normally aren’t editable without touching source files.

This weekend, I’m going to add some templates for a links page and an archives page, and maybe get started on some design. Miles volunteered to be a guinea pig alpha tester, so I want to get it pulled together sooner rather than later. All in all, I’m happy enough with this theme that I’m probably going to end up using a slightly modified version here on my own site.

I’m Working on Something Special

Most coders and programmers are familiar with the concept of “flow” or “getting in the zone.” In a nutshell, it’s a mental state you get in when you’re working on something where it almost becomes effortless. This state can be difficult to achieve, and usually takes the right combination of energy level, motivation, and little or no distractions. Once you get there, however, you’ll do your best work. When I get in the zone, I lose track of time and forget to eat until Annie notices that I haven’t left the computer all day.

The reason I bring this up is that I haven’t gotten in the zone for a personal project in well over a year. I hadn’t really noticed until this last weekend, when I spent over twenty hours coding. Annie and Zoe went to Centralia for the day to go shopping with her mom, and I stayed home. I went into the computer room at 10am, and I stayed in there coding until 4am. Even then, I was still deep in the zone and I didn’t want to stop, but I could tell I was tired because it was taking me too long to do everything. I slept for about four hours, and woke up eager to get back to it. I was supposed to be helping out with Zoe, but every chance I got that day, I sneaked back into the computer room to put a little more time it.

(As an aside, I would like to mention that my lovely wife is awesome. I felt terrible that I wasn’t helping out as much as I should, especially given that Zoe is both teething and recovering from an ear infection at the moment, so she’s really clingy. Even so, Annie knew that I don’t get into this headspace very often, and was really understanding about it.)

It was effortless. My mind was completely absorbed in the code I was writing, and even now, I’m completely jazzed about going home to work on it some more.

What I’m working on is a wordpress theme, which I’m calling Dojo. It’s an attempt to create a stable code platform that I can put on all the wordpress sites I maintain, and completely customize with just a CSS file.

Right now, it’s not much to look at. I intentionally stripped all images and CSS so that I could focus on the markup to make sure it’s a solid base to work from. Still, if you’re interested, you can check it out on the dev site.

In the coming weeks, I’ll be posting more about this, including some challenges I found with the markup, the admin module, and the design process.

Rustbucket

Blog v4

I posted a new skin here called Rustbucket. You can find it on the options page. I’m pretty fond of it, so I set it as the new default skin.

And while I’m talking about the site, let me take a moment to ask you all what you think of Rusted. What do you like and what do you dislike? I’m not really looking to change anything, but it would be nice to get some of your opinions.