• Resolved jonmojo

    (@jonmojo)


    I’m trying to extend the frontend form for the Events Manager using ACFE forms (The PHP code) embedded within the frontend form template (event-editor.php). With submit and form off, these appear OK within the form, but I can’t save the ACF elements on POST. I’m working from this post on the ACF forums, but I’m not sure if this is the best approach with the ACFE forms? How do you ‘save’ the ACFE form assuming you can see all the $_POST info.

    • This topic was modified 1 year, 4 months ago by jonmojo.

    The page I need help with: [log in to see the link]

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter jonmojo

    (@jonmojo)

    Just an update to this. The key issue I’ve encountered is that that the post_id of the AFCE form does not match the post_id for the form it’s actually in (event_id). AFCE appears to be using the post_id for the ACF form it’s related to. I’ve tried to hook in to change this on submission, but I don’t think these work with the ‘Submit’ enabled in the form. For reference my setting are:

    • General:
      • Field group name
      • Action:
        • Update post
      • Save:
        • Target: current: post
        • Defaults
        • Save ACF Fields: All
      • Load:
        • Load values: Yes
        • Defaults
        • Save ACF Fields: All
    • Settings:
      • Form element: off
      • Submit button: off
      • Everything else: default
    • HTML, Validation, Success:
      • Default

    Of the Hooks, none that start ‘afce/form/submit’ trigger on submission of the event form and the same result with ‘acfe/form/prepare/post’ . I’ve also tried:

    <?php acfe_form(array(
    	'name' => 'basic-info',
    	'post_id' => $EM_Event->post_id,
    	)); ?>
    Thread Starter jonmojo

    (@jonmojo)

    OK – The following seems to work OK with multiple AFCE forms in the same Events Manager template (event-editor.php – copy to themes). The filter is in the root file of a plugin or functions.php in a theme.

    Of the above settings, turn ‘Save ACF fields’ OFF, everything else stays the same.

    An ACF or ACFE approach would probably better, using their own hooks, filters or settings, but this is working for me.

    function my_form_extras_post ($result, $event) {
    
    
    	if ( (!empty($_POST)) && (!is_admin()) ) {  //check for post variables and ignore if on the admin pages.
    		
    		//Will iterate over each ACF field held in $_POST and save it.
    		foreach ( $_POST['acf'] as $ec_field => $ec_field_value) {
    
    			$event_id = $event->post_id;						//post_id of the event ('550')
    			$field_key = $ec_field;								//The ACF field Key (field_dkjasflk2343)
    			$field_details = get_field_object($field_key);		//The ACF field detail - array
    			$field_name = $field_details['name'];				//The ACF name for the field ('company_name')
    			$field_value = $ec_field_value;						//The value in the field as typed in on the interface ('Truck Mart')
    
    			if ( !empty( $field_value ) ) {						//Do nothing if the value field is empty
    					update_post_meta( $event_id, $field_name , $field_value );  //Creates or updates the meta field holding the value
    					update_post_meta( $event_id, "_".$field_name, $field_key ); //Creates or updates the meta field holding the ACF reference (note the underscore before the name)
    			}
    		}
    	}
    
    }
    
    add_filter( 'em_event_save', 'my_form_extras_post', 10, 2 );

    • This reply was modified 1 year, 4 months ago by jonmojo.
    Plugin Author Konrad Chmielewski

    (@hwk-fr)

    Hello,

    Thanks for the feedback!

    I’m glad to hear that you managed to make it work.

    Note that the default $form['post_id'] argument use acf_get_valid_post_id() which use get_the_ID(). I’m not quite sure how you Event Plugin handle post ids, so I’m not sure why it wouldn’t update meta using the classic “Target: Current Post” in the Form UI.

    The workaround you found seems correct, as you found out, each field input values are handled in $POST['acf'].

    Regards.

    Thanks for this

    jonmojo

    (@jonmojo)

    I am trying to do the same. I have set up the form as above, included the code snippet and modified the template (event-editor.php). The values save in “wp_postmeta” table. When I edit the event on the frontend I get a fatal error. “PHP Fatal error: Uncaught Error: Cannot use object of type stdClass as array in /my-site/wp-content/plugins/acf-openstreetmap-field/include/ACFFieldOpenstreetmap/Field/OpenStreetMap.php:565”.

    The form saves the values in “wp_postmeta” table but I noticed a difference between the values saved for ACF OpenStreetMap Field for events created by admin and frontend events.
    Example values.
    Admin a:7:{s:3:”lat”;d:-21.4530686;s:3:”lng”;d:149.23759.etc
    Frontend {“lat”:-24.9401487,”lng”:152.4928951,”zoom”:13,etc

    I do not know how to fix this and any help would be accepted gratefully. Thank You.

    Update on my issue
    ACFE form is available on frontend form only as Admin uses Field group location. I used “if ( !is_admin()) {}” in form template.

    ACF OpenStreetMap Field plugin version 1.4.3 – later versions produce errors

    The meta_value’s – as saved in “wp_postmeta” table for same map location

    ADMIN

    a:7:{s:3:”lat”;d:-21.3840324;s:3:”lng”;d:149.225235;s:4:”zoom”;i:10;s:7:”markers”;a:1:{i:0;a:6:{s:5:”label”;s:36:”J Pitcher Road, Queensland Australia”;s:13:”default_label”;s:36:”J Pitcher Road, Queensland Australia”;s:3:”lat”;d:-21.3834102;s:3:”lng”;d:149.2239913;s:7:”geocode”;a:0:{}s:4:”uuid”;s:20:”marker_65a0abf96ea7a”;}}s:7:”address”;s:36:”J Pitcher Road, Queensland Australia”;s:6:”layers”;a:1:{i:0;s:13:”OpenStreetMap”;}s:7:”version”;s:5:”1.4.3″;}

    Frontend Form

    {“lat”:-21.3840324,”lng”:149.225235,”zoom”:10,”markers”:[{“label”:”J Pitcher Road, Queensland Australia”,”default_label”:”J Pitcher Road, Queensland Australia”,”lat”:-21.3834102,”lng”:149.2239913,”geocode”:[],”uuid”:”marker_65a0abf96ea7a”}],”address”:”J Pitcher Road, Queensland Australia”,”layers”:[“OpenStreetMap”],”version”:”1.4.3″}

    If I edit the "wp_postmeta" table and replace the "Frontend Form" meta_value with the "ADMIN" meta_value, I can edit the event in Admin, and on frontend as permitted user, with all fields populated and displayed with no errors.
    However, if the event is updated on the frontend, and, even without changing any field values, the meta_value changes back and the error returns. The event is then not editable in Admin or Frontend.

    The “Fatal Error” is caused by the incorrect format of the meta_value.

    How do I modify how the values are saved for the field on the frontend form submission?

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Extending other plugin frontend forms with ACFE’ is closed to new replies.