The ACF acf/add_field_group
filter allows you to modify or add custom fields to a field group before it is saved to the database. This filter is useful when you want to programmatically add or modify fields based on certain conditions or requirements.
Here’s an example of how you can use the acf/add_field_group
filter to add a custom field to a field group:
function wpsnippets_add_custom_field_to_field_group($field_group) {
// Add a custom field to the field group
$field = array(
'key' => 'my_custom_field',
'label' => 'My Custom Field',
'name' => 'my_custom_field',
'type' => 'text',
);
$field_group['fields'][] = $field;
return $field_group;
}
add_filter('acf/add_field_group', 'wpsnippets_add_custom_field_to_field_group');
In this example, we define a custom function wpsnippets_add_custom_field_to_field_group
that takes the $field_group
parameter. Inside the function, we create a new field array and add it to the existing fields
array of the $field_group
. Finally, we return the modified $field_group
.
By adding this code snippet to your theme’s functions.php
file or a custom plugin, you can dynamically add the my_custom_field
to any field group using the ACF interface.
Note: Make sure to replace the field key, label, name, and type with your own values according to your requirements.
This code snippet can be useful when you want to programmatically add custom fields to specific field groups based on certain conditions or requirements. For example, you can use this filter to add additional fields to a field group only if a certain user role is logged in or if a specific post type is being edited.
Examples
Example 1: Adding a custom field group to a specific post type
This example demonstrates how to use the acf/add_field_group
filter to add a custom field group to a specific post type. In this case, we are adding a field group to the “book” post type.
function wpsnippets_add_custom_field_group( $field_group ) {
if ( 'book' === get_post_type() ) {
// Add your custom field group here
}
return $field_group;
}
add_filter( 'acf/add_field_group', 'wpsnippets_add_custom_field_group' );
Explanation: The acf/add_field_group
filter allows you to modify the field group before it is saved to the database. In this example, we check if the current post type is “book” using get_post_type()
. If it is, we can add our custom field group code inside the conditional statement.
Example 2: Modifying an existing field group
This example demonstrates how to use the acf/add_field_group
filter to modify an existing field group. In this case, we are adding an additional field to an existing field group.
function wpsnippets_modify_field_group( $field_group ) {
if ( 'my_field_group' === $field_group['key'] ) {
// Modify the existing field group here
}
return $field_group;
}
add_filter( 'acf/add_field_group', 'wpsnippets_modify_field_group' );
Explanation: The acf/add_field_group
filter allows you to modify the field group before it is saved to the database. In this example, we check if the field group key matches our target field group using $field_group['key']
. If it does, we can add our code to modify the field group inside the conditional statement.
Example 3: Removing a field group
This example demonstrates how to use the acf/add_field_group
filter to remove a field group. In this case, we are removing a field group with a specific key.
function wpsnippets_remove_field_group( $field_group ) {
if ( 'my_field_group' === $field_group['key'] ) {
return false; // Return false to remove the field group
}
return $field_group;
}
add_filter( 'acf/add_field_group', 'wpsnippets_remove_field_group' );
Explanation: The acf/add_field_group
filter allows you to modify the field group before it is saved to the database. In this example, we check if the field group key matches our target field group using $field_group['key']
. If it does, we return false
to remove the field group from being saved.