Hi everyone,
I have a form that uses File Upload Pro for two fields (Hoofdafbeelding ID:18 and Detailfoto’s ID:19)
Field 18 has to add the Featured Image to a woocommerce product through Advanced Post Creation add-on, and Field 19 does the same for the woocommerce gallery. But I can’t figure out how to make this work.
I also tried to hook into the function to do it manually, but that didn’t work either:
add_action(‘gform_advancedpostcreation_post_after_creation’, function($post_id, $feed, $entry, $form) {
// Only run for form 14
if ($form['id'] != 14) {
return;
}
/**
* -----------------------------
* FEATURED IMAGE (FIELD 18)
* -----------------------------
*/
$featured = rgar($entry, '18');
if (!empty($featured)) {
// File Upload Pro often returns array or string
$featured_url = is_array($featured) ? reset($featured) : $featured;
$featured_id = attachment_url_to_postid($featured_url);
if ($featured_id) {
set_post_thumbnail($post_id, $featured_id);
}
}
/**
* -----------------------------
* GALLERY IMAGES (FIELD 19)
* -----------------------------
*/
$gallery = rgar($entry, '19');
$ids = [];
if (!empty($gallery)) {
// Normalize to array
$gallery_items = is_array($gallery) ? $gallery : [$gallery];
foreach ($gallery_items as $item) {
$url = is_array($item) ? reset($item) : $item;
$attachment_id = attachment_url_to_postid($url);
if ($attachment_id) {
$ids[] = $attachment_id;
}
}
if (!empty($ids)) {
update_post_meta($post_id, '_product_image_gallery', implode(',', $ids));
}
}
}, 10, 4);