Last active
May 25, 2021 01:53
-
-
Save jetsloth/15156d8e78a23a475476d86267d48638 to your computer and use it in GitHub Desktop.
Set the post meta field to selected image choices URL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_filter( 'gform_post_data', 'gform_set_post_meta_to_image_choices_urls', 10, 3 ); | |
function gform_set_post_meta_to_image_choices_urls( $post_data, $form, $entry ) { | |
foreach( $form['fields'] as $field ) { | |
$value = RGFormsModel::get_lead_field_value( $entry, $field ); | |
if ( $field->type != 'post_custom_field' || !property_exists($field, 'postCustomFieldName') || empty($field->postCustomFieldName) || !property_exists($field, 'imageChoices_enableImages') || empty($field->imageChoices_enableImages) || !isset($post_data['post_custom_fields'][$field->postCustomFieldName]) ) { | |
// if it's not a custom field, doesn't use image choices, or no custom field name configured, skip | |
continue; | |
} | |
$images = []; | |
$i = 1; | |
foreach( $field->choices as $choice ) { | |
if ( is_array($value) ) { | |
$key = $field->id . '.' . $i; | |
if ( !isset($value[$key]) || $value[$key] != $choice['value'] || !isset($choice['imageChoices_imageID']) || empty($choice['imageChoices_imageID']) ) { | |
// if this isn't the selected choice, skip | |
$i++; | |
continue; | |
} | |
} | |
else if ( $value == '' || $value != $choice['value'] || !isset($choice['imageChoices_imageID']) || empty($choice['imageChoices_imageID']) ) { | |
// if this isn't the selected choice, skip | |
$i++; | |
continue; | |
} | |
$image_id = $choice['imageChoices_imageID']; | |
$image_src = wp_get_attachment_image_src( $image_id, 'large' );// set to desired size | |
$images[] = $image_src[0]; | |
$i++; | |
} | |
if ( count($images) == 0 ) { | |
continue; | |
} | |
$post_data['post_custom_fields'][$field->postCustomFieldName] = (count($images) > 1) ? implode(",", $images) : $images[0]; | |
} | |
return $post_data; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment