Attributes - Filtering

Last modified:

Blockstudio provides two methods to filter block attributes.

In Editor

The first method filters the attributes in the editor. This is useful if you want to adjust the default value of an attribute or its conditions.

functions.php

            add_filter('blockstudio/blocks/attributes', function ($attribute, $block) {
      if (isset($attribute['id']) && $attribute['id'] === 'lineNumbers') {
        $attribute['default'] = true;
        $attribute['conditions'] = [
          [
            [
              'id' => 'language',
              'operator' => '==',
              'value' => 'css',
            ],
          ],
        ];
      }

      return $attribute;
    }, 10, 2);

      
Copy

The code above will set the default value of the lineNumbers attribute to true and will hide the attribute if the language attribute is not set to css. Keep in mind that this filter is only evaluated when inserting blocks in the editor.

On Frontend

The second method filters the attributes on the frontend. This is useful if you want to adjust the attributes before they are passed to the block template.

functions.php

            add_filter('blockstudio/blocks/attributes/render', function ($value, $key, $block) {
      if (
        $key === 'copyButton' &&
        $block['name'] === 'blockstudio-element/code'
      ) {
        $value = true;
      }

      return $value;
    }, 10, 3);

      
Copy

Keep in mind that the above filter will override any values set in the editor.