1

Is there a away to set the gutenberg block-editor color palette other than in the theme?

1 Answer 1

2
+50

Yes, you have to use the init hook if you want to use add_theme_support from a plugin. I've tested the below code as a plugin and worked for me.

<?php

/* Plugin name: Add Color Palette
*/
function mytheme_setup_theme_supported_features()
{
    add_theme_support('editor-color-palette', array(
        array(
            'name' => esc_attr__('Strong magenta', 'themeLangDomain'),
            'slug' => 'strong-magenta',
            'color' => '#a156b4',
        ),
    ));
}

add_action('init', 'mytheme_setup_theme_supported_features');

Please consider avoiding using add_theme_support outside the theme's functions.php file.

The official description for the add_theme_support :

Must be called in the theme’s functions.php file to work. If attached to a hook, it must be ‘after_setup_theme’. The ‘init’ hook may be too late for some features.

https://developer.wordpress.org/reference/functions/add_theme_support/

2
  • So the docu says not to use init but after_setup_theme, still in your code you don't. Why?
    – kero
    Commented Jan 22, 2021 at 9:44
  • 1
    Thanks for asking, after_setup_theme hook doesn't work from a plugin to add support for editor-color-palette. Commented Jan 22, 2021 at 16:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.