# Expose a Custom Field in the REST API and Block Editor

> Registers a post meta field (a subtitle by default) so it is saved safely, available in the REST API and usable by the block editor and block bindings.

- Type: PHP
- Runs: Run everywhere
- Category: [Developer](https://wpide.com/library/category/developer/)
- Tags: rest-api, meta, custom-fields, block-editor
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/dev-register-meta-rest/

## Code

```php
add_action( 'init', function () {
    $post_type = 'post';            // Post type the field belongs to.
    $meta_key  = 'wpide_subtitle';  // Meta key.

    register_post_meta( $post_type, $meta_key, array(
        'type'              => 'string',
        'single'            => true,
        'default'           => '',
        'show_in_rest'      => true,
        'sanitize_callback' => 'sanitize_text_field',
        'auth_callback'     => function ( $allowed, $key, $post_id ) {
            return current_user_can( 'edit_post', $post_id );
        },
    ) );
} );
```

## Notes

Change $meta_key and $post_type. Only users who can edit the post can update the field.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Expose a Custom Field in the REST API and Block Editor" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
