Is there a away to set the gutenberg block-editor color palette other than in the theme?
1 Answer
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/
-
So the docu says not to use
init
butafter_setup_theme
, still in your code you don't. Why?– keroCommented Jan 22, 2021 at 9:44 -
1Thanks 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