functions.php to add a color setting and control in the Theme Customizer. What will the user see in the Customizer panel?<?php
function mytheme_customize_register($wp_customize) {
$wp_customize->add_setting('header_textcolor', [
'default' => '#000000',
'transport' => 'refresh',
]);
$wp_customize->add_control(new WP_Customize_Color_Control($wp_customize, 'header_textcolor_control', [
'label' => __('Header Text Color', 'mytheme'),
'section' => 'colors',
'settings' => 'header_textcolor',
]));
}
add_action('customize_register', 'mytheme_customize_register');
?>The code adds a color setting with a default black color and a color picker control labeled 'Header Text Color' inside the existing 'Colors' section. The 'transport' => 'refresh' means the preview reloads on change but does not affect control visibility.
Changes in the Customizer are previewed but not saved until the user clicks 'Publish'. Until then, get_theme_mod returns the saved value, which is the default '#ffffff'.
Setting 'transport' to 'postMessage' enables JavaScript to update the preview instantly without reload. 'refresh' reloads the preview. 'live' is not a valid transport value.
functions.php but the control does not show in the Customizer. What is the cause?<?php
function mytheme_customize_register($wp_customize) {
$wp_customize->add_setting('sidebar_position', [
'default' => 'right',
'transport' => 'refresh',
]);
$wp_customize->add_control('sidebar_position_control', [
'label' => __('Sidebar Position', 'mytheme'),
'section' => 'layout',
'settings' => 'sidebar_position',
'type' => 'radio',
'choices' => [
'left' => 'Left',
'right' => 'Right'
],
]);
}
add_action('customize_register', 'mytheme_customize_register');
?>The 'layout' section is not a default section in the Customizer. Controls must be added to existing sections or you must add the section first. Since 'layout' does not exist, the control is not shown.
Selective refresh requires registering a partial with a CSS selector and a PHP render callback to update only that part of the preview. It works with 'refresh' transport but needs explicit partial registration.