Validate Save PostFREE

Docs Hooks & Helpers Validate Save Post

#Validate Save Post Hooks

ACF Extended is bundled with an advanced version of the acf/validate_save_post hook. It allows developers to hook in any Post, User, Term and Options Pages validation process, on the back-end and front-end.

#Global Validate Save

It is possible to hook on all object validation (Post, User, Term and Options Pages) using the global acfe/validate_save. Usage example:

/*
 * ACFE Validate Save
 * 
 * @string        $post_id  12 | term_46 | user_22 | my-options | comment_89
 * @object/array  $object   The corresponding WP Object (Post, Term, User, Options Page or Comment)
 */
 
action('acfe/validate_save',               $post_id, $object);
action('acfe/validate_save/id=12',         $post_id, $object);
action('acfe/validate_save/id=term_46',    $post_id, $object);
action('acfe/validate_save/id=user_22',    $post_id, $object);
action('acfe/validate_save/id=my-options', $post_id, $object);
action('acfe/validate_save/id=comment_89', $post_id, $object);
add_action('acfe/validate_save', 'my_acfe_validate_save', 10, 2);
function my_acfe_validate_save($post_id, $object){

    // Retrieve the user input from "my_field" field
    get_field('my_field');

    // Retrieve "my_field" field value from DB
    get_field('my_field', $post_id);
    
    // Loop thru the user input from "my_repeater" field
    if(have_rows('my_repeater')):
        while(have_rows('my_repeater')): the_row();
             
             get_sub_field('my_sub_field');
             
        endwhile;
    endif;
    
    // Add a custom validation error
    acfe_add_validation_error('my_field', 'This is a custom error');

}

#Validate Save Post

It is possible to target specifically post validation using the following syntax: acfe/validate_save_post. This new hook also comes with a variation to target one specific post type. Usage example:

/*
 * ACFE Validate Save Post
 * 
 * @string  $post_id  The Post ID
 * @object  $object   The WP Post Object
 */
 
action('acfe/validate_save_post',                $post_id, $object);
action('acfe/validate_save_post/id=12',          $post_id, $object);
action('acfe/validate_save_post/post_type=page', $post_id, $object);
add_action('acfe/validate_save_post/post_type=page', 'my_acfe_validate_save_page', 10, 2);
function my_acfe_validate_save_page($post_id, $object){
    
    // Retrieve the user input from "my_field" field
    get_field('my_field');

    // Retrieve "my_field" field value from DB
    get_field('my_field', $post_id);
    
    // Loop thru the user input from "my_repeater" field
    if(have_rows('my_repeater')):
        while(have_rows('my_repeater')): the_row();
             
             get_sub_field('my_sub_field');
             
        endwhile;
    endif;
    
    // Add a custom validation error
    acfe_add_validation_error('my_field', 'This is a custom error');

}

#Validate Save Term

It is possible to target specifically term saves using the following syntax: acfe/validate_save_term. This new hook also comes with a variation to target one specific taxonomy. Usage example:

/*
 * ACFE Validate Save Term
 * 
 * @string  $post_id  The ACF Term ID
 * @object  $object   The WP Term Object
 */
 
action('acfe/validate_save_term',                   $post_id, $object);
action('acfe/validate_save_term/id=term_46',        $post_id, $object);
action('acfe/validate_save_term/taxonomy=category', $post_id, $object);
add_action('acfe/validate_save_term/taxonomy=category', 'my_acfe_validate_save_category', 10, 2);
function my_acfe_validate_save_category($post_id, $object){
    
    // Retrieve the user input from "my_field" field
    get_field('my_field');

    // Retrieve "my_field" field value from DB
    get_field('my_field', $post_id);
    
    // Loop thru the user input from "my_repeater" field
    if(have_rows('my_repeater')):
        while(have_rows('my_repeater')): the_row();
             
             get_sub_field('my_sub_field');
             
        endwhile;
    endif;
    
    // Add a custom validation error
    acfe_add_validation_error('my_field', 'This is a custom error');

}

#Validate Save User

It is possible to target specifically user saves using the following syntax: acfe/validate_save_user. This new hook also comes with a variation to target one specific role. Usage example:

/*
 * ACFE Validate Save User
 * 
 * @string  $post_id  The ACF User ID
 * @object  $object   The WP Term Object
 */
 
action('acfe/validate_save_user',             $post_id, $object);
action('acfe/validate_save_user/id=user_22',  $post_id, $object);
action('acfe/validate_save_user/role=editor', $post_id, $object);
add_action('acfe/validate_save_user/role=editor', 'my_acfe_validate_save_editor', 10, 2);
function my_acfe_validate_save_editor($post_id, $object){
    
    // Retrieve the user input from "my_field" field
    get_field('my_field');

    // Retrieve "my_field" field value from DB
    get_field('my_field', $post_id);
    
    // Loop thru the user input from "my_repeater" field
    if(have_rows('my_repeater')):
        while(have_rows('my_repeater')): the_row();
             
             get_sub_field('my_sub_field');
             
        endwhile;
    endif;
    
    // Add a custom validation error
    acfe_add_validation_error('my_field', 'This is a custom error');

}

#Validate Save Options Page

It is possible to target specifically options pages saves using the following syntax: acfe/validate_save_option. This new hook also comes with a variation to target one specific options page slug. Usage example:

/*
 * ACFE Save Options Page
 * 
 * @string  $post_id  The ACF Options Page Post ID
 * @array   $object   The ACF Options Page Settings array
 */
 
action('acfe/validate_save_option',                      $post_id, $object);
action('acfe/validate_save_option/id=my-options',        $post_id, $object);
action('acfe/validate_save_option/slug=my-options-page', $post_id, $object);
add_action('acfe/validate_save_option/slug=my-options-page', 'my_acfe_validate_save_options_page', 10, 2);
function my_acfe_validate_save_options_page($post_id, $object){
    
    // Retrieve the user input from "my_field" field
    get_field('my_field');

    // Retrieve "my_field" field value from DB
    get_field('my_field', $post_id);
    
    // Loop thru the user input from "my_repeater" field
    if(have_rows('my_repeater')):
        while(have_rows('my_repeater')): the_row();
             
             get_sub_field('my_sub_field');
             
        endwhile;
    endif;
    
    // Add a custom validation error
    acfe_add_validation_error('my_field', 'This is a custom error');

}

#Validate Save Comment

It is possible to target specifically comments saves using the following syntax: acfe/validate_save_comment. Usage example:

/*
 * ACFE Validate Save Comment
 * 
 * @string  $post_id  The ACF Comment ID
 * @object  $object   The WP Comment Object
 */
 
action('acfe/validate_save_comment',               $post_id, $object);
action('acfe/validate_save_comment/id=comment_89', $post_id, $object);
add_action('acfe/validate_save_comment/id=comment_89', 'my_acfe_validate_save_comment', 10, 2);
function my_acfe_validate_save_comment($post_id, $object){
    
    // Retrieve the user input from "my_field" field
    get_field('my_field');

    // Retrieve "my_field" field value from DB
    get_field('my_field', $post_id);
    
    // Loop thru the user input from "my_repeater" field
    if(have_rows('my_repeater')):
        while(have_rows('my_repeater')): the_row();
             
             get_sub_field('my_sub_field');
             
        endwhile;
    endif;
    
    // Add a custom validation error
    acfe_add_validation_error('my_field', 'This is a custom error');

}