Code Snippet: Oxygen Colors in Gutenberg




Oxygen is a powerful plugin for WordPress that deactivates the WordPress theme, supports the creation of own templates with a visual builder and thus enables a fully individual design for your website.

Within the Oxygen Builder you can define global colors, which are then available in all color selection fields of the Builder.
However, the Gutenberg Editor offers its own color palette, which does not necessarily match the design of the website.


My code snippet replaces the Gutenberg color palette with the Oxygen color palette.

Gutenberg color palette
Oxygen color palette


The code snippet is available for download here:

Version 1.0.0, 2020-12-29

For installation and use of the downloaded JSON file you will need the plugin Code Snippets.
You can install the JSON file using the "Import" function of the plugin. 
Don't forget to activate the snippet after import.

Alternative: At the end of this page you can view and copy the complete source code of the snippet.

New functionalities and bug fixes are documented in the change log.


There are two configuration options in the code snippet. These can be changed in the configuration section (~ line 25).

true: Adds black and white
false: Does not add black and white

: Allow custom colors to be defined
false: The link for custom colors is not displayed.


I have developed and tested the code snippet to the best of my knowledge under WordPress 5.6 and Oxygen 3.6.1. 
I provide the code snippet for free use.
I cannot give any guarantee for the functionality because of the countless possible variations in WordPress environments.
Download and use of this code snippet is at your own risk and responsibility.

Change Log

  • 2020-12-29
    • Start of development
  • Version 1.0.0 (2020-12-29)
    • Initial release

Source Code

Plugin Name:  MA Oxygen Colors Gutenberg
Description:  Provide Oxygen colors in Gutenberg
Author:       <a href="">Matthias Altmann</a>
Version:      1.0.0
Plugin URI:
Description:  en:
Copyright:    © 2020, Matthias Altmann

Version History:
Date		Version		Description
2020-12-29				Development start
2020-12-29 	1.0.0		Initial Release 


class MA_Oxygen_Colors_Gutenberg {
    const TITLE     	= 'MA Oxygen Colors Gutenberg';
	const SLUG	    	= 'ma_oxygen_colors_gutenberg';
    const VERSION   	= '1.0.0';
    // Configuration
	private static $debug				= false;    // caution! may produce a lot of output
    private static $timing				= false;    // caution! may produce a lot of output
    private static $add_black_white     = true;     // automatically add black and white colors
    private static $allow_custom_color  = true;     // allow custom color settings in Gutenberg

	static function init() {
        if (	!wp_doing_ajax() 														// skip for ajax
            && 	!wp_doing_cron()														// skip for cron
            && 	(!array_key_exists('heartbeat',$_REQUEST))								// skip for hearbeat
        ) {
            global $wp;
            add_action('after_setup_theme', [__CLASS__, 'palette_backend']);
            add_action('enqueue_block_assets', [__CLASS__, 'palette_frontend']);
    // Add Oxygen's global colors to Gutenberg's backend editor palette
    static function palette_backend() {
        $st = microtime(true);

        $gutenberg_colors = [];
        if (self::$add_black_white) {
            // add black and white
            $gutenberg_colors[] = [ 'name' => 'black', 'slug' => 'color-black', 'color' => '#000000' ];
            $gutenberg_colors[] = [ 'name' => 'white', 'slug' => 'color-white', 'color' => '#ffffff' ];
        // add oxygen global colors
        $oxy_colors = oxy_get_global_colors();
        foreach($oxy_colors['colors'] as $oxy_color) {
            $gutenberg_colors[] = [ 'name' => $oxy_color['name'], 'slug' => 'color-'.$oxy_color['id'], 'color' => $oxy_color['value'] ];
        add_theme_support( 'editor-color-palette', $gutenberg_colors );
        if (!self::$allow_custom_color) {
            add_theme_support( 'disable-custom-colors' );

        if (WP_DEBUG && self::$debug) {error_log(sprintf('%s::%s() gutenberg_colors: %s',self::TITLE,__FUNCTION__,print_r($gutenberg_colors,true)));}
		$et = microtime(true);
		if (WP_DEBUG && self::$timing) {error_log(sprintf('%s::%s() Timing: %.5f sec.',self::TITLE,__FUNCTION__,$et-$st));}

    // Add corresponding CSS to frontend Gutenberg blocks
    static function palette_frontend(){
        $st = microtime(true);

        $gutenberg_colors_frontend_css = "";
        if (self::$add_black_white) {
            // add black and white
            $gutenberg_colors_frontend_css .= '.has-color-black-color{color:#000000;}';
            $gutenberg_colors_frontend_css .= '.has-color-black-background-color{background-color:#000000;}';
            $gutenberg_colors_frontend_css .= '.has-color-white-color{color:#ffffff;}';
            $gutenberg_colors_frontend_css .= '.has-color-white-background-color{background-color:#ffffff;}';
        // add oxygen global colors
        $oxy_colors = oxy_get_global_colors();
        foreach( $oxy_colors['colors'] as $oxy_color) {
            $gutenberg_colors_frontend_css .= '.has-color-'.$oxy_color['id'].'-color {color:'.$oxy_color['value'].'}';
            $gutenberg_colors_frontend_css .= '.has-color-'.$oxy_color['id'].'-background-color{background-color:'.$oxy_color['value'].'}';
        wp_register_style('gutenberg-oxygen-colors', false );
        wp_add_inline_style('gutenberg-oxygen-colors', $gutenberg_colors_frontend_css );

        if (WP_DEBUG && self::$debug) {error_log(sprintf('%s::%s() gutenberg_colors_frontend_css: %s',self::TITLE,__FUNCTION__,print_r($gutenberg_colors_frontend_css,true)));}
		$et = microtime(true);
		if (WP_DEBUG && self::$timing) {error_log(sprintf('%s::%s() Timing: %.5f sec.',self::TITLE,__FUNCTION__,$et-$st));}