In WordPress, the customize_register
action is a hook that allows you to add, modify, or remove settings, controls, and sections in the WordPress Customizer. The Customizer is a built-in tool that lets users preview changes to their site’s appearance in real-time before applying them.
When you want to extend or modify the Customizer, you can hook into customize_register
. This action provides access to the WP_Customize_Manager
object, which is the core class that handles the Customizer. Through this object, you can:
Here’s a simple example of how to use customize_register
to add a custom setting and control to change the site’s background color:
function mytheme_customize_register( $wp_customize ) {
// Add a new section to the Customizer
$wp_customize->add_section( 'mytheme_colors', array(
'title' => __( 'Theme Colors', 'mytheme' ),
'priority' => 30,
) );
// Add a new setting for the background color
$wp_customize->add_setting(
'background_color', array(
'default' => '#ffffff', // Default color
'transport' => 'refresh', // Whether to refresh the page on change
) );
// Add a control to change the background color
$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'background_color', array(
'label' => __( 'Background Color', 'mytheme' ),
'section' => 'mytheme_colors',
'settings' => 'background_color',
)));
}
add_action( 'customize_register', 'mytheme_customize_register' );
customize_register
:
mytheme_customize_register
is attached to the customize_register
action. This means it will run when the Customizer is being initialized.$wp_customize->add_section()
adds a new section called “Theme Colors” to the Customizer. This is where all color-related settings will be grouped.$wp_customize->add_setting()
adds a new setting called background_color
. This stores the user’s chosen background color in the database.$wp_customize->add_control()
adds a color picker control to the “Theme Colors” section. This control lets the user select a background color.customize_register
?The customize_register
action is a powerful tool for theme and plugin developers to extend the WordPress Customizer. By hooking into it, you can create custom options that allow users to personalize their websites without touching any code. The result is a more flexible and user-friendly WordPress theme.