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.
Lessons
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.
| Piece | What it is |
|---|---|
| Panel | An optional top-level group of sections. |
| Section | A screen of related options, such as "Footer". |
| Setting | The stored value and how it is sanitised. |
| Control | The form field the owner uses to change the setting. |
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.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.
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.postMessage and confirm it updates without a reload.<script> as the footer text and confirm the sanitiser strips it.In accordance with Section 6 & Section 7 of the DPDP Act 2023, third-party analytics and marketing trackers are strictly gated behind your affirmative consent. You may withdraw consent at any time under Section 6(4).
Essential for session authentication (ci_session), Cross-Site Request Forgery security (csrf_cookie_name), and Cloudflare DDoS/bot prevention. Cannot be disabled.
Gated under DPDP Section 7. Allows Google Analytics 4 (Google Consent Mode v2) to measure aggregate, anonymized tool usage.
Enables browser local storage to preserve your theme selection (dark/light mode) and code editor layout between visits.
Used to display developer sponsorships and native tool partner notices.