Skip to content

Instantly share code, notes, and snippets.

@erickarbe
Created October 28, 2021 16:53
Show Gist options
  • Save erickarbe/8486671430136f73cf33ff255ebe6787 to your computer and use it in GitHub Desktop.
Save erickarbe/8486671430136f73cf33ff255ebe6787 to your computer and use it in GitHub Desktop.
Send Gravity Form Data to a Custom Post Type with Advanced Custom Fields (ACF)
// Change "_2" to the ID of the form
add_action( 'gform_after_submission_2', 'add_to_awardpost', 10, 2 );
function add_to_awardpost($entry, $form) {
// Create the variables
$facility_name = rgar($entry, '1' );
$primary_contact_name = rgar($entry, '2' );
$primary_contact_title = rgar($entry, '3');
$primary_contact_email = rgar($entry, '4');
$facility_phone = rgar($entry, '5');
$facility_address = rgar($entry, '6');
$facility_website = rgar($entry, '7');
$social_media_handles = rgar($entry, '12');
$category = rgar($entry, '30');
$individuals_involved = rgar($entry, '14');
$offsite_professionals = rgar($entry, '18');
$golf_fitness_programing = rgar($entry, '17');
$length_of_time = rgar($entry, '16');
$number_of_clients = rgar($entry, '15');
$origin_and_evolution = rgar($entry, '26');
$location_facilities = rgar($entry, '25');
$marketing = rgar($entry, '24');
$engagement_between_sessions = rgar($entry, '23');
$financial_impact = rgar($entry, '22');
$greatest_success = rgar($entry, '21');
$retail = rgar($entry, '20');
$programming_idea = rgar($entry, '19');
$upload_facility_logo = rgar($entry, '27'); // Single Image Upload field
$upload_facility_images = rgar($entry, '31'); // Mulitple Image upload field
$link_to_video = rgar($entry, '29');
// Add to a "Award Winner" post as a draft
$award_post = array(
'post_title' => $facility_name,
'post_content' => '',
'post_type' => 'winners',
'post_status' => 'draft',
'meta_input' => array(
'facility_company_name' => $facility_name,
'primary_contact_name' => $primary_contact_name,
'primary_contact_title' => $primary_contact_title,
'primary_contact_email' => $primary_contact_email,
'facility_company_phone' => $facility_phone,
'facility_company_address' => $facility_address,
'facility_company_website' => $facility_website,
'social_media_handles' => $social_media_handles,
'on_or_off_course' => $category,
'individuals_involved' => $individuals_involved,
'offsite_professionals' => $offsite_professionals,
'golf_fitness_programming' => $golf_fitness_programing,
'length_of_time' => $length_of_time,
'number_of_clients' => $number_of_clients,
'origin_and_evolution' => $origin_and_evolution,
'location_facilities' => $location_facilities,
'marketing' => $marketing,
'engagement_between_sessions' => $engagement_between_sessions,
'financial_impact' => $financial_impact,
'greatest_success' => $greatest_success,
'retail' => $retail,
'programming_idea' => $programming_idea,
'link_to_video' => $link_to_video,
)
);
// Insert the post into the database
$interview_post_id = wp_insert_post( $award_post );
// Upload the single image to the featured image AND the ACF Image field
$filename = $upload_facility_logo;
// Check the type of file. We'll use this as the 'post_mime_type'.
$filetype = wp_check_filetype( basename( $filename ), null );
// Get the path to the upload directory.
$wp_upload_dir = wp_upload_dir();
// Prepare an array of post data for the attachment.
$attachment = array(
'guid' => $wp_upload_dir['url'] . '/' . basename( $filename ),
'post_mime_type' => $filetype['type'],
'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
'post_content' => '',
'post_status' => 'inherit'
);
// Insert the attachment.
$attach_id = wp_insert_attachment( $attachment, $filename, $interview_post_id );
// Make sure that this file is included, as wp_generate_attachment_metadata() depends on it.
require_once( ABSPATH . 'wp-admin/includes/image.php' );
// Generate the metadata for the attachment, and update the database record.
$attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
wp_update_attachment_metadata( $attach_id, $attach_data );
set_post_thumbnail( $interview_post_id, $attach_id );
update_field('upload_facility_logo', $attach_id, $interview_post_id);
// If the user uploaded files to the multiple file area, load them to the ACF Gallery
if($upload_facility_images) {
// Get the field of the gallery.
$gallery_array = get_field('upload_facility_images', $interview_post_id, false);
// Check if it's an array.
if(!is_array($gallery_array)) {
$gallery_array = [];
}
// Foreach image uploaded, upload it to the media library and add it to the gallery.
foreach(json_decode($upload_facility_images, true) as $upload_facility_image) {
$filename = $upload_facility_image;
// Check the type of file. We'll use this as the 'post_mime_type'.
$filetype = wp_check_filetype( basename( $filename ), null );
// Get the path to the upload directory.
$wp_upload_dir = wp_upload_dir();
// Prepare an array of post data for the attachment.
$attachment = array(
'guid' => $wp_upload_dir['url'] . '/' . basename( $filename ),
'post_mime_type' => $filetype['type'],
'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment( $attachment, $filename, $interview_post_id );
// Make sure that this file is included, as wp_generate_attachment_metadata() depends on it.
require_once( ABSPATH . 'wp-admin/includes/image.php' );
// Generate the metadata for the attachment, and update the database record.
$attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
wp_update_attachment_metadata( $attach_id, $attach_data );
// Push to the array
$gallery_array[] = intval($attach_id);
}
update_field( 'upload_facility_images', $gallery_array, intval($interview_post_id) );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment