Halloween Sale 15% off Ends in See the plans
PHP

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.

Don’t have WPIDE yet? Install it on your site

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

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.