WP_Customize_Manager
is a core WordPress class responsible for managing the customization interface, commonly known as the WordPress Customizer. The Customizer allows users to preview and make changes to their site’s appearance, including theme options, widgets, and settings, all in a live preview mode before saving those changes.
Developers often use WP_Customize_Manager
to add custom theme options, such as a custom logo, background color, or layout settings, which users can tweak via 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' => 30,
));
// Add a new setting
$wp_customize->add_setting( 'mytheme_setting', array(
'default' => '#000000',
'transport' => 'refresh',
));
// Add a new control
$wp_customize->add_control( new WP_Customize_Color_control( $wp_customize, 'mytheme_setting', array(
'label' => __( 'Color Picker', 'mytheme' ),
'section' => 'mytheme_new_section',
'settings' => 'mytheme_setting',
)));
}
add_action( 'customize_register', 'mytheme_customize_register' );
This example adds a new section, setting, and color picker control to the Customizer, allowing users to select a color that can be applied to their theme.
add_action( 'customize_register', 'mytheme_customize_register' );
This line hooks the mytheme_customize_register
function into the customize_register
action. This action is triggered when the WordPress Customizer is being initialized, allowing you to add your custom settings, sections, and controls.
function mytheme_customize_register( $wp_customize ) {
The mytheme_customize_register
function is where you define all the customizations. It takes one parameter, $wp_customize
, which is an instance of the WP_Customize_Manager
class.
$wp_customize->add_section( 'mytheme_new_section' , array( 'title' => __( 'New Section', 'mytheme' ), 'priority' => 30, ));
add_section
: Adds a new section to the Customizer.'mytheme_new_section'
: This is a unique ID for the section.'title' => __( 'New Section', 'mytheme' )
: The title of the section, which will be displayed in the Customizer. The __()
function is used for internationalization, allowing the text to be translated.'priority' => 30
: Controls the order in which this section appears relative to other sections. Lower numbers appear higher up.$wp_customize->add_setting( 'mytheme_setting', array(
'default' => '#000000',
'transport' => 'refresh',
));
add_setting
: Registers a new setting in the Customizer.'mytheme_setting'
: A unique ID for the setting.'default' => '#000000'
: Sets the default value of the setting (in this case, black color).'transport' => 'refresh'
: Specifies how changes to this setting are handled. 'refresh'
reloads the entire preview pane when the setting changes, while 'postMessage'
uses JavaScript to update the preview instantly without a full reload.$wp_customize->add_control(
new WP_Customize_Color_control( $wp_customize, 'mytheme_setting', array(
'label' => __( 'Color Picker', 'mytheme' ),
'section' => 'mytheme_new_section',
'settings' => 'mytheme_setting',
)));