acf/validate_save_post

Last updated Sep 29, 2023

Description

Fires when validating the $_POST data of a submitted form.

Used to add or remove validation errors controlling whether or not the submitted data will be saved.

Please note this is a generic action. If you are looking to validate a specific field value, please use the acf/validate_value filter instead.

Changelog

  • Added in version 5.0.0

Example

This example demonstrates how to bypass validation for an administrator, and also to require a custom input value. See the Notes section below for additional information on the functions used.

functions.php

add_action('acf/validate_save_post', 'my_acf_validate_save_post');
function my_acf_validate_save_post() {

    // Remove all errors if the user is an administrator.
    if( current_user_can('manage_options') ) {
        acf_reset_validation_errors();
    }

    // Require custom input value.
    if( empty($_POST[‘acf’][‘field_xxxxxx’] ) ) {
        acf_add_validation_error( 'acf[field_xxxxxx]', 'Please check this input to proceed' );
    }
}

Notes

Additional functions

The following additional functions are made available during this action.

  • acf_add_validation_error()
    Adds a validation error and prevents the submitted data from being saved.

    acf_add_validation_error($input, [$message])
    
    • $input (string) (Required) The input used here should be an ACF field key, rather than the field name.
    • $message (string) (Optional) The error message to display.
  • acf_reset_validation_errors()
    Removes all validation errors and allows the submitted data to be saved.

    acf_reset_validation_errors()
    

Related