acf/validate_save_post

Overview

This action is fired during the validation process (triggered when publishing a post) and allows you to review $_POST data and add validation errors.

Please note that ACF also provides a filter to validate each field value called acf/validate_value.

Changelog

  • Added in v5.0.0

Usage

Adding a validation error is done via the acf_add_validation_error( $input, $message = '' ) function. This function accepts 2 paramters. The first is the input’s name attribute (used by JS to match the message to the correct HTML element) and the second is the message string.

Removing validation errors is done via the acf_reset_validation_errors() function.

This example demonstrates how the action could be used to bypass validation for an administrator

functions.php

<?php 

add_action('acf/validate_save_post', 'my_acf_validate_save_post', 10, 0);

function my_acf_validate_save_post() {
	
	// check if user is an administrator
	if( current_user_can('manage_options') ) {
		
		// clear all errors
		acf_reset_validation_errors();
		
	}
	
	
	// check custom $_POST data
	if( empty($_POST['my_input']) ) {
		
		acf_add_validation_error( 'my_input', 'Please check this input to proceed' );
		
	}
	
}

	
?>

Related