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
<?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.