Hooks - PHP

Last modified:

Below you'll find a list of all available PHP hooks that can be used to extend or adjust the functionality of the plugin.

Path

This filter allows you to adjust the path of the block folder. By default, this is blockstudio inside the currently active theme. Alternatively it is possible to create a new instance for multiple source directories.

functions.php

            add_action('blockstudio/path', function() {
      $path = get_stylesheet_directory() . '/blocks';
      return $path;
    });

      
Copy

Init

global

This action fires after the plugin has registered all blocks.

functions.php

            add_action('blockstudio/init', function() {
      // do something
    });

      
Copy

instance

This action fires after the plugin has registered all blocks of a specific instance.

functions.php

            add_action('blockstudio/init/$instance', function() {
      // do something
    });

      
Copy

global/before

This action fires before the plugin has registered all blocks.

functions.php

            add_action('blockstudio/init/before', function() {
      // do something
    });

      
Copy

global/before/instance

This action fires before the plugin has registered all blocks of a specific instance.

functions.php

            add_action('blockstudio/init/$instance', function() {
      // do something
    });

      
Copy

Blocks

render

This filter allows you to adjust the output of a block before it is rendered.

functions.php

            add_filter('blockstudio/blocks/render', function ($value, $block, $isEditor, $isPreview) {
      if ($block->name === 'blockstudio/my-block') {
        $value = str_replace('%CONTENT%', 'Replace in frontend', $value);

        if ($isEditor) {
          $value = str_replace('%CONTENT%', 'Only replace in editor', $value);
        }
        if ($isPreview) {
          $value = str_replace('%CONTENT%', 'Only replace in preview', $value);
        }
      }

      return $value;
    }, 10, 4);

      
Copy

meta

This filter allows you to adjust the data of the block.json file before it is registered.

functions.php

            add_filter('blockstudio/blocks/meta', function ($block) {
      if (strpos($block->name, 'marketing') === 0) {
        $block->blockstudio['icon'] = '<svg></svg>';
      }

      return $block;
    });

      
Copy

The above code would change the icon of all blocks starting with marketing.

conditions

This filter allows you to add custom conditions which can be used within blocks.

functions.php

            add_filter('blockstudio/blocks/conditions', function ($conditions) {
      $conditions['purchasedProduct'] = serHasPurchasedProduct();

      return $conditions;
    });

      
Copy

attributes

This filter allows you to adjust the attributes of a block before the block is registered. See Filtering for more information.

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

attributes/render

This filter allows you to adjust the attributes of a block before it is rendered. See Filtering for more information.

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

attributes/populate

This filter allows you to add custom data to the options of a checkbox, select or radio field. See Populating options for more information.

functions.php

            add_filter('blockstudio/blocks/attributes/populate', function ($options) {
      $options['customData'] = [
        [
          'value' => 'custom-1',
          'label' => 'Custom 1',
        ],
        [
          'value' => 'custom-2',
          'label' => 'Custom 2',
        ],
        [
          'value' => 'custom-3',
          'label' => 'Custom 3',
        ],
      ];

      return $options;
    });

      
Copy

components/useblockprops/render

This filter allows you to adjust the output of the useBlockProps content.

functions.php

            add_filter('blockstudio/blocks/components/useblockprops/render', function ($attributes, $block) {
      if ($block->name === 'blockstudio/title') {
        $attributes['class'] = $attributes['class'] . ' new-class';
      }

      return $attributes;
    }, 10, 2);

      
Copy

components/innerblocks/render

This filter allows you to adjust the output of the <InnerBlocks /> content.

functions.php

            add_filter('blockstudio/blocks/components/innerblocks/render', function ($value, $block) {
      if ($block->name === 'blockstudio/columns') {
        $value = str_replace('%CONTENT%', 'replace', $value);
      }

      return $value;
    }, 10, 2);

      
Copy

components/innerblocks/frontend/wrap

This filter allows you to remove the <InnerBlocks /> wrapper from the frontend.

functions.php

            add_filter('blockstudio/blocks/components/innerblocks/frontend/wrap', function ($render, $block) {
      if ($block->name === 'blockstudio/my-block') {
        $render = false;
      }

      return $render;
    }, 10, 2);

      
Copy

components/richtext/render

This filter allows you to adjust the output of the <RichText /> content.

functions.php

            add_filter('blockstudio/blocks/components/richtext/render', function ($value, $block) {
      if ($block->name === 'blockstudio/title') {
        $value = str_replace('%CONTENT%', 'replace', $value);
      }

      return $value;
    }, 10, 2);

      
Copy

Settings

path

This filter allows you to adjust the path of the blockstudio.json file. By default, this is blockstudio.json inside the currently active theme.

functions.php

            add_filter('blockstudio/settings/path', function() {
      $path = get_stylesheet_directory() . '/settings';
      return $path;
    });

      
Copy

users/ids

This filter allows you to enable the editor for specific user IDs.

functions.php

                                                      add_filter('blockstudio/settings/users/ids', function() {
                return [1];
              });

      
Copy

users/roles

This filter allows you to enable the editor for specific user roles.

functions.php

                                                      add_filter('blockstudio/settings/users/roles', function() {
                return ["administrator","editor"];
              });

      
Copy

assets/enqueue

This filter allows you to enable/disable the enqueueing of assets in frontend and editor.

functions.php

                                            add_filter('blockstudio/settings/assets/enqueue', function() {
                return true;
              });

      
Copy

assets/minify/css

This filter allows you to enable/disable the minification of CSS.

functions.php

                                            add_filter('blockstudio/settings/assets/minify/css', function() {
                return false;
              });

      
Copy

assets/minify/js

This filter allows you to enable/disable the minification of JS.

functions.php

                                            add_filter('blockstudio/settings/assets/minify/js', function() {
                return false;
              });

      
Copy

assets/process/scss

This filter allows you to enable/disable the processing of SCSS in .css files.

functions.php

                                            add_filter('blockstudio/settings/assets/process/scss', function() {
                return false;
              });

      
Copy

assets/process/scssFiles

This filter allows you to enable/disable the processing of .scss files to CSS.

functions.php

                                            add_filter('blockstudio/settings/assets/process/scssFiles', function() {
                return true;
              });

      
Copy

editor/formatOnSave

This filter allows you to enable/disable the formatting of code upon saving.

functions.php

                                            add_filter('blockstudio/settings/editor/formatOnSave', function() {
                return false;
              });

      
Copy

editor/assets

This filter allows you to enqueue additional assets in the editor.

functions.php

                                                      add_filter('blockstudio/settings/editor/assets', function() {
                return ["my-stylesheet","another-stylesheet"];
              });

      
Copy

editor/markup

This filter allows you to add additional markup to the end of the editor.

functions.php

                                                                add_filter('blockstudio/settings/editor/markup', function() {
                return '<style>body { background: black; }</style>';
              });

      
Copy

library

This filter allows you to enable the block library.

functions.php

                                            add_filter('blockstudio/settings/library', function() {
                return false;
              });

      
Copy

Assets

process/scss/importPaths

This filter allows you to add additional paths to the @import statement of the SCSS compiler.

functions.php

            add_filter('blockstudio/assets/process/scss/importPaths', function() {
      $paths[] = get_template_directory() . '/src/scss/';
      return $paths;
    });

      
Copy

Render

global

This filter allows you to adjust the output of the page before it is being rendered.

functions.php

            add_action('blockstudio/render', function($output, $blocks) {
      if ($blocks['ui/heading']) {
        $output = str_replace('<h1', '<h1 class="text-4xl font-bold mb-4"', $output);
      }

      return $output;
    }, 10, 2);

      
Copy

This filter allows you to adjust the output of the <head> tag before it is being rendered.

functions.php

            add_action('blockstudio/render/head', function($output, $blocks) {
      if ($blocks['ui/heading']) {
        $output. = '<style>h1 { color: black; }</style>';
      }

      return $output;
    }, 10, 2);

      
Copy

This filter allows you to adjust the output of the </body> before it is being rendered.

functions.php

            add_action('blockstudio/render/footer', function($output, $blocks) {
      if ($blocks['ui/heading']) {
        $output. = '<style>h1 { color: black; }</style>';
      }

      return $output;
    }, 10, 2);

      
Copy

🍪 This website uses cookies to ensure you get the best experience on our website.

Accept Decline