Skip to content

Instantly share code, notes, and snippets.

@drikusroor
Created November 15, 2021 12:08
Show Gist options
  • Save drikusroor/7b0be90bd9dc2de9deed870e621611c1 to your computer and use it in GitHub Desktop.
Save drikusroor/7b0be90bd9dc2de9deed870e621611c1 to your computer and use it in GitHub Desktop.
Quickly add multiple custom color settings and controls to a Wordpress theme's Customizer
<?php
function my_theme_add_setting_control($wp_customize, $color, $options)
{
$key = $color[0];
$label = $color[1];
$default_hex = $color[2];
$section = $options['section'];
$text_domain = $options['text_domain'];
$wp_customize->add_setting($key, array(
'default' => $default_hex ?? '#000',
'sanitize_callback' => 'sanitize_hex_color',
'capability' => 'edit_theme_options',
'type' => 'option',
));
$wp_customize->add_control(new WP_Customize_Color_Control($wp_customize, $key, array(
'label' => __($label, $text_domain),
'section' => $section,
'settings' => $key,
)));
}
function my_theme_customize_register($wp_customize)
{
$options = ['section' => 'colors', 'text_domain' => 'my_theme'];
$custom_colors = [
['my_theme_light_bg_color', 'Background color (light)', '#d8b65f'],
['my_theme_light_text_color', 'Text color (light)', '#000000'],
['my_theme_dark_bg_color', 'Background color (dark)', '#10181E'],
['my_theme_dark_text_color', 'Text color (dark)', '#C7BDAD'],
['my_theme_hacker_bg_color', 'Background color (hacker)', '#0A0D07'],
['my_theme_hacker_text_color', 'Text color (hacker)', '#4AAC2F'],
];
foreach ($custom_colors as $color) {
my_theme_add_setting_control($wp_customize, $color, $options);
}
}
add_action('customize_register', 'my_theme_customize_register');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment