Add Brand Colors to the Editor Palette
Adds your brand colors to the color palette in the block editor, next to the theme's colors, so the exact shades are one click away.
Opens WPIDE on
Code
// Colors to add: slug, name and hex value.
$wpide_brand_colors = array(
array( 'slug' => 'brand', 'name' => 'Brand', 'color' => '#2271b1' ),
array( 'slug' => 'brand-dark', 'name' => 'Brand dark', 'color' => '#135e96' ),
array( 'slug' => 'accent', 'name' => 'Accent', 'color' => '#d63638' ),
);
add_filter( 'wp_theme_json_data_theme', function ( $theme_json ) use ( $wpide_brand_colors ) {
$data = $theme_json->get_data();
$palette = isset( $data['settings']['color']['palette'] ) && is_array( $data['settings']['color']['palette'] ) ? $data['settings']['color']['palette'] : array();
$palette = isset( $palette['theme'] ) && is_array( $palette['theme'] ) ? $palette['theme'] : $palette;
$by_slug = array();
foreach ( array_merge( $palette, $wpide_brand_colors ) as $color ) {
if ( isset( $color['slug'], $color['color'] ) ) {
$by_slug[ $color['slug'] ] = $color;
}
}
return $theme_json->update_with( array(
'version' => 3,
'settings' => array(
'color' => array(
'palette' => array_values( $by_slug ),
),
),
) );
} );
Notes
Colors with a slug the theme already uses replace the theme color. Each color also becomes a CSS variable such as --wp--preset--color--brand.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.