• edselopez

    (@edselopez)


    I tried asking this question to the folks over at Advanced Custom fields, but they redirected me to this email, as my use case falls under this plugin plugin. Hoping someone can assist with this.

    We have a workflow where we allow a user to save an ACF form submission as draft, before they can proceed further. Here is some code which we are using to save to draft:

    function my_acf_button_ajax($field, $post_id){
    // Check if current user has any posts.
    $post_id = getDraftPost();
    if (!empty($post_id)) {
    if( $_POST['acf'] ) {
    update_post_meta( $post_id, 'entry_form', FORM_KEY );
    update_post_meta( $post_id, 'entry_submission_date', date( 'Y-m-d H:i:s' ) );
    acf_update_values( $_POST['acf'], $post_id );
    }
    }
    else {
    $post = [
    'post_status' => 'draft',
    'post_type' => 'af_entry',
    ];
    
    // insert the post
    $post_id = wp_insert_post($post);
    if ($_POST['acf'] && $post_id) {
    update_post_meta( $post_id, 'entry_form', FORM_KEY );
    update_post_meta( $post_id, 'entry_submission_date', date( 'Y-m-d H:i:s' ) );
    acf_update_values($_POST['acf'], $post_id);
    }
    }
    // Set post ID which we will use during submission, and other places.
    wp_send_json_success("Draft has been saved!");
    }

    Here is the getDraftPost() function, which simply looks for the most recent draft for the current logged in user.

    function getDraftPost($status = 'draft') : int {
    global $current_user;
    // Check if current user has any posts.
    $args = [
    'author' => $current_user->ID,
    'orderby' => 'post_date',
    'order' => 'DESC',
    'post_type' => 'af_entry',
    'post_status' => $status,
    ];
    
    $current_user_posts = get_posts( $args );
    
    if (!empty($current_user_posts)) {
    return $current_user_posts[0]->ID;
    }
    return 0;
    }

    And finally, the code used to populate defaults, if a draft is found.

    function set_draft_values( $field ) {
    $post_id = getDraftPost();
    
    if (!empty($post_id)) {
    $fieldValue = get_post_meta($post_id, $field['name'], TRUE);
    if (!empty($fieldValue)) {
    $field['default_value'] = $fieldValue;
    }
    }
    
    return $field;
    }
    add_filter('acf/load_field', 'set_draft_values');

    As you can see, we are using the acf/load_field to populate the default values. All of this works, but we are noticing that sometimes, values that are saved in drafts for one user, are populated on forms that are being submitted by another user altogether. There is no sharing of computers, and this happens completely at random for maytbe 1/50 submissions. Is there a better way to therefore populate the form with values saved from a draft where this pollution of data doesn’t happen?

    Few things to note:

    1. We are using the latest version of ACF pro: 6.2.3
    2. We are using Advanced Forms as we have a multi-page layout, where we are displaying the ACF fields in tabs.
    3. The form is only accessible to logged in users, so we’ll always have the User ID for the current logged in user.
    • This topic was modified 12 months ago by edselopez.
  • The topic ‘Help regarding saving as draft’ is closed to new replies.