Theme customiztion with customize_register hook in wordpress.
There are four main types of Customizer objects: Panels, Sections, Settings, and Controls. To add, remove, or modify any Customizer object, and to access the Customizer Manager, we have to use the customize_register
hook.
//example of customize_register hook
function themeslug_customize_register( $wp_customize ) {
// Do stuff with $wp_customize, the WP_Customize_Manager object.
}
add_action( 'customize_register', 'themeslug_customize_register' );
//adding section, setting & controls
function dws_customize_register($wp_customize){
$wp_customize->add_section('dws_color_scheme', array(
'title' => __('Color Scheme', 'theme_name'),
'description' => '',
'priority' => 120,
));
// Text Input
$wp_customize->add_setting('dws_theme_options[text_test]', array(
'default' => 'value abc',
'capability' => 'edit_theme_options',
'type' => 'option',
));
$wp_customize->add_control('dws_text_test', array(
'label' => __('Text Test', 'theme_name'),
'section' => 'dws_color_scheme',
'settings' => 'dws_theme_options[text_test]',
));
// Radio Input
$wp_customize->add_setting('dws_theme_options[color_scheme]', array(
'default' => 'value2',
'capability' => 'edit_theme_options',
'type' => 'option',
));
$wp_customize->add_control('dws_color_scheme', array(
'label' => __('Color Scheme', 'theme_name'),
'section' => 'dws_color_scheme',
'settings' => 'dws_theme_options[color_scheme]',
'type' => 'radio',
'choices' => array(
'value1' => 'Choice 1',
'value2' => 'Choice 2',
'value3' => 'Choice 3',
),
));
// Checkbox
$wp_customize->add_setting('dws_theme_options[checkbox_test]', array(
'capability' => 'edit_theme_options',
'type' => 'option',
));
$wp_customize->add_control('display_header_text', array(
'settings' => 'dws_theme_options[checkbox_test]',
'label' => __('Display Header Text'),
'section' => 'dws_color_scheme',
'type' => 'checkbox',
));
}
add_action('customize_register', 'dws_customize_register');