Created
July 14, 2021 19:43
-
-
Save jorpdesigns/b6e418b58d4f965efb0d31eeeb247203 to your computer and use it in GitHub Desktop.
Snippet to populate Gravity Forms checkbox field with values from custom post type
This file contains hidden or 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 | |
| // Replace 8 with your form ID | |
| add_filter( 'gform_pre_render_8', 'populate_activity_field' ); | |
| add_filter( 'gform_pre_validation_8', 'populate_activity_field' ); | |
| add_filter( 'gform_pre_submission_filter_8', 'populate_activity_field' ); | |
| add_filter( 'gform_admin_pre_render_8', 'populate_activity_field' ); | |
| function populate_activity_field( $form ) { | |
| foreach ( $form['fields'] as &$field ) { | |
| if ( strpos( $field->cssClass, 'activities' ) === false ) { // Replace 'activities' with your field class name | |
| continue; | |
| } | |
| $choices = array(); | |
| $activities = get_posts( 'numberposts=-1&post_type=activity' ); // Replace with your custom post type | |
| $field_id = $field->id; | |
| $input_id = 1; | |
| foreach ( $activities as $activity ) { | |
| if ( $input_id % 10 == 0 ) { | |
| $input_id++; | |
| } | |
| $choices[] = array( 'text' => html_entity_decode($activity->post_title), 'value' => html_entity_decode($activity->post_title) ); | |
| // Labels with post image | |
| // $thumbnail = wp_get_attachment_image_src( get_post_thumbnail_id($activity->ID), 'thumbnail' ); | |
| // $choices[] = array( 'text' => '<img src="' . $thumbnail[0] . '"><span>' . html_entity_decode($activity->post_title) . '</span>', 'value' => html_entity_decode($activity->post_title) ); | |
| $inputs[] = array( 'label' => html_entity_decode($activity->post_title), 'id' => "{$field_id}.{$input_id}" ); | |
| $input_id++; | |
| } | |
| $field->choices = $choices; | |
| $field->inputs = $inputs; | |
| break; | |
| } | |
| return $form; | |
| } | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment