The Official UpThemes Blog
The best place to find WordPress tutorials, theme teasers, company news and much more.

Exploring the Theme Customizer Code in WordPress 3.4

Being a theme developer means staying up-to-date on the latest WordPress features and changes to ensure our themes remain compatible and enjoy enhanced features that live in harmony with core WordPress code and utilities. The new Theme Customizer is certainly one of the most exciting features for theme developers in quite some time, allowing us to add our theme customization controls into a live customization panel that makes it super easy to view and update your theme’s design quickly and easily.

I want to give big props to Otto for providing a thorough explanation of the implementation details for developers (which is currently subject to change, since it is beta code, after all). It’s a helpful post so I’d recommend reading that first and coming back here to dive a little deeper into the code. I’m going to walk you through a quick overview of what the Theme Customizer code is actually doing and how it can be extended in your own themes to provide custom controls.

We’re currently working on implementing controls that already exist in the UpThemes Framework as a part of the new Theme Customizer so all UpThemes customers can have access to these features shortly after WordPress 3.4 is released. We just recently implemented support for the Settings API and now, with the new Theme Customizer, we’re a few short steps away from being able to quickly add support for that.

The PHP Stuffs

The new Theme Customizer code actually lives in a few files inside your wp-includes folder that we’re going to look at today:

First, we’re going to take a look at class-wp-customize.php. This is where the Theme Customizer class is defined and does the bulk of the work. One area I want to point out is where the Theme Customizer checks to see that it is active:

This function essentially tells the customizer to load on the front-end by checking for a query string variable of “customize” that is currently set to “on” and then the magic happens.

The next part to take note of is the start_previewing_theme() function. This function sits right below the setup_theme() function:

As you can see inside this function, there are a number of filters added as well as an action that is fired to officially start previewing the theme.

There are a few functions here that are important to note for adding settings, sections, and controls. I’ve dropped them in below for reference, just so you can see exactly how WordPress is building out the theme customization panel. This is very similar to how the Settings API works.

Putting it all together

Now, I realize that what I’ve shared above isn’t all that helpful until you see some code that you can actually use as an example for registering your own settings. One of the largest functions in the entire file is the one that registers your default settings, like so:

If you pay close attention to this section, you can see how each and every default preview setting is registered. Each “option” is actually defined by a pair of definitions, with an add_setting and add_control function being fired for each one:

The important pieces of these settings to note are the ‘type’ and the ‘capability’ bits. These pieces allow you to define what control type it is and which users can access it.

This is where we can jump over to the class-wp-customize-control.php file and take a peek at the different types of default controls we have access to:

  • text
  • checkbox
  • radio
  • select
  • dropdown-pages

And of course, you can add your own controls, as shown by the custom ‘color,’ ‘upload,’ and ‘header-image’ types, defined below the function I just shared with you:

The Difference Between Using a Default and Custom Control

If you compare the two control declarations below, you’ll see one of them uses a default setting control of 'type' => 'checkbox' and the other actually instantiates a new object for WP_Customize_Color_Control(). Notice the array of arguments passed in are quite a bit different.



Make sure to browse the files I linked earlier in the post and learn about how the Theme Customizer works and it’ll make life much easier when implementing your custom options. Also note that these functions will all change at some point, so please reference the current version of the code in trunk (which is what I linked above). We are certainly excited about the future of the UpThemes Framework with the new Theme Customizer and we’ll definitely be posting our progress in implementing these features in the near future so make sure to subscribe to the RSS feed to stay updated.

About the Author

Chris Wallace

Co-founder of UpThemes, Chris designs and develops many of the WordPress themes that you'll find here on this site. He is a cancer survivor, Christian, family man, and Starbucks lover.

WordPress Tips & Tricks Newsletter

Sign up for our 'once-in-a-while' WordPress tips and tricks newsletter for all skill levels. We provide excellent tips for marketing, SEO, and customization for your WordPress site.

2 thoughts on “Exploring the Theme Customizer Code in WordPress 3.4

  1. devinsays said:

    I’ll be curious how you implement it.  I’m also trying to decide how to best incorporate it into the Options Framework.
     
    Also, FYI, class-wp-customize.php is a broken link.

  2. FurqanKhanzada said:

    Nice article.
    is it possible category dropdown  like pages ( type => dropdown-pages )  ??? 

Leave a Reply

Basic HTML allowed.