Code snippets

Last modified:

Although Blockstudios main focus is on custom blocks, its file-first approach is also suitable for code snippets.

Setup

To create a new code snippet, simply create a new folder and place an init.php file in it. Blockstudio will always execute that file. For more information on the init file, check the Initialization page.

Styles and scripts

Folders marked as code snippets will also process and enqueue styles and scripts. To enqueue global styles and scripts, use the global prefix for the asset name.

assets

init.php
global-styles.css
global-scripts.js

Assets inside code snippet folders also support all processing.

Custom directories

The above technique can also be used on directories outside of the folder where all blocks are stored. For example, let's say you want to store global assets like styles and scripts inside your theme. Simply create a new Blockstudio instance using the init method.

functions.php

            add_action('init', function () {
      Blockstudio\Build::init([
        'dir' => get_template_directory() . '/assets'
      ]);
    });

      
Copy

Now, simply follow the setup instructions inside the to register a code snippet inside the assets folder.