acf/include_fields
Fires during ACF initialization after built-in field types and location rules have loaded. Intended for registering local field groups and fields through PHP.
This is the recommended place to call acf_add_local_field_group(), acf_add_local_field(), and similar field registration functions.
The hook is suitable when field groups:
- are stored in a theme or plugin;
- must be transferred between environments together with the code;
- must not be edited through the ACF interface;
- are created dynamically from PHP configuration.
ACF uses this hook in code generated by the field group export tool.
Unlike the acf/init event, this hook is specifically intended for including fields. acf/init fires later, after local fields, post types, and ACF taxonomies have been registered.
Official documentation: Register fields via PHP.
Usage
add_action( 'acf/include_fields', 'wp_kama_acf_include_fields_action' );
/**
* Function for `acf/include_fields` action-hook.
*
* @param int $ACF_FIELD_API_VERSION ACF_FIELD_API_VERSION The field API version.
*
* @return void
*/
function wp_kama_acf_include_fields_action( $ACF_FIELD_API_VERSION ){
// action...
}
- $field_api_version(int)
- ACF field API version. Current ACF versions pass the value of the
ACF_FIELD_API_VERSIONconstant.
Examples
#1 Register a field group
Add a text field to regular posts.
add_action( 'acf/include_fields', 'wpkama_register_post_fields' );
function wpkama_register_post_fields() {
acf_add_local_field_group(
[
'key' => 'group_post_details',
'title' => 'Post details',
'fields' => [
[
'key' => 'field_post_subtitle',
'label' => 'Subtitle',
'name' => 'post_subtitle',
'type' => 'text',
],
],
'location' => [
[
[
'param' => 'post_type',
'operator' => '==',
'value' => 'post',
],
],
],
]
);
}
#2 Get the field API version
The hook argument can be used when registration code depends on the ACF API version.
add_action( 'acf/include_fields', 'wpkama_register_fields', 10, 1 );
function wpkama_register_fields( $field_api_version ) {
if ( $field_api_version < 5 ) {
return;
}
acf_add_local_field_group(
[
'key' => 'group_page_settings',
'title' => 'Page settings',
'fields' => [],
'location' => [
[
[
'param' => 'post_type',
'operator' => '==',
'value' => 'page',
],
],
],
]
);
}Changelog
| Since 5.0.0 | Introduced. |
Where the hook is called
acf/include_fields
acf/acf.php 453
do_action( 'acf/include_fields', ACF_FIELD_API_VERSION );
Where the hook is used in Advanced Custom Fields PRO
acf/includes/admin/tools/class-acf-admin-tool-export.php 404
echo "add_action( 'acf/include_fields', function() {\r\n";
acf/includes/local-fields.php 743
add_action( 'acf/include_fields', '_acf_do_prepare_local_fields', 0, 1 );
acf/includes/local-json.php 65
add_action( 'acf/include_fields', array( $this, 'include_fields' ) );
acf/pro/acf-pro.php 63
add_action( 'acf/include_fields', array( $this, 'include_options_pages' ) );