# Register ACF Fields in Code

> A ready-to-edit example that registers an ACF field group (subtitle, summary and call-to-action link) for posts in PHP, so the fields are version-controlled and cannot be changed in the admin.

- Type: PHP
- Runs: Run everywhere
- Category: [Custom Post Types & Fields](https://wpide.com/library/category/post-types/)
- Tags: acf, fields, developer
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/acf-register-fields-in-code/

## Code

```php
add_action( 'acf/init', function () {
    if ( ! function_exists( 'acf_add_local_field_group' ) ) {
        return;
    }

    acf_add_local_field_group( array(
        'key'      => 'group_wpide_post_extras',
        'title'    => 'Post Extras',
        'fields'   => array(
            array(
                'key'   => 'field_wpide_subtitle',
                'label' => 'Subtitle',
                'name'  => 'subtitle',
                'type'  => 'text',
            ),
            array(
                'key'       => 'field_wpide_summary',
                'label'     => 'Summary',
                'name'      => 'summary',
                'type'      => 'textarea',
                'rows'      => 3,
                'maxlength' => 300,
            ),
            array(
                'key'           => 'field_wpide_cta',
                'label'         => 'Call to action',
                'name'          => 'cta',
                'type'          => 'link',
                'return_format' => 'array',
            ),
        ),
        'location' => array(
            array(
                array( 'param' => 'post_type', 'operator' => '==', 'value' => 'post' ),
            ),
        ),
        'position'      => 'normal',
        'style'         => 'default',
        'show_in_rest'  => 1,
    ) );
} );
```

## Notes

Keep every key unique and never change a key after content has been saved. Edit the location rule to target other post types.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Register ACF Fields in Code" 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/
