WordPress Theme Customiser

WordPress Theme Customiser

Reference

Customiser Object

Four main types

Panels

Containers for sections

Sections

UI containers

Settings

Accociate UI elements(controls) with settings

$wp_customize->add_setting( 'setting_id', array(
  'type' => 'theme_mod', // or 'option'
  'capability' => 'edit_theme_options',
  'theme_supports' => '', // Rarely needed.
  'default' => '',
  'transport' => 'refresh', // or postMessage
  'sanitize_callback' => '',
  'sanitize_js_callback' => '', // Basically to_json.
) );

Theme Modification VS Options

$wp_customize->add_setting('custom_theme_css', array(
  'type' => 'theme_mod',
) );

$wp_customize->add_setting('custom_theme_css', array(
  'type' => 'option',
) );

Default Value and Sanitization callback: ensure that no unsafe data is stored in the database

$wp_customize->add_setting( 'accent_color', array(
  'default' => '#f72525',
  'sanitize_callback' => 'sanitize_hex_color',
) );

Retrieve and use the values: get_theme_mod( string $name, bool|string $default = false )

Controls

WP_Customize_Manager

Methods: add_TYPE , get_TYPE , and remove_TYPE

add_action('customize_register','my_customize_register');
function my_customize_register( $wp_customize ) {
  $wp_customize->add_panel();
  $wp_customize->get_panel();
  $wp_customize->remove_panel();
 
  $wp_customize->add_section();
  $wp_customize->get_section();
  $wp_customize->remove_section();
 
  $wp_customize->add_setting();
  $wp_customize->get_setting();
  $wp_customize->remove_setting();
 
  $wp_customize->add_control();
  $wp_customize->get_control();
  $wp_customize->remove_control();
}