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.
Opens WPIDE on
Code
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 add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.