• Resolved bw10

    (@bw10)


    Not sure if I am doing this correctly, but I wanted to set the defaults for a few of this plugin’s blocks in my theme.json.

    I know I can do that for core blocks like this:

    {
      "version": 2,
      "$schema": "https://schemas.wp.org/trunk/theme.json",
      "settings": {
        "appearanceTools": true,
        "border": {
          ...
        },
        "color": {
          ...
        },
        "layout": {
          "contentSize": "1600px",
          "wideSize": "100%"
        },
        "spacing": {
          ...
        },
        "typography": {
          ...
        },
        "blocks": {
          "core/heading": {
            "color": {
              "text": true,
              "background": false,
              "link": false,
              "palette": [
                {
                  "color": "#ffffff",
                  "slug": "white",
                  "name": "White"
                }, 
                {
                  "color": "#4f99bb",
                  "slug": "blue",
                  "name": "#4f99bb - Blue"
                }
              ]
            },
            "typography": {
    					"fontFamily": "var(--wp--preset--font-family--MINE)"
    				}
          }
        }
      },

    However, when I try to add in the areoi/container block like this (under “blocks”)

          "areoi/container": {
            "attributes": {
              "container": {
                "type": "string",
                "default": "container-xl"
              }
            }
          }

    It doesn’t work.

    Note: When I edit the default value in the blocks/container/block.json, it does work.

    Am I going about this the wrong way? Is there a PHP function instead to override the default value for this or any other block in this plugin?

    Thanks in advance!

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Miles

    (@areoimiles)

    Hi @bw10

    Thanks for reaching out. I’m not sure if you’re able to change blocks loaded from plugins in your theme.json file. Have you seen this ability for any other plugins?

    That being said there is a bit of PHP that you can add to your functions.php file that should do the trick.

    add_filter( 'block_type_metadata', function( $metadata ) {
    	if ( !empty( $metadata['name'] ) && $metadata['name'] == 'areoi/container' ) {
    		$metadata['attributes']['container']['default'] = 'container-xl';
    	}
    
    	return $metadata;
    } );

    You should be able to change the defaults for any block using the above method, just copy the IF statement and change the block name, attribute and value.

    Hopefully this solves your issue but if not please don’t hesitate to get back in touch.

    Miles

    Thread Starter bw10

    (@bw10)

    Thanks for this!

    I’ve been spending a lot of time lately learning how to build blocks from scratch.

    Curious, any plans on putting this onto GitHub? I’d love to propose/contribute some code. Sure others would too.

    Plugin Author Miles

    (@areoimiles)

    Hi @bw10

    Yes, eventually. In the coming months we’re going to be making a start on version 2 so we have a bit of work to do getting the plugin in a state where it is ready for contributions along with defining community guidelines etc.

    In the meantime, if you have suggestions feel free to open up a ticket and we’ll do our best to accommodate.

    Thanks

    Miles

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Set Defaults for AREOI blocks in my theme’s theme.json’ is closed to new replies.