How to add template color in custom block option in WordPress Gutenberg editor sidebar?


DeBoyoti Ghosh

Please see the attached image for reference.

I'm building a block based plugin for the Wordpress Gutenberg editor.

How can I add template colors (ie those black and pastel shades) toColorPalette ("Input border color") ?

The problem is that I don't want the PanelColorSettingsincluded toggle system ("color settings") .

The "input border color" will be part PanelBodyof the other settings that already exist using TextControl.

I did some googling and came across one called withColors- but that's just PanelColorSettings. So I don't think this can solve my requirement.

enter image description here

Walsh

The colors of the palette are stored in the block editor data. How do I retrieve colors from a theme when dealing with a similar problem using theme.json and a component I found by reading the Gutenberg source <ColorPalette>code . The key is to get the coloruseSelect('core/block-editor').getSettings().colors

Here is a simplified example block using theme colors :<ColorPalette>

import { registerBlockType } from '@wordpress/blocks';
import { ColorPalette, PanelBody } from '@wordpress/components';
import { useState } from '@wordpress/element';
import { useBlockProps, InspectorControls } from '@wordpress/block-editor';
import { useSelect } from '@wordpress/data';

registerBlockType('so-68876119/custom-colors', {
    title: 'Custom Colors',
    supports: {
        color: true
    },
    edit({ attributes, setAttributes }) {
        const MyColorPalette = () => {
                
            const [color, setColor] = useState(); // No default color set
                
            // Retrieve the themes color settings from the block editors' data
            const colors = useSelect('core/block-editor').getSettings().colors;
    
            return (
                <ColorPalette
                    colors={colors}
                    value={color}
                    onChange={(color) => setColor(color)}
                />
            );
        }
        return (
           <div {...useBlockProps()}>
                <InspectorControls>
                    <PanelBody title="Input border color">
                        <MyColorPalette />
                    </PanelBody>
                </InspectorControls>
                <h2>hello world</h2>
            </div>
        );
    },
    save() {
        return null;
    },
});

Related


How to Add Custom Links on the WordPress Admin Sidebar

dal How to add custom link on wordpress admin sidebar without using plugin? For example, I want to add a "Google.com" link. what should I do? I tried this: added next code in admin-bar.php function mycustomlink() { global $wp_admin_bar; $wp_admin_bar->add_menu

How to Add Custom Links on the WordPress Admin Sidebar

dal How to add custom link on wordpress admin sidebar without using plugin? For example, I want to add a "Google.com" link. what should I do? I tried this: added next code in admin-bar.php function mycustomlink() { global $wp_admin_bar; $wp_admin_bar->add_menu

How to Add Custom Links on the WordPress Admin Sidebar

dal How to add custom link on wordpress admin sidebar without using plugin? For example, I want to add a "Google.com" link. what should I do? I tried this: added next code in admin-bar.php function mycustomlink() { global $wp_admin_bar; $wp_admin_bar->add_menu

How to Add Custom Links on the WordPress Admin Sidebar

dal How to add custom link on wordpress admin sidebar without using plugin? For example, I want to add a "Google.com" link. what should I do? I tried this: added next code in admin-bar.php function mycustomlink() { global $wp_admin_bar; $wp_admin_bar->add_menu

How to Add Custom Links on the WordPress Admin Sidebar

dal How to add custom link on wordpress admin sidebar without using plugin? For example, I want to add a "Google.com" link. what should I do? I tried this: added next code in admin-bar.php function mycustomlink() { global $wp_admin_bar; $wp_admin_bar->add_menu

Restrict custom post type wordpress for Gutenberg Editor

Aplin Dev I need to create my own custom post type and want to limit the Gutenberg editor to my post type (only) and use the WordPress editor in this post type. How can I restrict this plugin for my cpt? thanks Kirill Merkushev You can use a filter to disable

Restrict custom post type wordpress for Gutenberg Editor

Aplin Dev I need to create my own custom post type and want to limit the Gutenberg editor to my post type (only) and use the WordPress editor in this post type. How can I restrict this plugin for my cpt? thanks Kirill Merkushev You can use a filter to disable

WordPress: Embed code with custom Gutenberg block

Giannis Dallas I'm trying to include a Custom HTML embed input in my Gutenberg block, but don't know how. I would like to know the same best way to add HTML in Custom HTML block is to add HTML in block. Am I missing the Gutenberg element? i tried attributes:{

Wordpress gutenberg custom block - preview loading issue

Jamie I created a plugin with custom blocks. I'm having trouble with the block preview. When I set the block to preview mode and reload the page editor, I get an error message on all the blocks previewedError loading block: You are probably offline. In the con

WordPress: Embed code with custom Gutenberg block

Giannis Dallas I'm trying to include a Custom HTML embed input in my Gutenberg block, but don't know how. I would like to know the same best way to add HTML in Custom HTML block is to add HTML in block. Am I missing the Gutenberg element? i tried attributes:{

Wordpress gutenberg custom block - preview loading issue

Jamie I created a plugin with custom blocks. I'm having trouble with the block preview. When I set the block to preview mode and reload the page editor, I get an error message on all the blocks previewedError loading block: You are probably offline. In the con

Wordpress gutenberg custom block - preview loading issue

Jamie I created a plugin with custom blocks. I'm having trouble with the block preview. When I set the block to preview mode and reload the page editor, I get an error message on all the blocks previewedError loading block: You are probably offline. In the con

WordPress: Embed code with custom Gutenberg block

Giannis Dallas I'm trying to include a Custom HTML embed input in my Gutenberg block, but don't know how. I would like to know the same best way to add HTML in Custom HTML block is to add HTML in block. Am I missing the Gutenberg element? i tried attributes:{

Wordpress gutenberg custom block - preview loading issue

Jamie I created a plugin with custom blocks. I'm having trouble with the block preview. When I set the block to preview mode and reload the page editor, I get an error message on all the blocks previewedError loading block: You are probably offline. In the con

Wordpress gutenberg custom block - preview loading issue

Jamie I created a plugin with custom blocks. I'm having trouble with the block preview. When I set the block to preview mode and reload the page editor, I get an error message on all the blocks previewedError loading block: You are probably offline. In the con

Wordpress Add onclick event inside Gutenberg block

james I've been searching around to find out how to add events in gutenberg blocks. I'm struggling to add an accordion system using ACF and Foundation. I have created a block, field and template using ACF. I want my users to be able to open and close the accor

Add Shadow to Image in WordPress Gutenberg Image Block

Andrew Tracker CSS .ts-learn-img figure:hover{ -moz-box-shadow: inset 0 0 20px #000000; -webkit-box-shadow: inset 0 0 20px #000000; box-shadow: inset 0 0 20px #000000; } HTML: <div class="wp-block-image ts-learn-img"> <figure class="aligncenter size

Wordpress Add onclick event inside Gutenberg block

james I've been searching around to find out how to add events in gutenberg blocks. I'm struggling to add an accordion system using ACF and Foundation. I have created a block, field and template using ACF. I want my users to be able to open and close the accor

Add Shadow to Image in WordPress Gutenberg Image Block

Andrew Tracker CSS .ts-learn-img figure:hover{ -moz-box-shadow: inset 0 0 20px #000000; -webkit-box-shadow: inset 0 0 20px #000000; box-shadow: inset 0 0 20px #000000; } HTML: <div class="wp-block-image ts-learn-img"> <figure class="aligncenter size

Add Shadow to Image in WordPress Gutenberg Image Block

Andrew Tracker CSS .ts-learn-img figure:hover{ -moz-box-shadow: inset 0 0 20px #000000; -webkit-box-shadow: inset 0 0 20px #000000; box-shadow: inset 0 0 20px #000000; } HTML: <div class="wp-block-image ts-learn-img"> <figure class="aligncenter size

Wordpress Add onclick event inside Gutenberg block

james I've been searching around to find out how to add events in gutenberg blocks. I'm struggling to add an accordion system using ACF and Foundation. I have created a block, field and template using ACF. I want my users to be able to open and close the accor

Add Shadow to Image in WordPress Gutenberg Image Block

Andrew Tracker CSS .ts-learn-img figure:hover{ -moz-box-shadow: inset 0 0 20px #000000; -webkit-box-shadow: inset 0 0 20px #000000; box-shadow: inset 0 0 20px #000000; } HTML: <div class="wp-block-image ts-learn-img"> <figure class="aligncenter size