Sunday, July 3, 2022
HomeWordPress Developmentplugin improvement - Setting international block attribute worth

plugin improvement – Setting international block attribute worth


I’ve a situation the place I should use the identical API key for all of the blocks that’s current and that will likely be created new. So when person inputs on one block via a type, it would replace on all of the blocks of the namespace immediately.

At the moment when register_block_type() and props.setAttributes methodology is used, the values for every attribute is exclusive for every of the block. I’ve seen the documentation. I could not see a regular approach for sharing the identical worth throughout blocks.

Register code:

registerBlockType("namespace", {
  title: "Plugin title",
  class: "widespread",
  attributes: {
    apiKey: { kind: "string" },

Dealing with submit from the shape inside InspectorControls:

<InspectorControls>
      <PanelBody>
        <type id="api-key-input" onSubmit={(e) => handleSubmit(e)}>
          <enter
            kind="password"
            ref={inputRef}
            defaultValue={props.attributes.apiKey}
          />
          <enter kind="submit" />
        </type>
 </InspectorControls>

perform handleSubmit(e) {
  e.preventDefault();
  props.setAttributes({ apiKey: inputRef.present.worth });
}

The strategies that I’ve checked out to date.

  1. Storing the attributes within the database utilizing update_option however I didn’t see any get_option for the Gutenberg block.
  2. wp.knowledge.subscribe approach of dispatching to the blocks required.
  3. Making a registry retailer and managing the general worth utilizing states.

Is there an best/most secure approach of doing this that I’m lacking, like setting an possibility or helps worth?

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments