WordPress Customizer API Overview
The WordPress Customizer (Theme Customization API) provides a framework for live-previewing any changes to WordPress settings. Introduced in WordPress 3.4, it allows developers to customize and preview changes to the site before publishing them.
Architecture
The Customizer consists of four primary object types that work together in a hierarchical structure:
Panels (optional containers)
└── Sections (containers for controls)
└── Controls (UI elements)
└── Settings (data storage)
Core Classes
| Class | Purpose | Since |
|---|---|---|
WP_Customize_Manager |
Central orchestrator, factory for all components | 3.4 |
WP_Customize_Setting |
Handles data storage, sanitization, and preview | 3.4 |
WP_Customize_Section |
UI container for controls | 3.4 |
WP_Customize_Panel |
UI container for sections | 4.0 |
WP_Customize_Control |
UI element for user input | 3.4 |
WP_Customize_Selective_Refresh |
Partial content refresh | 4.5 |
WP_Customize_Partial |
Defines refreshable content regions | 4.5 |
Live Preview System
The Customizer implements a sophisticated live preview system:
Preview Transport Methods
refresh(default) – Reloads the entire preview iframe when setting changespostMessage– Uses JavaScript to update preview without reload
$wp_customize->add_setting( 'my_setting', array(
'transport' => 'postMessage', // Enable JS-based preview
) );
Preview Flow
- User makes changes in the Customizer pane
- Changes are stored in a changeset (post type:
customize_changeset) - Preview iframe displays changes without saving to database
- On publish, settings are persisted to their storage locations
Changesets (Since 4.7)
Changesets allow saving customizations without publishing:
- UUID-based – Each changeset has a unique identifier
- Transactional – All-or-nothing publishing
- Schedulable – Can be scheduled for future publishing
- Collaborative – Supports autosave revisions per user
- Branching – Optional concurrent changeset support
Changeset Statuses
| Status | Description |
|---|---|
auto-draft |
Unsaved work in progress |
draft |
Saved but not published |
pending |
Awaiting review |
future |
Scheduled for publication |
publish |
Applied to the site |
Selective Refresh (Since 4.5)
Selective refresh updates only specific portions of the preview:
$wp_customize->selective_refresh->add_partial( 'blogname', array(
'selector' => '.site-title a',
'render_callback' => function() {
bloginfo( 'name' );
},
) );
Benefits
- Performance – Updates only what changed
- UX – Smoother preview experience
- Reliability – Maintains scroll position and state
Registration Hook
All Customizer components should be registered on the customize_register action:
add_action( 'customize_register', function( $wp_customize ) {
// Add panels, sections, controls, settings, and partials
} );
Basic Example
add_action( 'customize_register', function( $wp_customize ) {
// Add a section
$wp_customize->add_section( 'my_section', array(
'title' => __( 'My Section' ),
'priority' => 30,
) );
// Add a setting
$wp_customize->add_setting( 'my_color', array(
'default' => '#000000',
'sanitize_callback' => 'sanitize_hex_color',
'transport' => 'postMessage',
) );
// Add a control
$wp_customize->add_control( new WP_Customize_Color_Control(
$wp_customize,
'my_color',
array(
'label' => __( 'My Color' ),
'section' => 'my_section',
)
) );
} );
Built-in Control Types
Basic Controls (via type argument)
text– Text input (default)checkbox– Checkboxtextarea– Multi-line textradio– Radio buttons (requireschoices)select– Dropdown (requireschoices)dropdown-pages– Page selectoremail,url,number,hidden,date– HTML5 inputs
Specialized Control Classes
WP_Customize_Color_Control– Color pickerWP_Customize_Media_Control– Media library attachmentWP_Customize_Image_Control– Image uploadWP_Customize_Upload_Control– File uploadWP_Customize_Cropped_Image_Control– Image with croppingWP_Customize_Code_Editor_Control– Syntax-highlighted code editorWP_Customize_Date_Time_Control– Date/time picker
Capability Model
The Customizer uses WordPress capabilities for access control:
customize– Meta capability that maps toedit_theme_options- Controls inherit capability from their associated settings
- Sections and panels have their own
capabilityproperty
Theme Support
Settings can require theme support:
$wp_customize->add_setting( 'header_image', array(
'theme_supports' => 'custom-header',
) );
Starter Content (Since 4.7)
Themes can provide starter content that’s imported on fresh installations:
add_theme_support( 'starter-content', array(
'widgets' => array( /* ... */ ),
'posts' => array( /* ... */ ),
'nav_menus' => array( /* ... */ ),
'options' => array( /* ... */ ),
'theme_mods' => array( /* ... */ ),
) );
JavaScript API
The Customizer has a robust JavaScript API (wp.customize):
// React to setting changes
wp.customize( 'my_setting', function( setting ) {
setting.bind( function( value ) {
// Update preview
} );
} );
// From preview frame
wp.customize.preview.bind( 'active', function() {
// Preview is ready
} );
Related Files
| File | Purpose |
|---|---|
class-wp-customize-manager.php |
Manager class |
class-wp-customize-setting.php |
Settings class |
class-wp-customize-section.php |
Sections class |
class-wp-customize-panel.php |
Panels class |
class-wp-customize-control.php |
Controls class |
customize/ |
Specialized subclasses |