Using WordPress ‘customize_register’ PHP action

The customize_register WordPress PHP action fires once WordPress has loaded, allowing you to customize and manipulate the Theme Customization admin screen.

Usage

add_action('customize_register', 'your_custom_function_name');

function your_custom_function_name($wp_customize) {
    // your custom code here
}

Parameters

  • $wp_customize (WP_Customize_Manager): An instance of the WP_Customize_Manager class that controls the Theme Customizer screen.

More information

See WordPress Developer Resources: customize_register

Examples

Add a color picker control

Add a color picker control to the customizer to change the background color.

function mytheme_customize_register($wp_customize) {
    $wp_customize->add_setting('mytheme_background_color', array(
        'default' => '#FFFFFF',
        'sanitize_callback' => 'sanitize_hex_color',
        'transport' => 'refresh',
    ));

    $wp_customize->add_control(new WP_Customize_Color_Control($wp_customize, 'mytheme_background_color', array(
        'label' => __('Background Color', 'mytheme'),
        'section' => 'colors',
        'settings' => 'mytheme_background_color',
    )));
}
add_action('customize_register', 'mytheme_customize_register');

Add a custom logo control

Add a custom logo control to the customizer, allowing users to upload their own logo.

function mytheme_customize_register($wp_customize) {
    $wp_customize->add_setting('mytheme_logo', array(
        'sanitize_callback' => 'esc_url_raw',
        'transport' => 'refresh',
    ));

    $wp_customize->add_control(new WP_Customize_Image_Control($wp_customize, 'mytheme_logo', array(
        'label' => __('Logo', 'mytheme'),
        'section' => 'title_tagline',
        'settings' => 'mytheme_logo',
    )));
}
add_action('customize_register', 'mytheme_customize_register');

Add a text area control

Add a text area control to the customizer for users to add custom footer text.

function mytheme_customize_register($wp_customize) {
    $wp_customize->add_setting('mytheme_footer_text', array(
        'default' => '',
        'sanitize_callback' => 'wp_kses_post',
        'transport' => 'refresh',
    ));

    $wp_customize->add_control('mytheme_footer_text', array(
        'label' => __('Footer Text', 'mytheme'),
        'section' => 'footer',
        'type' => 'textarea',
        'settings' => 'mytheme_footer_text',
    ));
}
add_action('customize_register', 'mytheme_customize_register');

Add a radio button control

Add a radio button control to the customizer for users to choose a layout style.

function mytheme_customize_register($wp_customize) {
    $wp_customize->add_setting('mytheme_layout_style', array(
        'default' => 'full-width',
        'sanitize_callback' => 'mytheme_sanitize_layout',
        'transport' => 'refresh',
    ));

    $wp_customize->add_control('mytheme_layout_style', array(
        'label' => __('Layout Style', 'mytheme'),
        'section' => 'layout',
        'type' => 'radio',
        'choices' => array(
            'full-width' => __('Full Width', 'mytheme'),
            'boxed' => __('Boxed', 'mytheme'),
        ),
        'settings' => 'mytheme_layout_style',
));
}

function mytheme_sanitize_layout($input) {
$valid = array(
'full-width' => __('Full Width', 'mytheme'),
'boxed' => __('Boxed', 'mytheme'),
);

if (array_key_exists($input, $valid)) {
    return $input;
} else {
    return '';
}
}

add_action('customize_register', 'mytheme_customize_register');

Add a dropdown pages control

Add a dropdown pages control to the customizer for users to select a custom home page.

function mytheme_customize_register($wp_customize) {
    $wp_customize->add_setting('mytheme_home_page', array(
        'default' => '',
        'sanitize_callback' => 'absint',
        'transport' => 'refresh',
    ));

    $wp_customize->add_control('mytheme_home_page', array(
        'label' => __('Home Page', 'mytheme'),
        'section' => 'static_front_page',
        'type' => 'dropdown-pages',
        'settings' => 'mytheme_home_page',
    ));
}
add_action('customize_register', 'mytheme_customize_register');