The Theme Customizer lets you change your WordPress site's look and feel easily. You can see your changes live before saving.
Theme customizer in Wordpress
Start learning this pattern below
Jump into concepts and practice - no test required
<?php
function mytheme_customize_register( $wp_customize ) {
$wp_customize->add_section('mytheme_colors', array(
'title' => __('Colors', 'mytheme'),
'priority' => 30,
));
$wp_customize->add_setting('header_textcolor', array(
'default' => '#000000',
'sanitize_callback' => 'sanitize_hex_color',
));
$wp_customize->add_control(new WP_Customize_Color_Control($wp_customize, 'header_textcolor_control', array(
'label' => __('Header Text Color', 'mytheme'),
'section' => 'mytheme_colors',
'settings' => 'header_textcolor',
)));
}
add_action('customize_register', 'mytheme_customize_register');The customize_register hook lets you add sections, settings, and controls.
Settings store the value, controls create the UI, and sections group controls.
<?php // Add a text setting and control $wp_customize->add_setting('footer_text', array( 'default' => 'My footer text', 'sanitize_callback' => 'sanitize_text_field', )); $wp_customize->add_control('footer_text_control', array( 'label' => __('Footer Text', 'mytheme'), 'section' => 'title_tagline', 'settings' => 'footer_text', 'type' => 'text', ));
<?php // Add an image upload control $wp_customize->add_setting('logo_image'); $wp_customize->add_control(new WP_Customize_Image_Control($wp_customize, 'logo_image_control', array( 'label' => __('Upload Logo', 'mytheme'), 'section' => 'title_tagline', 'settings' => 'logo_image', )));
This code adds a new section called "Colors" in the Theme Customizer. Inside it, there is a setting for the header text color with a color picker control. Users can pick a color and see it live before saving.
<?php
function mytheme_customize_register( $wp_customize ) {
// Add a section for colors
$wp_customize->add_section('mytheme_colors', array(
'title' => __('Colors', 'mytheme'),
'priority' => 30,
));
// Add a setting for header text color
$wp_customize->add_setting('header_textcolor', array(
'default' => '#000000',
'sanitize_callback' => 'sanitize_hex_color',
));
// Add a color picker control
$wp_customize->add_control(new WP_Customize_Color_Control($wp_customize, 'header_textcolor_control', array(
'label' => __('Header Text Color', 'mytheme'),
'section' => 'mytheme_colors',
'settings' => 'header_textcolor',
)));
}
add_action('customize_register', 'mytheme_customize_register');Always use sanitize_callback to keep user input safe.
Use descriptive labels so users understand what each control does.
Test your customizer options on different screen sizes for accessibility.
The Theme Customizer lets users change site appearance with live preview.
You add sections, settings, and controls to create options.
Use sanitization and clear labels for a safe and friendly experience.
Practice
Solution
Step 1: Understand the role of the Theme Customizer
The Theme Customizer is designed to let users modify the look and feel of their site and see changes immediately.Step 2: Compare options with this purpose
Options B, C, and D relate to plugins, user management, and content creation, which are not the Customizer's function.Final Answer:
To allow users to change site appearance with a live preview -> Option CQuick Check:
Theme Customizer = live preview appearance changes [OK]
- Confusing Customizer with plugin installer
- Thinking it manages users or content
- Assuming it edits posts directly
Solution
Step 1: Identify the function for adding sections
The functionadd_section()is specifically used to add a new section in the Theme Customizer.Step 2: Differentiate from other functions
add_setting()adds settings,add_control()adds controls, andregister_section()is not a valid function.Final Answer:
add_section() -> Option AQuick Check:
Section addition = add_section() [OK]
- Using add_setting() to add sections
- Confusing add_control() with add_section()
- Using non-existent register_section()
$wp_customize->add_setting('header_text', [
'default' => 'Welcome!',
'sanitize_callback' => 'sanitize_text_field'
]);
$wp_customize->add_control('header_text', [
'label' => 'Header Text',
'section' => 'title_tagline',
'type' => 'text'
]);
What will get_theme_mod('header_text') return if the user has not changed the setting?Solution
Step 1: Check the default value in add_setting()
The setting 'header_text' has a default value of 'Welcome!'.Step 2: Understand get_theme_mod behavior
If the user has not changed the setting,get_theme_mod()returns the default value.Final Answer:
'Welcome!' -> Option DQuick Check:
Default value returned by get_theme_mod() = 'Welcome!' [OK]
- Assuming it returns null if unset
- Expecting empty string instead of default
- Thinking it throws an error without user input
$wp_customize->add_setting('footer_text', [
'default' => 'Footer here',
'sanitize_callback' => 'sanitize_text_field'
]);
$wp_customize->add_control('footer_text', [
'label' => 'Footer Text',
'section' => 'footer_section',
'type' => 'textarea'
]);
Assuming 'footer_section' was never added, what issue will occur?Solution
Step 1: Check if the section exists
The control references 'footer_section', but this section was never added with add_section().Step 2: Understand control behavior without a valid section
Controls must belong to existing sections; otherwise, they won't display in the Customizer UI.Final Answer:
The control will not display because the section does not exist -> Option AQuick Check:
Control needs valid section to display = true [OK]
- Assuming setting fails to save without section
- Thinking sanitize_callback causes error here
- Believing default value is ignored
Solution
Step 1: Add a section for background settings
First, create a section usingadd_section()to group background options.Step 2: Add a setting with a default color
Useadd_setting()to define the background color setting and provide a default value.Step 3: Add a color picker control linked to the setting and section
Useadd_control()withtype => 'color'to let users pick a color.Final Answer:
Use add_setting() with default color, add_section() for background, then add_control() with type 'color' -> Option BQuick Check:
Section, setting, control in order with color type [OK]
- Adding control before section or setting
- Skipping add_section()
- Using wrong control type for color picker
