customize_register

WPTurbo » Hooks » customize_register

The customize_register hook is a WordPress filter that’s triggered when the Customizer is initialized. It’s mainly used to add or modify settings, sections, and controls in the Customizer. This hook gives developers the ability to extend the WordPress Customizer with additional options and features.

When the customize_register hook is triggered, it passes a single parameter: an instance of the WP_Customize_Manager class. This class contains all of the settings, sections, and controls that are currently registered in the Customizer.

Here’s an example of how to use the customize_register hook to add a new section and control to the Customizer:

function mytheme_customize_register( $wp_customize ) {
    // Add a new section
    $wp_customize->add_section( 'mytheme_new_section', array(
        'title' => __( 'New Section', 'mytheme' ),
        'priority' => 10,
    ) );

    // Add a new control
    $wp_customize->add_setting( 'mytheme_new_setting', array(
        'default' => '#ffffff',
        'transport' => 'refresh',
    ) );

    $wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'mytheme_new_control', array(
        'label' => __( 'New Control', 'mytheme' ),
        'section' => 'mytheme_new_section',
        'settings' => 'mytheme_new_setting',
    ) ) );
}
add_action( 'customize_register', 'mytheme_customize_register' );

In this example, we’re adding a new section called "New Section" with a priority of 10, and a new color control called "New Control" that’s linked to the "mytheme_new_setting" setting within the "mytheme_new_section" section of the Customizer.

Overall, the customize_register hook is an essential tool for WordPress developers who want to customize the Customizer and add new options to their themes or plugins.

Learn More on WordPress.org

Register an account to save your snippets or go Pro to get more features.