How to Create Custom Color Palettes in WordPress Gutenberg

In this tutorial you’ll learn how to create custom color palettes for WordPress Gutenberg, making your theme even more flexible.
Color palettes are collections of swatches in the editor’s sidebar which allow you to choose from predefined colors and ap…


This content originally appeared on Envato Tuts+ Tutorials and was authored by Adi Purdila

In this tutorial you’ll learn how to create custom color palettes for WordPress Gutenberg, making your theme even more flexible.

Color palettes are collections of swatches in the editor’s sidebar which allow you to choose from predefined colors and apply them to elements on your page. There’s also a color picker tool to select whichever color you like (we’ll be removing this option):

gutenberg palettesgutenberg palettesgutenberg palettes

Using these tools you can change the appearance of text, background, UI components like buttons, and much more, all from within the Gutenberg page editor. But how can you add your own colors to these palettes? And how might you limit what users are able to pick themselves? Let’s find out!

Custom Color Palettes in WordPress Gutenberg

1. Disable the Custom Color Picker

When you’re editing certain elements in the Gutenberg editor you’ll notice, underneath the defined color swatches, a Custom color option. When you click this a color picker will open, allowing you to choose any color you can possibly think of.

custom color picker in gutenbergcustom color picker in gutenbergcustom color picker in gutenberg

As a theme developer this is a dangerous option to give users! Allowing this kind of stylistic freedom can result in the website content going way beyond any color schemes you’ve defined in your theme. Users may pick colors that clash with your theme, colors that make reading difficult (an accessibility issue), or pick a huge range of colors and ruin any continuity on the website.

So, for this first step, we’re going to remove this custom color picker altogether. 

Add Snippet to Your Theme’s Functions.php

Find your theme’s functions.php file. It will be in wp-content > themes > your-theme or wp-content > themes > your-child-theme.

In this example I’m using the twentytwentyone theme which comes with each new installation of WordPress at the moment. Opening up the functions.php file there’s a function called

Before the closing braces of that function we add the following line of code:

Note: it might well be that your functions.php doesn’t have this kind of setup function, but it’s important you place this line of code somewhere which is called by WordPress. In our case, after the setup function, there’s an add_action to do that.

That’s it! If you go back to the Gutenberg editor and hit refresh, you’ll see that Custom color option has been removed.

2. Define Your Own Color Palette

To add our own colors to the available palettes we go back to our functions.php.

Back in Functions.php

In the same function we were using before, above the snippet where we disable the custom color picker, we can create an array of color values.

Again, in the twentytwentyone theme, we’ll can already see a bunch of defined colors within the twenty_twenty_one_setup() function. They’re defined in another function which looks like:

The array here is the array of colors, and each color is itself defined with an array of name:value pairs. Here’s what the existing code in twentytwentyone looks like:

Color Arrays

For each color you want to have in your palette you’d add an array to this structure. Each array has:

  • name: the name you want to call your color, a human-readable label (include text domain for proper translation)
  • slug: how it’s referred within the CSS code (more on this in a moment)
  • color: the actual color hex value

In the example above you’ll notice the color values are using a PHP variable (eg: $dark_gray). This isn’t compulsory, but you may find it makes sense to define your colors in variables, in case they’re used elsewhere in your code. The twentytwentyone theme uses a collection of variables like this:

Let’s Add Our Own Color

Let’s add our own by first defining a color variable (this is a deep red color):

Then we’ll add an array to the existing list (in your case you may have had to write the editor-color-palette function yourself):

Now, if you refresh the Gutenberg editor, you’ll see a color swatch has been added to the palette (both text colors and background colors) and you can use it on your content!

our custom color in the gutenberg paletteour custom color in the gutenberg paletteour custom color in the gutenberg palette

Mission accomplished! Well, not quite.

3. Write the Corresponding CSS

You can use the color in your content on the back end, in the Gutenberg editor, but you won’t see it on the front end. This is because we now have to create a CSS rule to reflect it.

For example, if you go ahead and apply the “Adi Custom” color to some text, save it, then hit refresh on the front end, you won’t see any change. But if you inspect the code you’ll see a new class has been applied to the text you changed:

You’ll recognize the slug adicustom in amongst that class name, so all we have to do is define a couple of rules using that selector. We need to define one for when text color is used, and another for when background color is used, so we do that in our theme’s style.css file. For example:

For an extra layer of control you may want to use custom CSS variables for this step. Check out my short course to learn all about them!

front end CSSfront end CSSfront end CSS

Key Takeaways

And we’re done! That simple trick will give you, as a theme developer, a little more control over how your theme is used. Let’s go over what we learned:

  1. Limiting the available colors in Gutenberg allows you to stay within branding guidelines and avoids accessibility issues.
  2. Disable the custom color picker and define your new palette within your theme’s functions.php file.
  3. Remember to write the corresponding CSS for the new colors inside your theme’s style.css file.

More WordPress Gutenberg Tutorials


This content originally appeared on Envato Tuts+ Tutorials and was authored by Adi Purdila


Print Share Comment Cite Upload Translate Updates
APA

Adi Purdila | Sciencx (2021-05-29T17:29:37+00:00) How to Create Custom Color Palettes in WordPress Gutenberg. Retrieved from https://www.scien.cx/2021/05/29/how-to-create-custom-color-palettes-in-wordpress-gutenberg/

MLA
" » How to Create Custom Color Palettes in WordPress Gutenberg." Adi Purdila | Sciencx - Saturday May 29, 2021, https://www.scien.cx/2021/05/29/how-to-create-custom-color-palettes-in-wordpress-gutenberg/
HARVARD
Adi Purdila | Sciencx Saturday May 29, 2021 » How to Create Custom Color Palettes in WordPress Gutenberg., viewed ,<https://www.scien.cx/2021/05/29/how-to-create-custom-color-palettes-in-wordpress-gutenberg/>
VANCOUVER
Adi Purdila | Sciencx - » How to Create Custom Color Palettes in WordPress Gutenberg. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/05/29/how-to-create-custom-color-palettes-in-wordpress-gutenberg/
CHICAGO
" » How to Create Custom Color Palettes in WordPress Gutenberg." Adi Purdila | Sciencx - Accessed . https://www.scien.cx/2021/05/29/how-to-create-custom-color-palettes-in-wordpress-gutenberg/
IEEE
" » How to Create Custom Color Palettes in WordPress Gutenberg." Adi Purdila | Sciencx [Online]. Available: https://www.scien.cx/2021/05/29/how-to-create-custom-color-palettes-in-wordpress-gutenberg/. [Accessed: ]
rf:citation
» How to Create Custom Color Palettes in WordPress Gutenberg | Adi Purdila | Sciencx | https://www.scien.cx/2021/05/29/how-to-create-custom-color-palettes-in-wordpress-gutenberg/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.