0
0
WordpressHow-ToBeginner · 4 min read

How to Use Customizer in WordPress: Simple Guide

Use the WP_Customize_Manager class in WordPress to add settings, controls, and sections to the Customizer. Hook your function to customize_register action to register your custom options and see live preview changes.
📐

Syntax

The WordPress Customizer uses the customize_register action hook to add custom settings and controls. You create a function that receives the $wp_customize object, then use methods like add_section, add_setting, and add_control to define your options.

Each part means:

  • add_section: Creates a new panel or group in the Customizer.
  • add_setting: Defines a setting to store user input.
  • add_control: Adds a UI control (like text input or color picker) linked to a setting.
php
function mytheme_customize_register($wp_customize) {
  $wp_customize->add_section('mytheme_new_section', array(
    'title' => __('New Section', 'mytheme'),
    'priority' => 30,
  ));

  $wp_customize->add_setting('mytheme_text_setting', array(
    'default' => 'Hello World',
    'sanitize_callback' => 'sanitize_text_field',
  ));

  $wp_customize->add_control('mytheme_text_setting', array(
    'label' => __('Text Setting', 'mytheme'),
    'section' => 'mytheme_new_section',
    'type' => 'text',
  ));
}
add_action('customize_register', 'mytheme_customize_register');
💻

Example

This example adds a new section called "Site Message" with a text input control. The user can change the message, which you can display in your theme using get_theme_mod.

php
<?php
function mytheme_customize_register($wp_customize) {
  $wp_customize->add_section('site_message_section', array(
    'title' => __('Site Message', 'mytheme'),
    'priority' => 30,
  ));

  $wp_customize->add_setting('site_message_setting', array(
    'default' => 'Welcome to my website!',
    'sanitize_callback' => 'sanitize_text_field',
  ));

  $wp_customize->add_control('site_message_setting', array(
    'label' => __('Message Text', 'mytheme'),
    'section' => 'site_message_section',
    'type' => 'text',
  ));
}
add_action('customize_register', 'mytheme_customize_register');

// To display the message in your theme:
echo get_theme_mod('site_message_setting', 'Welcome to my website!');
Output
Welcome to my website!
⚠️

Common Pitfalls

Common mistakes when using the Customizer include:

  • Not sanitizing user input with a sanitize_callback, which can cause security issues.
  • Forgetting to hook your function to customize_register, so your settings don’t appear.
  • Using incorrect section or setting IDs, causing controls not to show.
  • Not providing a default value, which can lead to empty outputs.
php
/* Wrong: Missing sanitize_callback */
$wp_customize->add_setting('bad_setting', array(
  'default' => 'text',
  'sanitize_callback' => '',
));

/* Right: With sanitize_callback */
$wp_customize->add_setting('good_setting', array(
  'default' => 'text',
  'sanitize_callback' => 'sanitize_text_field',
));
📊

Quick Reference

FunctionPurpose
add_sectionCreate a new section in the Customizer
add_settingDefine a setting to store user input
add_controlAdd a UI control linked to a setting
sanitize_callbackFunction to clean user input
get_theme_modRetrieve the saved setting value

Key Takeaways

Hook your function to customize_register to add customizer options.
Always use sanitize_callback to clean user input for security.
Use add_section, add_setting, and add_control to build your customizer UI.
Retrieve saved values with get_theme_mod to show changes in your theme.
Provide default values to avoid empty or broken outputs.