The ACF update_value
action is used to perform custom actions when a field value is updated in Advanced Custom Fields (ACF). This action can be useful in scenarios where you need to perform additional tasks or validations when a specific field is updated.
To use the update_value
action, you need to create a custom function and hook it to the action using the add_action()
function. Here’s an example of how you can use the update_value
action:
function wpsnippets_custom_field_update( $value, $post_id, $field ) {
// Perform custom actions here based on the updated field value
// You can access the field object using the $field parameter
// Example: Log the updated value to the error log
error_log( 'Field updated: ' . $field['name'] . ' - New value: ' . $value );
}
add_action( 'acf/update_value', 'wpsnippets_custom_field_update', 10, 3 );
In the above example, we define a custom function wpsnippets_custom_field_update
that accepts three parameters: $value
(the updated field value), $post_id
(the ID of the post being updated), and $field
(the ACF field object).
Inside the function, you can perform any custom actions based on the updated field value. In this example, we simply log the updated field name and its new value to the error log using error_log()
.
By hooking this function to the acf/update_value
action using add_action()
, the function will be executed whenever a field value is updated in ACF.
This code snippet can be useful in scenarios where you need to perform additional tasks or validations when a specific field value is updated in ACF. For example, you can use it to update related fields, trigger notifications, or perform calculations based on the updated value.
Examples
Example 1: Updating a field value before it is saved
This use case demonstrates how to use the acf/update_value
action to modify the value of a field before it is saved to the database. In this example, we’ll update the value of a text field to convert it to uppercase before saving.
function wpsnippets_update_field_value( $value, $post_id, $field ) {
if ( $field['type'] === 'text' ) {
$value = strtoupper( $value );
}
return $value;
}
add_filter( 'acf/update_value', 'wpsnippets_update_field_value', 10, 3 );
In this code example, we define a custom function wpsnippets_update_field_value
that accepts three parameters: $value
(the current value of the field), $post_id
(the ID of the post being edited), and $field
(the ACF field object). Inside the function, we check if the field type is “text” and if so, we convert the value to uppercase using the strtoupper()
function. Finally, we return the modified value.
Example 2: Updating a field value based on another field
This use case demonstrates how to update the value of a field based on the value of another field using the acf/update_value
action. In this example, we’ll update a text field based on the value of a checkbox field.
function wpsnippets_update_field_value( $value, $post_id, $field ) {
if ( $field['name'] === 'text_field' ) {
$checkbox_value = get_field( 'checkbox_field', $post_id );
if ( $checkbox_value ) {
$value = 'Checked';
} else {
$value = 'Unchecked';
}
}
return $value;
}
add_filter( 'acf/update_value', 'wpsnippets_update_field_value', 10, 3 );
In this code example, we define a custom function wpsnippets_update_field_value
that accepts three parameters: $value
(the current value of the field), $post_id
(the ID of the post being edited), and $field
(the ACF field object). Inside the function, we check if the field name is “text_field” and if so, we retrieve the value of the checkbox field using the get_field()
function. Depending on the checkbox value, we update the text field value accordingly. Finally, we return the modified value.
Example 3: Updating a field value based on a condition
This use case demonstrates how to update the value of a field based on a custom condition using the acf/update_value
action. In this example, we’ll update a number field based on whether a related checkbox field is checked or not.
function wpsnippets_update_field_value( $value, $post_id, $field ) {
if ( $field['name'] === 'number_field' ) {
$checkbox_value = get_field( 'checkbox_field', $post_id );
if ( $checkbox_value ) {
$value = $value * 2;
}
}
return $value;
}
add_filter( 'acf/update_value', 'wpsnippets_update_field_value', 10, 3 );
In this code example, we define a custom function wpsnippets_update_field_value
that accepts three parameters: $value
(the current value of the field), $post_id
(the ID of the post being edited), and $field
(the ACF field object). Inside the function, we check if the field name is “number_field” and if so, we retrieve the value of the checkbox field using the get_field()
function. If the checkbox is checked, we multiply the number field value by 2. Finally, we return the modified value.