How to create multiple meta fields in gutenberg block How to create multiple meta fields in gutenberg block wordpress wordpress

How to create multiple meta fields in gutenberg block


It would be better if you included the code that did not work. In any case, I changed your code by adding another text input and a textarea (with relevant entries in attributes and meta).

Here is the modified code. Also, I have changed some of the code to be more readable.

Javascript

( function( wp ) {    const el = wp.element.createElement;    const registerBlockType = wp.blocks.registerBlockType;    const TextControl = wp.components.TextControl;    const TextareaControl = wp.components.TextareaControl;    registerBlockType( 'dc-references-block/dc-references-block', {        title: 'Title',        icon: 'edit',        category: 'common',        attributes: {            blockValue: {                type: 'string',                source: 'meta',                meta: 'dc_references_block_field'            },            // Add two new attributes            name: {                type: 'string',                source: 'meta',                meta: 'dc_references_block_field_name'            },            desc: {                type: 'string',                source: 'meta',                meta: 'dc_references_block_field_desc'            }        },        edit: function( props ) {            const className = props.className;            const setAttributes = props.setAttributes;            // Original element with onChange event as an anonymous function            const text = el( TextControl, {                label: 'write here name of company',                value: props.attributes.blockValue,                key: 'companyName',                onChange: function( value ) {                    setAttributes( { name: value } );                }            } );            //Add two new elements            const secondText = el( TextControl, {                label: 'Write your name',                value: props.attributes.name,                key: 'username',                onChange: function( value ) {                    setAttributes( { name: value } );                }            } );            const textArea = el( TextareaControl, {                label: 'Write a description',                value: props.attributes.desc,                key: 'desc',                onChange: function( value ) {                    setAttributes( { desc: value } );                }            } );            return el(                'div',                { className: className },                // Children of the main div as an array                [ text, secondText, textArea ]            );        },        save: function() {            return null;        }    } );}( window.wp ) );

PHP

register_post_meta( 'page', 'dc_references_block_field', array(        'show_in_rest' => true,        'single' => true,        'type' => 'string',    ) );   // register two new meta corresponding to attributes in JS    register_post_meta( 'page', 'dc_references_block_field_name', array(        'show_in_rest' => true,        'single' => true,        'type' => 'string',    ) );    register_post_meta( 'page', 'dc_references_block_field_desc', array(        'show_in_rest' => true,        'single' => true,        'type' => 'string',    ) );