Skip to content

Instantly share code, notes, and snippets.

@ajskelton
Last active July 17, 2020 05:05
Show Gist options
  • Save ajskelton/0d31057f76b14fdc1651ea6a49c5daba to your computer and use it in GitHub Desktop.
Save ajskelton/0d31057f76b14fdc1651ea6a49c5daba to your computer and use it in GitHub Desktop.
Add a Color Control field to the WordPress Customizer.
$wp_customize->add_setting( 'core_color_setting_id', array(
'sanitize_callback' => 'themeslug_sanitize_hex_color',
) );
$wp_customize->add_control(
new WP_Customize_Color_Control( $wp_customize, 'core_color_setting_id',
array(
'label' => __( 'Core Color Setting' ),
'description' => __( 'Select a color for something' ),
'section' => 'custom_section', // Add a default or your own section
) ) );
function themeslug_sanitize_hex_color( $hex_color, $setting ) {
// Sanitize $input as a hex value without the hash prefix.
$hex_color = sanitize_hex_color( $hex_color );
// If $input is a valid hex value, return it; otherwise, return the default.
return ( ! null( $hex_color ) ? $hex_color : $setting->default );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment