• I had an issue where I was using the acf_form() function on a page template and not on the the page or post itself. This caused the incorrect post_id to be submitted and used in some of the validations.

    Below is the updated function in validated_field_v5.php

    function set_post_id_to_acf_form(){
    		global $post;
    
    		?>
    
    		<script type="text/javascript">
    		jQuery(document).ready(function(){
    			jQuery('form.acf-form').append('<input type="hidden" name="acf[post_ID]" value="' + acf.o.post_id + '"/>');
    			jQuery('form.acf-form').append('<input type="hidden" name="acf[frontend]" value="true"/>');
    		});
    		</script>
    
    		<?php
    	}

    The update is to use acf.o.post_id in Javascript instead of the global $post; object in php as we may not be on the relevant post or page.

    I hope this helps someone 🙂

    https://wordpress.org/plugins/validated-field-for-acf/

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter Charl Pretorius

    (@charlkri8itcom)

    You might need to add some checks here :

    <script type="text/javascript">
    		jQuery(document).ready(function(){
                        if( jQuery('form.acf-form').get(0) ){
                            jQuery('form.acf-form').append('<input type="hidden" name="acf[post_ID]" value="' + acf.o.post_id + '"/>');
    			jQuery('form.acf-form').append('<input type="hidden" name="acf[frontend]" value="true"/>');
                        }
    		});
    </script>

    Plugin Author doublesharp

    (@doublesharp)

    Hi @charlkri8itcom,

    Just to make sure I understand the issue, you are using acf_form() on the front end of your site, and within the function call you are specifying a different post_id than the the ID of the page that the form is embedded on?

    Assuming that is what you are trying to do it looks like you are correct that it’s grabbing the wrong post_id. I’m currently working on a new version of this plugin with lots of new features and support for some add-ons as well, so this fix will probably need to wait until that is ready, which hopefully should be soon. One thing to note is that the fields used by Validated Field in the form post have been placed into their own array element, hopefully to prevent conflicts.

    I haven’t tested this at all, but this is the code I dropped into the 2.0 version

    <script type="text/javascript">
    (function($){
    	$(document).ready(function(){
    		if ( $form = $('form.acf-form') && $form.length ){
    			$form.append('<input type="hidden" name="acf[acf_vf][post_ID]" value="' + acf.o.post_id + '"/>');
    			$form.append('<input type="hidden" name="acf[acf_vf][frontend]" value="true"/>');
    		}
    	});
    })(jQuery);
    </script>
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Front End Form Fix for forms on page templates’ is closed to new replies.