Support

Account

Home Forums General Issues ACF Front End Form Revisions Reply To: ACF Front End Form Revisions

  • The following snippets have served me well to store meta revisions on a front-end post save. Note that if your ACF group is very large this can have very large impacts on your DB and post save performance. YMMV. I have not tested it for WP > 5.0

    I believe I sourced the initial concept from a different thread here but cannot locate at the moment.

    
    //ACF - Extend ACF Save Behaviour to ensure front-end saves trigger revision
    add_action('acf/pre_save_post', 'pre_save_post_update_revisions');
    function pre_save_post_update_revisions($post_id) {
    	// bail early if editing in admin
    	// ACF already handles admin revisions correctly
    	if (is_admin()) {
    	  return $post_id;
    	}
      
    	// force a post update, this will generate a revision and
    	// trigger the '_wp_put_post_revision' action
    	$update_post = get_post($post_id);
    
    	$saved_revision = wp_save_post_revision( $update_post );
      	return $post_id;
    }
    
    //ACF - Extend ACF Save Behaviour to ensure revisions include meta
    add_action('_wp_put_post_revision', '_on_wp_put_post_revision');
    function _on_wp_put_post_revision($revision_id) {
    
    	// get the revision post object
    	$revision = get_post($revision_id);
    	// get the id of the original post
    	$post_id = $revision->post_parent;
    
    	// bail early if editing in admin
    	if (is_admin())
    		return;
      	//For Non-Admin Cases - ensure the meta data is written to revision as well as post
    	if (!empty($_POST['acf'])) {
    	  	foreach ($_POST['acf'] as $k => $v) {
     
    			// bail early if $value is not is a field_key
    			if (!acf_is_field_key($k)) {
    				continue;
    			}
    			update_field($k, $v, $revision_id);
    			update_metadata('post', $revision_id, $k, $v);
    		} 
    	}  
    }