Skip to content

Instantly share code, notes, and snippets.

@jslyonnais
Last active January 6, 2022 16:46
Show Gist options
  • Save jslyonnais/49a7e5fce15cc38e90cb5cfdfc960300 to your computer and use it in GitHub Desktop.
Save jslyonnais/49a7e5fce15cc38e90cb5cfdfc960300 to your computer and use it in GitHub Desktop.
WordPress + ACF - Register fields via PHP
<?php
add_action( 'acf/init', 'register_allpage_fields' );
function register_allpage_fields() {
$slug = "allpage";
$fields = [
[
'key' => 'page_subtitle',
'label' => 'Subtitle',
'name' => 'page_subtitle',
'type' => 'text',
],
];
acf_add_local_field_group([
'key' => "group_$slug",
'title' => 'Options',
'position'=> 'acf_after_title',
'label_placement' => 'left',
'fields' => $fields,
'location' => [
[
[
'param' => 'post_type',
'operator' => '==',
'value' => "page",
]
]
]
]);
}
?>
<?php
/**
* Import Custom Fields
*
* @see Import any custom acf fields in `includes/custom-fields` folder
* @warning Required ACF Pro to be installed!
*/
foreach (glob(get_template_directory() . '/includes/custom-fields/*.php') as $file) {
$filename = basename($file);
require_once locate_template('/includes/custom-fields/' . $filename);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment