Support

Account

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

  • 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' );