Complete the code to add a setting in the WordPress theme customizer.
function mytheme_customize_register($wp_customize) {
$wp_customize->add_setting('[1]');
}
add_action('customize_register', 'mytheme_customize_register');The add_setting method requires the setting ID as a string. 'header_textcolor' is a common setting ID used in themes.
Complete the code to add a control for the setting in the WordPress theme customizer.
function mytheme_customize_register($wp_customize) {
$wp_customize->add_setting('header_textcolor');
$wp_customize->add_control(new WP_Customize_Color_Control($wp_customize, 'header_textcolor_control', [
'label' => 'Header Text Color',
'section' => 'colors',
'settings' => [1]
]));
}
add_action('customize_register', 'mytheme_customize_register');The control must link to the setting ID it controls. Here, the setting is 'header_textcolor'.
Fix the error in the code to properly add a text input control in the theme customizer.
function mytheme_customize_register($wp_customize) {
$wp_customize->add_setting('footer_text');
$wp_customize->add_control('footer_text_control', [
'label' => 'Footer Text',
'section' => 'title_tagline',
'settings' => [1]
]);
}
add_action('customize_register', 'mytheme_customize_register');The 'settings' key must refer to the setting ID, which is 'footer_text'. Using the control ID or section name is incorrect.
Fill both blanks to add a checkbox setting and control in the theme customizer.
function mytheme_customize_register($wp_customize) {
$wp_customize->add_setting('[1]', [
'default' => false,
'sanitize_callback' => 'wp_validate_boolean'
]);
$wp_customize->add_control('[2]', [
'type' => 'checkbox',
'label' => 'Show Sidebar',
'section' => 'sidebar_options',
'settings' => 'show_sidebar'
]);
}
add_action('customize_register', 'mytheme_customize_register');The setting ID is 'show_sidebar'. The control ID should be a unique string, here 'show_sidebar_control'.
Fill all three blanks to add a text setting with a sanitize callback and a control in the theme customizer.
function mytheme_customize_register($wp_customize) {
$wp_customize->add_setting('[1]', [
'default' => '',
'sanitize_callback' => [2]
]);
$wp_customize->add_control('[3]', [
'type' => 'text',
'label' => 'Custom Text',
'section' => 'custom_text_section',
'settings' => '[1]'
]);
}
add_action('customize_register', 'mytheme_customize_register');The setting ID is 'custom_text'. The sanitize callback is 'sanitize_text_field' (without quotes in code, but here as string). The control ID is 'custom_text_control'.