Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jetsloth/96ecf8a306417878f19c10889d82d1dc to your computer and use it in GitHub Desktop.
Save jetsloth/96ecf8a306417878f19c10889d82d1dc to your computer and use it in GitHub Desktop.
Example of dynamic population for image choices options
<?php
add_filter( 'gform_pre_render', 'custom_image_choices_options' );
add_filter( 'gform_pre_validation', 'custom_image_choices_options' );
add_filter( 'gform_pre_submission_filter', 'custom_image_choices_options' );
add_filter( 'gform_admin_pre_render', 'custom_image_choices_options' );
function custom_image_choices_options( $form ) {
if ( $form['id'] != 9999 ) {// set to the form id you want to run this on
return $form;
}
$posts_query = new WP_Query(array(
'post_type' => 'custom_post_type',
'tax_query' => array(
array(
'taxonomy' => 'custom_taxonomy',
'field' => 'slug',
'terms' => 'lorem-ipsum'
)
),
'posts_per_page' => -1,
'fields' => 'ids'
));
$items = array();
if ( !empty($posts_query->posts) ) {
foreach( $posts_query->posts as $post_id ) {
$items[] = array(
'value' => get_the_title($post_id),// or set to whatever you want the "value" to be (eg $post_id)
'text' => get_the_title($post_id),
'imageChoices_image' => get_the_post_thumbnail_url($post_id, 'medium'),//set to the image URL of the size you want to use
'imageChoices_imageID' => get_post_thumbnail_id($post_id)// set to the image ID
);
}
foreach ( $form['fields'] as &$field ) {
if ( $field->id == 9999 ) {// set to the relevant field id
$field->imageChoices_enableImages = true;
$field->choices = $items;
}
}
}
return $form;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment