SponsoredHosinger Special Offer

WP WordPress Theme Development

Build classic and block WordPress themes from scratch: the template hierarchy, The Loop, the_title() and the_content(), template parts, menus, the Customizer, child themes and theme.json.

The Theme Customizer API

WordPress Theme Development Lesson 24 of 30 ~5 min read

Overview

The Customizer API lets a classic theme add its own options to Appearance → Customize — an accent colour, a footer message, a layout choice — with a live preview, so site owners can adjust the design without editing code.

Panels, sections, settings and controls

PieceWhat it is
PanelAn optional top-level group of sections.
SectionA screen of related options, such as "Footer".
SettingThe stored value and how it is sanitised.
ControlThe form field the owner uses to change the setting.

Adding an accent colour and a footer text

Create inc/customizer.php and load it from functions.php:

<?php
/**
 * Customizer options for GDC Starter.
 *
 * @param WP_Customize_Manager $wp_customize Customizer object.
 */
function gdc_starter_customize_register( $wp_customize ) {

    // ── Accent colour, added to the core Colors section. ──
    $wp_customize->add_setting(
        'gdc_starter_accent_color',
        array(
            'default'           => '#6c63ff',
            'sanitize_callback' => 'sanitize_hex_color',
            'transport'         => 'refresh',
        )
    );

    $wp_customize->add_control(
        new WP_Customize_Color_Control(
            $wp_customize,
            'gdc_starter_accent_color',
            array(
                'label'   => esc_html__( 'Accent colour', 'gdc-starter' ),
                'section' => 'colors',
            )
        )
    );

    // ── A new Footer section. ──
    $wp_customize->add_section(
        'gdc_starter_footer',
        array(
            'title'    => esc_html__( 'Footer', 'gdc-starter' ),
            'priority' => 120,
        )
    );

    $wp_customize->add_setting(
        'gdc_starter_footer_text',
        array(
            'default'           => '',
            'sanitize_callback' => 'sanitize_text_field',
            'transport'         => 'postMessage',
        )
    );

    $wp_customize->add_control(
        'gdc_starter_footer_text',
        array(
            'label'   => esc_html__( 'Footer text', 'gdc-starter' ),
            'section' => 'gdc_starter_footer',
            'type'    => 'text',
        )
    );
}
add_action( 'customize_register', 'gdc_starter_customize_register' );
Watch out: Every setting needs a sanitize_callback. The Customizer saves whatever the browser sends, and without sanitising, an attacker with a stolen editor account could store script in your footer.

Using the values

Read a value with get_theme_mod(), always passing the same default you registered:

<?php
// footer.php
$footer_text = get_theme_mod( 'gdc_starter_footer_text', '' );

if ( $footer_text ) {
    echo '<p class="footer-text">' . esc_html( $footer_text ) . '</p>';
}

For the colour, add CSS to your enqueued stylesheet rather than printing a <style> tag by hand:

<?php
function gdc_starter_accent_css() {
    $accent = sanitize_hex_color( get_theme_mod( 'gdc_starter_accent_color', '#6c63ff' ) );

    wp_add_inline_style(
        'gdc-starter-style',
        ':root { --gdc-accent: ' . $accent . '; }'
    );
}
add_action( 'wp_enqueue_scripts', 'gdc_starter_accent_css', 20 );

Then use var(--gdc-accent) throughout style.css. One setting now recolours links, buttons and borders everywhere.

Instant preview with postMessage

transport => refresh reloads the preview on every change. postMessage updates it instantly with a little JavaScript, loaded only inside the Customizer preview:

// assets/js/customizer-preview.js
( function ( api ) {
    api( 'gdc_starter_footer_text', function ( value ) {
        value.bind( function ( newText ) {
            document.querySelectorAll( '.footer-text' ).forEach( function ( el ) {
                el.textContent = newText; // textContent: never inject HTML.
            } );
        } );
    } );
} )( wp.customize );
<?php
function gdc_starter_customize_preview_js() {
    wp_enqueue_script(
        'gdc-starter-customizer-preview',
        get_template_directory_uri() . '/assets/js/customizer-preview.js',
        array( 'customize-preview' ),
        GDC_STARTER_VERSION,
        true
    );
}
add_action( 'customize_preview_init', 'gdc_starter_customize_preview_js' );
Tip: Block themes do not use the Customizer — their options live in theme.json and the Site Editor (lesson 28). The Customizer remains the right tool for classic themes.

Practice

  • Add the accent colour and switch it in the Customizer; watch links and buttons change.
  • Add the footer text with postMessage and confirm it updates without a reload.
  • Try saving <script> as the footer text and confirm the sanitiser strips it.
Create a free account to save which lessons you've finished. Save my progress