Gravity Forms to ACF Pattern

When you use Gravity Forms to make a post, you can provision ACF fields but what I found was that the data wasn’t showing up correctly until I manually went and updated the created post. I tried using the WordPress wp_update_post() function but found that didn’t do it.

I ended up taking a look at the post_meta in the database directly.1 That’s something that I find myself doing more and more. When you can look directly at evidence, do that. Don’t assume. So what I saw was this . . .
Screenshot of Sequel Pro showing the metadata associated with the post that Gravity Forms created.
You can see that the base custom fields are there. The data is visible.

Now I hit update and refreshed the database view and saw lots of new custom fields get generated. This data associates the human readable fields with the field keys that ACF creates. Note the underscores which prevent those fields from showing up in the backend of WP even if you have view custom fields selected.
New view showing all the new metadata fields that get generated.

I feel like this has something to do with acf/save_post but couldn’t figure out how to make that work. When my knowledge fails,2 I resort to force.

First, I turned on the ability to see the field keys in ACF. It’s easy to miss and easy to forget that this is a Screen Option in WP.
Showing the screen option at the top of the page that allows you to see the ACF field keys.

Once I could see that clearly, I felt typing in all that junk would take a while and be a place where I could make dumb errors. So I cut and pasted the data into a Google Sheet and used my old text manipulation tools to build the function that would build the text I needed.

="update_field("&$D$1&C2&$D$1&","&$D$1&D2&$D$1&",$post_id);"

That was cut/pasted into the plugin and hooked to the after submission hook that Gravity Forms has.

add_action( 'gform_after_submission_1', 'asphs_update_post_content', 10, 2 );

That triggers this function and everything now behaves.

function asphs_update_post_content( $entry, $form ) {
    //getting post
        $post_id = get_post( $entry['post_id'] );
        update_field("_personal_information_first_name","field_5b672aa250437",$post_id);
	update_field("_personal_information_last_name","field_5b672aa850438",$post_id);
	update_field("_personal_information_email","field_5b677443f6a3b",$post_id);
	update_field("_personal_information_phone_number","field_5b672ab050439",$post_id);
	update_field("_personal_information_university_affiliation","field_5b672ab75043a",$post_id);
	update_field("_personal_information_private","field_5b672afd156eb",$post_id);
	update_field("_personal_information_biography","field_5b672adeed779",$post_id);
	update_field("_personal_information_profile_picture","field_5b672c956690c",$post_id);
	update_field("_personal_information_expertise","field_5b9871dce76ec",$post_id);
	update_field("_personal_information","field_5b672a9150436",$post_id);
	update_field("_location_street_address","field_5b6727dba1a94",$post_id);
	update_field("_location_city","field_5b6727e5a1a95",$post_id);
	update_field("_location_state_province","field_5b9daddced8c0",$post_id);
	update_field("_location_zip_code","field_5b6727eba1a96",$post_id);
	update_field("_location_country","field_5b6727f5a1a97",$post_id);
	update_field("_location","field_5b6727c3a1a93",$post_id);
    $i = wp_update_post( $post_id );

}

1 I use Sequel Pro.

2 And many, many Google searches . . .