Skip to content

Instantly share code, notes, and snippets.

@thierrypigot
Created January 7, 2025 17:01
Show Gist options
  • Save thierrypigot/50ecab6aa27f2921fc9b1d81c2f7e171 to your computer and use it in GitHub Desktop.
Save thierrypigot/50ecab6aa27f2921fc9b1d81c2f7e171 to your computer and use it in GitHub Desktop.
Prevent saving empty ACF fields, except when the value is explicitly '0'.
<?php
/**
* Prevent saving empty ACF fields, except when the value is explicitly '0'.
*
* @param mixed $value The field value.
* @param int $post_id The ID of the current post being saved.
* @param array $field The ACF field settings.
*
* @return mixed Returns null if the value is empty (and not explicitly '0'), otherwise returns the value.
*/
function waw_prevent_acf_empty_field_save($value, $post_id, $field) {
// Check if the value is empty and not explicitly '0'
if (empty($value) && $value !== '0') {
return null; // Prevent saving empty values
}
return $value; // Return the value as is otherwise
}
// Apply the filter to ACF field save
add_filter('acf/update_value', 'waw_prevent_acf_empty_field_save', 10, 3);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment