Support

Account

Home Forums General Issues ACF Front End Form Revisions

Solving

ACF Front End Form Revisions

  • Hi,

    None of the fields and their values are being saved when a form is being submitted on the front end. If I update a post in the back end, everything is fine and all of the fields and their values show up.

    Is there a fix for this?

    BTW, when I edit a post in the back end, instead of field titles I see field names in the revision, is it possible to change it to field titles?

    Thanks ๐Ÿ™‚

  • Did you add acf_form_head(); before get_header(); in your template file?

    I’m not sure what you mean for the second question about field titles instead of field names. Do you mean when using the WP revision compare tool?

  • Hi John,

    Yes I do have acf_form_head(); before get_header();

    The only thing that I can I see in the revisions is the changes to post title..

    As for the other questions yes I meant revisions (how they are displaying fields and field values)

    Lets say instead of field title which would be “Title” i get the field name, which comes from <input name="something_title">. Does that make sense? ๐Ÿ˜ฎ

    https://www.dropbox.com/s/3wz43jgyiwdou9s/Screenshot%202015-11-21%2019.38.14.png?dl=0

  • Sorry for the basic question, just needed to make sure.

    For the second question, there is nothing in ACf that will alter the field name to the field title. They may be something in WP, some hook that would allow you to filter that, but I’m not familiar enough with it.

    When you say that the field values aren’t being saved do you mean they’re not being saved at all or just that they’re not appearing in the revisions.

    If they are not being saved at all then you may have a problem with compatibility with another plugin, or there may be a filter interfering.

    What is you acf_form setup. Can you post the code where you have the form arguments? Maybe something in there could be causing the problem.

  • They are not appearing in the revisions, they are being saved and I can display them in the front end ๐Ÿ™‚

    Here is how I build my forms:

    $new_post = array(
    	'post_id'            => 'new', 
    	'field_groups'       =>  array(205),
    	'submit_value'       => 'Create Post',
    	'return'             => '%post_url%',
    	'updated_message'    => 'Post has been saved.',
    	'html_before_fields' => '<h1 class="page-header-title">Create a New Post</h1>',
    );

    BTW, thanks for the help ๐Ÿ™‚

  • You’re creating a new post. If you’re creating a new post I’m not sure that you’d see anything in revisions. But I don’t know to be honest. I’m really not sure how well ACF front end forms work with revisions. But since it’s creating a new post then the image you provided should be correct. The fields would not contain anything before the current version of the new post.

  • You’ll need to have https://github.com/adamsilverstein/wp-post-meta-revisions – or at least until it becomes part of core to enable meta field changes triggering revisions.

    That helps for everything “back end” but does not let you present any of the revisioned meta on the front-end. This SQL might give you a starting point

    SELECT * FROM wp_postmeta WHERE post_ID = [dynamic] AND post_ID IN (SELECT ID FROM wp_posts WHERE post_type = 'revision') ORDER By meta_key;

  • Correction – As long as you pass the revision ID (as post ID) to functions like get_field_object and have_rows, you don’t need to change much front-end logic for presenting revisioned meta on the front-end either.

  • @idealien I don’t see how this is correct for changes made on the front end.
    ACF handles it’s own revisions very similar to how wp-post-meta-revisions does. They both react to filters applied as a result of wp_insert_post which is only called when updates are made in the admin. acf_form_head() however only updates metadata on a single post ID. It does not trigger any functions that would engage ACF’s revisioning or wp-post-meta-revisions revisioning. Altering WP_Query won’t reveal any revisions resulting from use of acf_form() because it doesn’t create any.

  • Hi!

    So I’m encountering the exact same problem.

    I add all the meta fields to _wp_post_revision_fields

    
    add_filter('_wp_post_revision_fields', function ($fields, $post) {
        if ($post && $post['post_type'] === 'visit') {
            $json = TEMPLATEPATH.'/acf-json/group_5b311d2d73933.json';
    
            if (is_file($json)) {
                $visits_fields = json_decode(file_get_contents($json));
    
                if (is_array($visits_fields) && $visits_fields->fields) {
                    foreach ($visits_fields->fields as $field) {
                        if ($field->name) {
                            $fields[$field->name] = $field->label;
                        }
                    }
                }
            }
        }
    
        return $fields;
    }, 10, 2);
    

    Everything works as expected on the admin side, but when updating “visit” from the fontend – a revision is created, but no “change”.

    When I edit post from backend then revision is also created but shows all the custom fields also which were added via the filter.

    I don’t use the acf_form on frontend. Just a simple custom form and wp_insert/update_post and update_field(“<hash>”, value); functions.

  • Everything works as expected on the admin side, but when updating โ€œvisitโ€ from the fontend โ€“ a revision is created, but no โ€œchangeโ€. https://get-shareit.com

  • 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);
    		} 
    	}  
    }
    
Viewing 12 posts - 1 through 12 (of 12 total)

You must be logged in to reply to this topic.