Skip to content

Instantly share code, notes, and snippets.

@jetsloth
Created January 15, 2019 03:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jetsloth/bfffcd9925e2378a3546b002e1873f4f to your computer and use it in GitHub Desktop.
Save jetsloth/bfffcd9925e2378a3546b002e1873f4f to your computer and use it in GitHub Desktop.
Merge tag filter to enable display of selected image choice in email notifications
<?php
/*
If you use the merge tag {all_fields} this will always display the image
Or if you use individual field merge tags, eg {Options:1} you can display the image by adding a modifier, eg {Options:1:image}
You can also specify the size that the image should display at by adding a pixel size in the following format, eg {Options:1:image_50px}
By default your image will display at its natural size
*/
add_filter( 'gform_merge_tag_filter', 'image_choices_merge_tag_image', 11, 5 );
function image_choices_merge_tag_image( $value, $merge_tag, $modifier, $field, $raw_value ) {
if ( ($merge_tag != 'all_fields' && substr($modifier, 0, 5) != 'image') ) {
return $value;
}
if ( !is_object( $field ) || !property_exists($field, 'imageChoices_enableImages') || empty($field->imageChoices_enableImages)) {
return $value;
}
if ($field->type == 'radio') {
$text = RGFormsModel::get_choice_text($field, $value);
$image = '';
foreach($field->choices as $choice) {
if ($choice['value'] != $value) {
continue;
}
if (!empty($choice['imageChoices_image'])) {
$image = $choice['imageChoices_image'];
}
}
if (!empty($image)) {
$size = '';
if (strlen($modifier) > 5 && substr($modifier, 0, 6) == 'image_') {
$size = 'style="width:'.substr($modifier, 6).'; height:auto; max-width:100%;"';
}
$value = '<img src="'.$image.'" alt="'.$text.'" '.$size.' />';
}
}
return $value;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment