Default WordPress Editor Color Palette

Setting up a Custom Palette in Gutenberg

While there’s been a lot written about the new editing experience that came out with WordPress v5.0 last month, I want to give a reminder of some of the neat features for end users. One of the best things about the new editor is that a theme or plugin can add or remove features from the editor with simple hooks, allowing you to craft an experience that fits your needs.

As an example, I have taken a few client sites that have embraced the new editor, and used their style guides to add their branding colors, fonts, and variants into the page editor. Now, when they want to add a block of content with a colored background or change the color of a button on a page, they have their palette of brand-approved colors already set to use. No need to remember hex codes or anything confusing!

Sounds great! How do I set up a custom color palette?

Default WordPress Editor Color Palette
Notice that the editor will warn you if your background and text colors aren’t high contrast. This makes it a bit easier to keep your content accessible!

By default the editor will have a palette of 11 colors, plus a color picker to get a different color. You can swap to a palette of your own by adding some code to your theme. Place the following in your functions.php file or where appropriate based on your structure. Next, we’ll modify it to fit our needs.

This code came directly from the Gutenberg Theme Support Handbook, a good resource for all WordPress developers.

function mytheme_setup_theme_supported_features() {
    add_theme_support( 'editor-color-palette', array(
        array(
            'name' => __( 'strong magenta', 'themeLangDomain' ),
            'slug' => 'strong-magenta',
            'color' => '#a156b4',
        ),
        array(
            'name' => __( 'light grayish magenta', 'themeLangDomain' ),
            'slug' => 'light-grayish-magenta',
            'color' => '#d0a5db',
        ),
        array(
            'name' => __( 'very light gray', 'themeLangDomain' ),
            'slug' => 'very-light-gray',
            'color' => '#eee',
        ),
        array(
            'name' => __( 'very dark gray', 'themeLangDomain' ),
            'slug' => 'very-dark-gray',
            'color' => '#444',
        ),
    ) );
}

add_action( 'after_setup_theme', 'mytheme_setup_theme_supported_features' );
Code language: PHP (php)

There’s a lot of code there, but not a lot to break down. First, remember that after_setup_theme is a hook, on which you add the function mytheme_setup_theme_supported_features that you’re creating. In that function we’re using add_theme_support, a built in WordPress function, where we’re using editor-color-palette to set our palette up.

We’re adding an array of colors, and each element of that array is itself an array. Within those nested arrays we have the name of the color, which we’re making translatable with the __() function, and setting the textdomain of our theme. Change themeLangDomain to whatever matches your theme. This name is a descriptor for when you hover over it in the palette.

The slug is a string of how you’ll refer to the color elsewhere in your code. The color is the hexadecimal value of the color that you want in your palette. With the above code, you’ve got a new editor palette with four colors that you’ve set, along with the color picker.

Our custom WordPress editor color palette
Our four custom colors now appear, along with the color picker

Adding to Our Palette

There are a few more features of the editor color palette that I’d like to show off, including targeting blocks in CSS, Customizer set colors, and removing the color picker.

Using our Color Palette Selections in CSS

If you’re editing text with the color palette you shouldn’t have to make any other changes. But what if you want to use the color selection in something a bit more customized, or in your own block type?

The slug that we added to our colors in the example above lets us target for both background and text colors. We don’t even need to use the color set in the editor, but something custom to our needs. For example, you may want a specific background or text color when you use the strong magenta color. In that case, here’s the CSS that can target the classes added when we use that color:

.has-strong-magenta-background-color {
    background-color: #313131;
}

.has-strong-magenta-color {
    color: #f78da7;
}Code language: CSS (css)

Setting a Color Palette with the Customizer

The twentynineteen theme that comes with WordPress has a custom palette that includes colors that can be set in the Customizer. This means that you can set your own primary and secondary color from the WordPress dashboard, without changing code!

array(
	'name'  => __( 'Primary', 'twentynineteen' ),
	'slug'  => 'primary',
	'color' => twentynineteen_hsl_hex( 'default' === get_theme_mod( 'primary_color' ) ? 199 : get_theme_mod( 'primary_color_hue', 199 ), 100, 33 ),
),
array(
	'name'  => __( 'Secondary', 'twentynineteen' ),
	'slug'  => 'secondary',
	'color' => twentynineteen_hsl_hex( 'default' === get_theme_mod( 'primary_color' ) ? 199 : get_theme_mod( 'primary_color_hue', 199 ), 100, 23 ),
),Code language: PHP (php)

The new color is now set as the output of a function that will get a theme mod, if you’ve modified the color. If not, it’ll return the default, ensuring that there’s always a color set.

The WordPress customizer with a primary color selection

Removing the Color Picker

You can also do things like disable the color picker, to ensure that users can only use the colors that you have preset for them. Doing so requires just one line of code in your functions file:

add_theme_support( 'disable-custom-colors' );Code language: PHP (php)

With that single line we’ve made it so the beautiful design that we’ve worked so hard to craft and the branding style guide that we have had to constantly review will always be set the way that we want.

Wrapping Up

As you can see, there’s a lot that you can do to change how users edit content in the Gutenberg editor, without having to add a tremendous amount of code.

This is only the beginning, and even more developer and user friendly features like this already exist or are coming to the editor and the rest of WordPress. I’m excited for the new opportunities this gives to all stakeholders of a site, from designers and developers, to admins and editors, all the way to customers and visitors. Let’s keep making WordPress better for everyone!

19 comments on “Setting up a Custom Palette in Gutenberg

Comment navigation

  1. I love this! Sometimes clients go rogue and want to add all the colors of the rainbow to their sites, instead of sticking to brand colors. This is a great way to rein them in.

  2. I found this REALLY helpful, thank you. Quick followup- would be excellent to be able to add custom bg to this via a repeating png bg. So you can pick from colors or bg pattern. Make sense?

  3. I optimized the color setup with a scss mixin (bootstrap 4 compatible):

    $theme-colors: (“primary”: #efefef,
    “secondary”: #000
    );

    @mixin gen-props($prefix, $postfix, $property) {

    @each $color-name,
    $color in $theme-colors {
    .#{$prefix}-#{$color-name}-#{$postfix} {
    #{$property}: $color
    }
    }
    }

    @include gen-props(‘has’, ‘color’, ‘color’);
    @include gen-props(‘has’, ‘background-color’, ‘background’);

  4. This is great, thanks!
    Is it also possible to add colors to the default palette rather than overwriting the palette completely?

    1. Excellent question!

      Not from what I’ve been able to see, but this is worth raising as an idea for development.

      Right now I suppose you could copy the existing colors to the palette, though this isn’t really the fix that you’re looking for.

  5. Is there a way to define a custom text color palette and a separate background color palette, or to force users to use certain combinations? E.g. I don’t want clients using black text on a blue button, nor do I want them using light orange on a white background. How can that be controlled? Is it just by forcing the CSS?

    1. I’m sorry, I’m not aware of a way to do this currently. You can remove the color selector, so that users can’t choose custom colors. It’s not the same as forcing a specific pairing, but it can at least limit the amount of poor choices that they can make. You can also force the CSS, but I realize that’s not ideal.

      There’s also a feature in the block editor that warns users if there isn’t sufficient contrast in their pairings between text and background colors. While this isn’t the same as enforcing it, hopefully that can help.

Reposts

  • SeriousWP
  • Andrew Lima
  • Deborah Edwards-Onoro
  • Josh Pollock 🌋

Mentions

  • FixUpFox

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.