Support

Account

Home Forums Front-end Issues No revision when update from front end

Solving

No revision when update from front end

  • Hi,

    I’m having problem with post revisions, after lots of testing (I submitted ticket before this but I got nothing…) still im not able to figure out whats wrong.

    If I update the post from the back-end, revisions works perfectly, but when I move to the front end and submit any changes, no revisions! wordpress only see the title+content changes.

    I reached this state after deleting the function I had in the function.php!, as if I use it even the back-end doesn’t work except for the title!!!

    now this is what I have for updating the front end in my single.php

    <?php
    	$edit_post = array(
    		'post_id'					=> get_the_ID(), 	// Get the post ID
    		'post_title'					=> true,				// using default title text
    		'post_content'			=> true,
    		'field_groups'				=> array(11), 		// Create post field group ID(s)
    		'return'						=> '%post_url%',	//home_url(),
    		'html_before_fields'	=> '<button id="edit_close" role="button">Close editing</button>',
    		'html_after_fields'		=> '',
    		'submit_value'			=> 'Save Changes',
    		'updated_message'		=> 'Your Handover has been updated',
    	);
    	acf_form( $edit_post );
    ?>
    

    I did search and All I found is asking to add this
    wp_update_post( array('ID' => $post_id) );

    I tried it in few places “not sure where is should be, and it didnt help.

    Regards

  • I did a test and it does look like revisions are not created using a front end form. I don’t know if this is intended or a bug. I will flag this post for the developer.

  • Thanks, Please let me know on this as it is essential matter in my project and waiting for this.

    If there is way to force WP to revision the post I would love to know it.

    Thanks

  • You would need to dig through the code in ACF. More than likely you could force the revision to be saved. Here is an older topic that talks about what was done to force revisions to be saved in the back end. http://support.advancedcustomfields.com/forums/topic/wordpress-3-6-revisions-custom-fields-no-longer-tracked/

  • Thanks for the link, ill go through it and see if I can find a way.

    Do I need to report this bug ? or the developers aware of it now ?

  • I have marked this topic so that the developer will take a look at it. I can’t say when that will be though. I just help him out on this forum. If you reported on this forum as a bug then it would be the same situation.

  • Forcing revision didn’t help saving the the ACF in the revisions, still only the title + content. In the link you posted there is someone having the same bug! lets hope the developer can patch this soon.

    I recommended this plugin for my organization as the best solution for our websites, hopefully this wont fail me that quick 🙁

  • Hi @et3rnal

    Thanks for the topic.
    I’ll add this to my to-do and have a look.
    I think if you are to update a post, it should create a revision, that sounds fair.

    Cheers
    E

  • Thanks, Ill be waiting for that so badly.

  • Hi,

    Just wanted to check about this issue or feature.
    I took time to go through the different posts related to this but I’m still confused.
    I’m still not sure what is the best way to trigger the revision in the callback.
    The wp_update_post doesn’t seem to work.

    Any advice on how to get pending revisions on acf_form updates?

  • Revisions are actually created but the meta are not included in the revision or at least they don’t show.

  • The feature plugin WP-Post-Meta-Revisions is what you need. With it active, changes to meta from the front-end forms will be saved in / as a full revision.

    If you want to present a revision (with all it’s meta at the time of revision) on the front-end, make sure you identify the revision ID (as post ID) to the ACF functions like have_rows and get_field_object.

  • Just ran into this one. It appears (though I’m not sure) that ACF and WP-Post-Meta-Revisions are redundant. I suspect that if you have ACF, you don’t need the other. I’d like clarification on that.

    In the mean time I’ll be trying to discover why font end edits don’t trigger a revision.

  • I’ve tested the plugin, and did a small changes to make it loop through all the meta fields in the post “I have many”

    function add_meta_keys_to_revision( $keys ) {
    	$fields = get_fields();
    	$count=0;
    	if( $fields )
    	{
    		foreach( $fields as $field_name => $value )
    		{
    			$field = get_field_object($field_name, false, array('load_value' => false));
    			$keys[$count] = $field_name;
    			$count++;
    		}
    	}
        return $keys;
    }
    add_filter( 'wp_post_revision_meta_keys', 'add_meta_keys_to_revision' );

    it works fine, however im getting error in the revision page

    version in F:\EasyPHP-12.1\www\hot\wp-content\plugins\advanced-custom-fields-pro\core\revisions.php on line 272

    also I noticed I have 2 revision every time i make a modification one with no meta-data and the other with.

    I hove the next update will have a fix for this old bug based on this plugin

  • I didn’t realize ACF has already solved the admin side revisions. So the Post Meta Revisions Plugin is not needed not that it integrates well anyway. Because of ACF’s multiple meta-keys per field setup, a plugin native solution is most desirable. This is the last piece of a big problem so I would really love to hear from @elliot how this is coming along.

  • UPDATE: disregard this hack. See next post for a more complete solution.

    Ok so I’ve made a little progress on the concept of revisioning with acf_form() but is admittedly a hack. It will revision a post but for some reason, not until the 2nd change. I’m still working on it but I’m posting it here in hopes that someone else can expand on or replace with a more elegant solution.

    Basically I’m hijacking ‘pre_save_post’ and instead of letting it alter post meta for the existing post, I’m forcing the creation of a revision and letting acf act on that new post ID instead. Of course we still want the active post to get updated so I’m manually calling an un-documented acf function which obviously is a very bad approach.

    function my_pre_save_post( $post_id ) {
        // bail early if editing in admin
        if( is_admin() ) {
          return;
        }
        // bail if this is a brand new post
        if ( ! is_numeric($post_id) ) {
          return $post_id;
        }
    
        acf_save_post( $post_id );
        add_filter( 'wp_save_post_revision_check_for_changes', 'on_forced_revision', 10, 3 );
        $new_id = wp_save_post_revision( $post_id );
        remove_filter( 'wp_save_post_revision_check_for_changes', 'on_forced_revision', 10 );
        // return the new ID
        return $new_id;
    
    }
    add_filter('acf/pre_save_post' , 'my_pre_save_post', 1, 2 );
    
    function on_forced_revision($check_for_changes, $last_revision, $post) {
      if( isset($_POST['_acfchanged']) && $_POST['_acfchanged'] == '1' )
      {
        return false;
      }
      return $check_for_changes;
    }
  • Ok so I finally have something that so far appears to be pretty solid. It’s basically hooking into acf filters to trigger our own revision and then copying values over. This hasn’t been tested for creating NEW posts with acf_form, just editing them.

    
    function my_pre_save_post( $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
        wp_update_post( get_post($post_id) );
        // allow acf to update the parent post meta normally
        return $post_id;
    }
    add_filter('acf/pre_save_post' , 'my_pre_save_post' );
    
    // revision just got created
    // detect all of the acf fields on the original and
    // apply them to the revision
    function _on_wp_put_post_revision( $revision_id ) {
      // bail early if editing in admin
      if( is_admin() ) {
        return;
      }
      // get the revision post object
      $revision = get_post( $revision_id );
      // get the id of the original post
      $post_id  = $revision->post_parent;
      // A lot of this is copied from ACF's revision class
      // get all the post meta from the original post
      $custom_fields = get_post_custom( $post_id );
      if( !empty($custom_fields) ) {
        foreach( $custom_fields as $k => $v ) {
          // value is always an array
          $v = $v[0];
          // bail early if $value is not is a field_key
          if( !acf_is_field_key($v) ) {
            continue;
          }
          // remove prefix '_' field from reference
          $field_name = substr($k, 1);
          // update the field value using the POST value supplied in the form
          update_field($field_name, $_POST['acf'][$v], $revision_id);
          // add the reference key
          update_metadata('post', $revision_id, $k, $v);
        }
      }
    }
    add_action( '_wp_put_post_revision', '_on_wp_put_post_revision' );
    
  • @joelstransky Tried your last code and her is what happened:

    -it created 3 revisions? only the first one has the changes, the other 2 didnt hold the meta

    – It through errors few times, the first was a loop of all the feilds like this:
    Notice: Undefined index: field_55fee582b1b7e in F:\EasyPHP-12.1\www\hot\wp-content\themes\hand-theme\functions.php on line 111

    and last line error was:
    Warning: Cannot modify header information – headers already sent by (output started at F:\EasyPHP-12.1\www\hot\wp-content\themes\hand-theme\functions.php:111) in F:\EasyPHP-12.1\www\hot\wp-includes\pluggable.php on line 1171

    the line:111 is this (of your code):
    update_field($field_name, $_POST['acf'][$v], $revision_id);

    -Finally I got simller error to the one I got when using the code+plugin
    version in F:\EasyPHP-12.1\www\hot\wp-content\plugins\advanced-custom-fields-pro\core\revisions.php on line 272

    Hops that helps.

  • My code’s not bullet proof for sure, but given a certain environment+process, it should work. So I’ll attempt to describe my setup to see if we differ.
    First. Do not use the wp-post-meta-revisions plugin. Even if you don’t use its required filter, I fear it creates a race condition with ACF. There may be a more elegant way to tie the two plugins together but I couldn’t find one. This could be an issue if it ever becomes part of core.

    Ok, so as far as front end EDITS, acf_form_head() must come before get_header() as explained here. In fact, that basic code example should be all you need. I haven’t tested this where a specific post_id is passed to acf_form’s post_id argument although that should work too.
    My code is only intended to be ran while processing an acf_form() so it assumes $_POST['acf'] will exist.

    As far as your 3 revisions, wordpress is kinda funny. I tested on a clean wordpress install with no plugins and the twentyfifteen theme and when you create a new post and publish, you won’t see the revisions meta box. Then if you change the content, title or except and click update, the revisions meta box will show (if you have it in screen options) and two revisions will be there. They are your first ‘publish’ and first ‘update’. This doesn’t explain why you see 3 after your first front end edit. Perhaps you could share more about your setup.

  • Works on my environment, no errors noted.

    @joelstransky 1000x thank you for an elegant solution!

  • FYI – When using @joelstransky snippet with the
    Component Field Add-On, I do get similar errors to what @et3rnal posted. It looks like the $_POST[‘acf’][$v] index is not being properly identified for sub-fields within the component. I’ve cross-posted support request to the vendor page in link above, but thought I’d double up here in case anyone has suggestions to resolve.

  • @joelstransky

    Thx for your solution. Works perfectly for me. What is needed if acf_form is set to false and an embedded form is used. Any suggestions for your snippet?

  • Hi,

    Revisions where also missing for me on frontend posting with ACF.

    If it helps, I just test successful to call an ACF function to copy metadata from post to revision.
    Perhaps it’s better to add action to ‘save_post’ because _wp_put_post_revision() should not be used by theme or plugin.

    add_action('save_post', 'custom_save_post', 20);
    function custom_save_post( $post_id ) {
    	// only on front-end
    	if( is_admin() ) {
    		return;
    	}
    	
    	$parent_id = wp_is_post_revision( $post_id ); //return False if not a revision, ID of revision's parent otherwise.
    	
    	if ( $parent_id ) {
    		acf_copy_postmeta( $parent_id, $post_id ); //This function will copy meta from a post to it's latest revision
    	}
    }
  • I am currently implementing a bug tracker based on WordPress with ACF where I make heavy use of revisions.

    Today I found that the issue of this thread is still there – even in 2020. Are there any plans to make this a part of the ACF code rather than having to use a dirty hack?

    Also, I found that the fix from @joelstransky works fine for posts which are updated via frontend forms, but if the post has just been created, the first revision doesn’t contain the field values. I reckon this is because the code uses the pre_save_post filter. If I change this to the save_post filter, it works just fine – but then there are two revisions being created for each save, which is rather annoying.

    Any update from the developer(s) would be much appreciated, @elliot.

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

You must be logged in to reply to this topic.