ACF

How to Stop Post Update and Throw a General Error

// ACF/save_post
add_action('acf/save_post', 'acf_save_post_condition', 5);
function acf_save_post_condition($post_id){

    $my_condition = true;

    if($my_condition){
        
        // Do not update meta fields
        unset($_POST['acf']);
        
        // Change updated URL
        add_filter('redirect_post_location', function($location){
            
            // Remove '?message=1' from URL
            $location = remove_query_arg('message', $location);
            
            // Add '?my-save-error=1' in URL
            return add_query_arg('my-save-error', '1', $location);
            
        });
        
        return;
        
    }

}


// Admin notices
add_action('admin_notices', 'acf_save_post_condition_error');
function acf_save_post_condition_error(){
    
    if(isset($_GET['my-save-error']) && $_GET['my-save-error'] == '1'){ ?>
        <div class="notice notice-error">
            <p><?php _e('My error message'); ?></p>
        </div>
        <?php
    }
    
}

 

ACF Hooks