Add custom colors to the WordPress editor via `add_theme_support`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Add custom colors to the WordPress editor via `add_theme_support` | |
* | |
* This takes the existing colours (if your theme or a plugin has set any) | |
* and combines them with a new set of colours defined in the function | |
* | |
* New colours should be visible in the various color pickers found in | |
* WordPress admin | |
*/ | |
function add_custom_colors_to_palette() { | |
// Get the existing colors if the theme supports it | |
$existing_colors = get_theme_support( 'editor-color-palette' ); | |
// Set custom colors | |
$additional_colors = array( | |
array( | |
'name' => esc_html__( 'Custom Color 1', 'twentytwentyone' ), | |
'slug' => 'custom-color-1', | |
'color' => '#aa5555', | |
), | |
array( | |
'name' => esc_html__( 'Custom Color 2', 'twentytwentyone' ), | |
'slug' => 'custom-color-2', | |
'color' => '#55aa55', | |
), | |
array( | |
'name' => esc_html__( 'Custom Color 3', 'twentytwentyone' ), | |
'slug' => 'custom-color-3', | |
'color' => '#5555aa', | |
) | |
); | |
// Merge if there are existing colors | |
if ( isset( $existing_colors[0] ) ) { | |
$all_colors = array_merge( $existing_colors[0], $additional_colors ); | |
} else { | |
$all_colors = $additional_colors; | |
} | |
// Set the colors again | |
add_theme_support( | |
'editor-color-palette', | |
$all_colors | |
); | |
} | |
// Change priority to 20 to hook in later | |
add_action( 'after_setup_theme', 'add_custom_colors_to_palette', 20 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Adding priority 20 was the clincher. I was panicking thinking that👍
add_theme_support( 'editor-color-palette', $did_not_take_a_variable_or_callback );
Glad that it does! Thanks for this