Support

Account

Home Forums Front-end Issues ACF Front End Forms Manually Save

Helping

ACF Front End Forms Manually Save

  • I need to be able to use a different field group for the frontend acf_form than I use for the field group assigned to the post type where the data will live.

    This is due to extensive validation and conditional logic which makes using a subset of fields a non-starter.

    I’ve run across issues because of course the field keys are different. Additionally, I have a Date field on the frontend form that needs to be inserted into a Date Time field.

    How can I set up acf_form so the data doesn’t save anywhere, but I can still use acf/save_post to manually insert the data where it needs to go?

    Here’s the code I was using to try and change the Date field before I realized the field key related issues.

    add_action('acf/save_post', function($post_id){
    
        if(get_post_type($post_id) == 'progression' && !empty($_POST['acf_form_id']) && ($_POST['acf_form_id'] == 'add_transcript_entry')) {
    
           log($_POST);
    
            wp_update_post([
                'ID' => $post_id,
                'post_title' => get_field('class_name', $post_id),
                'post_name' => sanitize_title(get_field('class_name', $post_id)),
            ]);
    
            $user = wp_get_current_user();
    
            update_field('entry', 'Student', $post_id);
            update_field('user', get_current_user_id(), $post_id);
            update_field('first_name', $user->first_name, $post_id);
            update_field('last_name', $user->last_name, $post_id);
            update_field('email', $user->user_email, $post_id);
            update_field('status', 'Manual Entry', $post_id);
            $completed_at  = \DateTime::createFromFormat('!Ymd', $_POST['acf']['field_6377cef804236'], new \DateTimeZone('America/New_York'));
    
            update_field('date_completed', $completed_at->format('Y-m-d H:i:s'), $post_id);
        }
    }, 20);
  • The only way to accomplish this (that I know of) is to use the acf/save_post with a priority of < 10. This means that your filter will run before ACF saves any values to the DB. You would access the fields in $_POST[‘acf’]. You would use these field keys in your update_field() calls using the field key (not the field name). When you are done unset the input array

    
    unset($_POST['acf']);
    

    This last step will prevent ACF from saving anything because nothing exists.

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

You must be logged in to reply to this topic.