Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Jespertastesen/aa82cc05d9f482b25b9f60d0c02d193c to your computer and use it in GitHub Desktop.
Save Jespertastesen/aa82cc05d9f482b25b9f60d0c02d193c 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