Blockstudio
Blocks

Storage

By default, all field values are stored as block attributes within the block comment in the post content. The storage feature allows you to additionally store values in post meta (wp_postmeta) or options (wp_options), making them accessible outside the block context.

Why Use Storage?

Block attributes are stored inside HTML comments in the post content, which means they're only accessible when parsing block content. By using additional storage locations, you can:

  • Query posts by field values using meta_query
  • Access values via REST API for headless setups
  • Share data with other plugins like SEO or caching plugins
  • Create global settings that persist across posts

Storage Types

TypeLocationScopeBest For
postMetawp_postmeta tablePer postQueryable post-specific data
optionwp_options tableGlobal (site-wide)Site settings, shared values

Block attribute storage (in the post content HTML comment) always happens automatically. The storage property adds additional storage locations - it doesn't replace block storage.

Configuration

Add the storage property to any field definition:

block.json
{
  "name": "blockstudio/example",
  "title": "Example Block",
  "blockstudio": {
    "attributes": [
      {
        "id": "subtitle",
        "type": "text",
        "label": "Subtitle",
        "storage": {
          "type": "postMeta",
          "postMetaKey": "my_subtitle"
        }
      }
    ]
  }
}

Storage Properties

PropertyTypeDescription
typestring | string[]Additional storage location(s): "postMeta", "option"
postMetaKeystringCustom meta key (default: {block_name}_{field_id})
optionKeystringCustom option key (default: {block_name}_{field_id})

Single vs Multiple Storage

// Add post meta storage
"storage": {
  "type": "postMeta"
}

// Add both post meta and option storage
"storage": {
  "type": ["postMeta", "option"]
}

How It Works

  1. Registration: When WordPress loads, Blockstudio registers meta keys and settings based on your field configurations
  2. Editing: Values are managed as block attributes in the editor (always stored in block)
  3. Saving: When the post is saved, values are synced to configured storage locations

Values are synced on every post save. If you update a value in the block editor and save, all storage locations are updated automatically.

Accessing Stored Values

In Block Templates

Inside your block template, field values are always available via $attributes:

index.php
<div>
  <h1><?php echo $a['subtitle']; ?></h1>
</div>
index.twig
<div>
  <h1>{{ a.subtitle }}</h1>
</div>

Post Meta (Outside Block Context)

Values stored in postMeta can be accessed anywhere in WordPress:

// Get meta value
$subtitle = get_post_meta( $post_id, 'my_subtitle', true );

// Query posts by meta value
$posts = get_posts([
  'meta_key'   => 'my_subtitle',
  'meta_value' => 'Hello World',
]);
{# Using Timber #}
{{ post.meta('my_subtitle') }}

{# Or via function #}
{{ function('get_post_meta', post.id, 'my_subtitle', true) }}

Post meta is also available via the REST API:

GET /wp-json/wp/v2/posts/123
# Response includes: { "meta": { "my_subtitle": "Hello World" } }

Options (Global Values)

Values stored in option are accessible site-wide:

$company_name = get_option( 'site_company_name' );
{{ function('get_option', 'site_company_name') }}

Examples

SEO Meta Fields

Store SEO data in post meta for compatibility with SEO plugins:

block.json
{
  "blockstudio": {
    "attributes": [
      {
        "id": "seo_title",
        "type": "text",
        "label": "SEO Title",
        "storage": {
          "type": "postMeta",
          "postMetaKey": "_yoast_wpseo_title"
        }
      },
      {
        "id": "seo_description",
        "type": "textarea",
        "label": "Meta Description",
        "storage": {
          "type": "postMeta",
          "postMetaKey": "_yoast_wpseo_metadesc"
        }
      }
    ]
  }
}

Add post meta storage to enable querying posts by field value:

block.json
{
  "blockstudio": {
    "attributes": [
      {
        "id": "featured",
        "type": "toggle",
        "label": "Featured",
        "storage": {
          "type": "postMeta",
          "postMetaKey": "is_featured"
        }
      }
    ]
  }
}

Query featured posts:

$featured_posts = get_posts([
  'meta_key'   => 'is_featured',
  'meta_value' => '1',
]);

Global Site Settings

Store site-wide configuration in options:

block.json
{
  "blockstudio": {
    "attributes": [
      {
        "id": "company_name",
        "type": "text",
        "label": "Company Name",
        "storage": {
          "type": "option",
          "optionKey": "site_company_name"
        }
      },
      {
        "id": "support_email",
        "type": "text",
        "label": "Support Email",
        "storage": {
          "type": "option",
          "optionKey": "site_support_email"
        }
      }
    ]
  }
}

Access anywhere on your site:

<footer>
  <p>&copy; <?php echo get_option( 'site_company_name' ); ?></p>
  <p>Contact: <?php echo get_option( 'site_support_email' ); ?></p>
</footer>

Post Meta and Option

For data that needs both post-level and global accessibility:

block.json
{
  "blockstudio": {
    "attributes": [
      {
        "id": "important_value",
        "type": "text",
        "label": "Important Value",
        "storage": {
          "type": ["postMeta", "option"],
          "postMetaKey": "important_meta",
          "optionKey": "important_option"
        }
      }
    ]
  }
}

Default Key Names

When you don't specify a custom key, Blockstudio generates one automatically:

Storage TypeDefault Key FormatExample
postMeta{block_name}_{field_id}blockstudio_example_subtitle
option{block_name}_{field_id}blockstudio_example_subtitle

The block name is sanitized (slashes replaced with underscores).

Custom keys are recommended for production use. Default keys may change if you rename your block.

On this page