Support

Account

Home Forums General Issues Save Post Revisions

Solving

Save Post Revisions

  • Hi all,

    I’m currently building a WP theme and I’m making extensive use of the flexible content field.

    To ensure user content will remain as-is if the user uninstalls ACF or changes their theme, when a post is saved the ACF fields from the flexible content are copied over to ‘the_content()’ using this function:

    function save_flex_to_content( $post_id ) {
    		
    	// Only run this code if we're on a particular post type
    	if ( 'page' == get_post_type() ) {
    
    		// If there are any blocks
    		if( get_field('acf-flex') ) {
    
    			// Start an output buffer
    			ob_start();
    			
    			// check if the flexible content field has rows of data
    			if( have_rows('acf-flex') ):
    
    				// loop through the rows of data
    				while ( have_rows('acf-flex') ) : the_row();
    
    					// Get the file /template-parts/content-partials (which contains the relevant block layouts)
    					get_template_part( 'template-parts/content', 'partials' );
    
    				endwhile;
    
    			endif;
    
    			// Store output buffer
    			$new_post_content = ob_get_clean();
    
    			// Remove post revisions (as there was an issue with 2 revisions being created)
    			remove_action( 'post_updated', 'wp_save_post_revision' );
    
    			// Update the post_content 
    			wp_update_post( array('ID' => $post_id, 'post_content' => $new_post_content ));
    
    			// Re-add post revisions
    			add_action( 'post_updated', 'wp_save_post_revision' );
    
    		} else {
    
    			return;
    
    		}
    
    	}
    
    }
    // On 'ACF Save Post', save our blocks to post content
    add_action('acf/save_post', 'save_flex_to_content', 20);

    The issue I’m having is that every time a post is saved, 2 revisions are also being saved. I can avoid this in part by adding remove_action( 'post_updated', 'wp_save_post_revision' ); and add_action( 'post_updated', 'wp_save_post_revision' ); to the above function, which means the code being copied to ‘the_content()’ won’t save a revision.

    What I want though is for the initial ‘save post’ not to create a revision, so the only revision will be from the above function.

    Note: I want revisions to continue to act as normal other than the above, so disabling them altogether is not an option.

    Thanks all.

    Shaun

  • I just spent some time looking into this because I also have an interest in doing the same thing. I came to the same conclusion that you have as far as removing and adding the filter for the post revision.

    I do not see any way to do this on the initial save, only on your second save as you’ve suggested.

    The only possibility I can see is to add a pre_post_update https://developer.wordpress.org/reference/hooks/pre_post_update/

    In this filter, check the post type, or other data to see if it’s something that you don’t want to save a revision for. If it is then do.

    
    remove_filter( 'post_updated', 'wp_save_post_revision' );
    

    Not that this will happen before ACF has updated the fields. Then, just before you update the post do

    
    add_action( 'post_updated', 'wp_save_post_revision' );
    
  • Hello, I am resurrecting this old post because I am more or less looking for the same thing.

    Whenever a post is created the save_post filter immediately creates another revision of the post, so, consecutive posts, don’t have consecutive ids.

    However, I do need revisions, so how can I prevent revisions on the save_post function?

  • I have recently found something new that might help those looking to prevent multiple revisions. Please note that I have not tested this and it is just speculation at this time.

    This filter in WP is called just before saving a post revision

    
    $post_has_changed = (bool) apply_filters( 'wp_save_post_revision_post_has_changed', $post_has_changed, $last_revision, $post );
    

    So, using this hook you could

    
    add_filter('wp_save_post_revision_post_has_changed', 'my_prevent_post_revisions', 10, 3);
    function my_prevent_post_revisions($post_has_changed, $last_revision, $post) {
      // here you can do some checking
      // for example if you only want to prevent revisions on a specific post type
      // return false to prevent post revision
    
      return $post_has_changed;
    }
    

    Then in your filter or function where you want the revision to be saved

    
    // remove the prevent post revision filter
    remove_filter('wp_save_post_revision_post_has_changed', 'my_prevent_post_revisions', 10);
    
    // update the post
    wp_update_post($args)
    
    // add the filter again
    add_filter('wp_save_post_revision_post_has_changed', 'my_prevent_post_revisions', 10, 3);
    
    

    If anyone has a chance to test this let us know.

Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Save Post Revisions’ is closed to new replies.