The ACF acf/prepare_field_for_export
filter allows you to modify the value of a field before it is exported or saved to a file. This can be useful when you need to manipulate the field value or perform additional checks before exporting it.
Here’s an example of how you can use the acf/prepare_field_for_export
filter to modify the value of a field:
function wpsnippets_modify_field_value_for_export( $value, $field ) {
// Modify the value here
$modified_value = $value . ' (modified)';
return $modified_value;
}
add_filter( 'acf/prepare_field_for_export', 'wpsnippets_modify_field_value_for_export', 10, 2 );
In this example, we define a custom function wpsnippets_modify_field_value_for_export
that takes two parameters: $value
(the original value of the field) and $field
(the ACF field object). Inside the function, we modify the value by appending (modified)
to it. Finally, we return the modified value.
To use this code snippet, you need to replace wpsnippets_modify_field_value_for_export
with your own function name and modify the logic inside the function to suit your needs. Once you’ve added this code to your WordPress theme’s functions.php
file or a custom plugin, the acf/prepare_field_for_export
filter will be applied to all ACF fields before they are exported or saved to a file.
Examples
Example 1: Modify ACF field value before exporting
This use case demonstrates how to use the acf/prepare_field_for_export
filter to modify the value of an Advanced Custom Fields (ACF) field before it is exported.
add_filter( 'acf/prepare_field_for_export', 'wpsnippets_modify_field_value', 10, 3 );
function wpsnippets_modify_field_value( $field, $post_id, $format ) {
if ( $field['type'] === 'text' ) {
$field['value'] = strtoupper( $field['value'] );
}
return $field;
}
In this example, the acf/prepare_field_for_export
filter is used to modify the value of a text field before it is exported. The code checks if the field type is ‘text’ and then converts the value to uppercase using the strtoupper()
function. The modified field is then returned.
Example 2: Exclude specific ACF fields from export
This use case demonstrates how to exclude specific ACF fields from being exported using the acf/prepare_field_for_export
filter.
add_filter( 'acf/prepare_field_for_export', 'wpsnippets_exclude_fields', 10, 3 );
function wpsnippets_exclude_fields( $field, $post_id, $format ) {
$excluded_fields = array( 'field_name_1', 'field_name_2' );
if ( in_array( $field['name'], $excluded_fields ) ) {
$field = false;
}
return $field;
}
In this example, the acf/prepare_field_for_export
filter is used to exclude specific ACF fields from being exported. The code defines an array of field names to be excluded. If the current field’s name is found in the array, the $field
variable is set to false
, effectively excluding the field from the export.
Example 3: Modify ACF field based on post ID
This use case demonstrates how to modify an ACF field based on the post ID using the acf/prepare_field_for_export
filter.
add_filter( 'acf/prepare_field_for_export', 'wpsnippets_modify_field_by_post_id', 10, 3 );
function wpsnippets_modify_field_by_post_id( $field, $post_id, $format ) {
if ( $post_id === 123 ) {
$field['value'] = 'Custom Value';
}
return $field;
}
In this example, the acf/prepare_field_for_export
filter is used to modify the value of an ACF field based on the post ID. The code checks if the current post ID is 123 and if so, sets a custom value for the field. The modified field is then returned.