Skip to content

Instantly share code, notes, and snippets.

@michaelbragg
Last active April 30, 2024 08:21
Show Gist options
  • Save michaelbragg/e04f14f883e56fc73613a07ef91d8020 to your computer and use it in GitHub Desktop.
Save michaelbragg/e04f14f883e56fc73613a07ef91d8020 to your computer and use it in GitHub Desktop.
Hotfix: ACF Block Validation not being checked.
<?php
/**
* Hotfix: ACF Block Validation not being checked.
*
* @version 1.0.0
*
* @author Seriphyn <https://support.advancedcustomfields.com/forums/users/seriphyn/>
* @author Michael Bragg <https://michaelbragg.com> <https://vatu.dev>
* @copyright 2022 Curiosity Stream Inc.
*/
add_action( 'acf/validate_save_post', 'acf_validate_save_post', 5 );
/**
* Validate required fields before saving post.
*
* @link https://support.advancedcustomfields.com/forums/topic/required-fields-in-gutenberg-editor/
* @link https://support.advancedcustomfields.com/forums/topic/checking-nonce-when-validating-field/
*
* @return void
*/
function acf_validate_save_post(): void {
// phpcs:ignore WordPress.Security.NonceVerification.Missing -- ACF validates its own nonce before this hook.
$sanitized_post = wp_unslash( $_POST );
foreach ( $sanitized_post as $key => $value ) {
if ( strpos( $key, 'acf' ) !== 0 ) {
continue;
}
if ( empty( $value ) ) {
continue;
}
acf_validate_values( $value, $key );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment