> For the complete documentation index, see [llms.txt](https://whoisandywhite.gitbook.io/porterwp/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://whoisandywhite.gitbook.io/porterwp/developer-api/block-helper-api.md). Agents can also search and read this documentation through its [MCP server](https://whoisandywhite.gitbook.io/porterwp/~gitbook/mcp).

# Block and configuration helpers

PorterWP exposes `Porter_Blocks_Trait` for the small amount of repeated setup used by theme-owned ACF blocks. Existing themes may also use `porterBlockHelpers`, a compatibility class that contains the same trait. The older `traitPorterBlockHelpers` trait name is also retained as an alias for existing block classes; new code should use `Porter_Blocks_Trait`.

These APIs are available after PorterWP has initialized. The `porterBlockHelpers` class is loaded with the premium ACF custom-block subsystem, so new theme block files should prefer the trait-based factory shown below.

## Create a helper for one block

Set `dir` to the directory containing the block's `block.json`:

```php
namespace Theme\Testimonial;

function helpers() {
    static $helper;

    if ( ! isset( $helper ) ) {
        $helper = new class {
            use \Porter_Blocks_Trait;
        };
        $helper->dir = __DIR__;
    }

    return $helper;
}
```

Keeping one instance avoids repeatedly constructing the helper. Existing code that uses the compatibility class remains valid while the premium ACF block integration is loaded:

```php
$helper = new \porterBlockHelpers();
$helper->dir = __DIR__;
```

## Per-block methods

| Method                                          | Result                                                                                                      |
| ----------------------------------------------- | ----------------------------------------------------------------------------------------------------------- |
| `get_json()`                                    | Decodes this directory's `block.json` as an object.                                                         |
| `path()`                                        | Returns the block directory relative to the active stylesheet directory, suitable for a template-part slug. |
| `meta( 'name' )`                                | Returns the complete block name, such as `acf/testimonial`.                                                 |
| `meta( 'name', true )`                          | Returns the name without its namespace, such as `testimonial`.                                              |
| `meta( 'title' )`                               | Returns the title from `block.json`.                                                                        |
| `key( $key = '', $prepend = '', $append = '' )` | Builds the legacy, block-name-based key used by PHP-registered ACF field groups.                            |
| `anchor( $block )`                              | Returns an escaped `id="..."` attribute when the ACF block has an anchor, or an empty string.               |
| `build_block_class_attrs( $attrs )`             | Serializes an array as preview attributes. See the output-safety note below.                                |

For example, a block named `acf/testimonial` can use the same metadata for its render callback and PHP field group:

```php
$args = [
    'anchor' => helpers()->anchor( $block ),
];

get_template_part( helpers()->path() . '/template', '', $args );

$field_group = [
    'key'      => helpers()->key(),
    'title'    => helpers()->meta( 'title' ),
    'location' => [
        [
            [
                'param'    => 'block',
                'operator' => '==',
                'value'    => helpers()->meta( 'name' ),
            ],
        ],
    ],
    'fields'   => [
        [
            'key'   => helpers()->key( 'quote' ),
            'name'  => 'quote',
            'label' => 'Quote',
            'type'  => 'textarea',
        ],
    ],
];
```

`key()` preserves Porter's established key format for existing field groups. For `acf/testimonial`, `key()` returns `testimonial` and `key( 'quote' )` returns `testimonial__quote`. Use fixed developer-controlled segments so a field's key stays stable after deployment.

### Output safety

`anchor()` escapes the anchor value before returning its complete attribute. The values returned by `meta()`, `path()` and `key()` are data rather than ready-to-print HTML; escape them for their eventual output context.

`build_block_class_attrs()` is a legacy preview helper and does not escape attribute names or values. Pass only fixed, trusted attributes that have already been prepared for HTML output. Never pass request data or unescaped ACF values to it. On the frontend, use WordPress's `get_block_wrapper_attributes()` instead.

## Block discovery methods

The trait also contains the methods PorterWP's registrar uses to resolve block discovery settings:

| Method                  | Result                                                                                            |
| ----------------------- | ------------------------------------------------------------------------------------------------- |
| `get_block_path()`      | Configured `blocks.settings.path`, defaulting to `/porter/blocks`, after `porter_block_path`.     |
| `get_block_folders()`   | Configured `blocks.settings.folders`, defaulting to an empty array, after `porter_block_folders`. |
| `get_block_scan_dirs()` | The configured folders with leading slashes, or `[ '/' ]` when the block root is scanned.         |

These methods call `porter_get_settings()`. A custom registrar must therefore compose both traits; the per-block helper above does not need them:

```php
$registrar = new class {
    use \Porter_Config_Trait;
    use \Porter_Blocks_Trait;
};

$directories = $registrar->get_block_scan_dirs();
```

Most themes should configure discovery through [`blocks.json`](https://whoisandywhite.gitbook.io/porterwp/getting-started/configuration-files/blocks-json.md) instead of calling these methods directly.

## Read Porter configuration from theme PHP

For a one-off trusted configuration read, `porter_utility_helpers()` returns an object using `Porter_Config_Trait`:

```php
$taxonomies = porter_utility_helpers()->porter_get_settings(
    [ 'taxonomies' ],
    'taxonomies.json'
);
```

The public method signature is `porter_get_settings( $path = [], $file = 'porter.json' )`.

An empty path returns the decoded file. A nested path returns its value, or `false` when that path does not exist. An unreadable file returns `null`. Configuration paths pass through `porter_wp_config_filepath`, and the normal theme-aware caching rules still apply.

Existing theme classes may continue to `use Porter_Config_Trait` directly. For code that only needs to read a value, the factory keeps the dependency explicit and avoids adding the trait's lower-level cache and utility methods to the theme class's public surface.

See [Configuration files](https://whoisandywhite.gitbook.io/porterwp/getting-started/configuration-files.md) for the supported file schemas and [Caching and environments](https://whoisandywhite.gitbook.io/porterwp/features/caching.md) for cache behavior.
