Dynamic Populate checkboxes

Hello

Would anyone know a way to dynamically populate gravity form “checkboxes” choices from 2 custom post type fields?

I have a CPT which allows a user to fill in some basic event details, i.e title, date, time.
I then have a form for a user to register for the event.
Each time a new event is added I have to add the event to the CPT and then the event title and date to the registration form.
Is there an easy way to auto populate the choices in checkboxes with the “event title” & “event date” ??

For example…
Event title: loss of vision
Event date: 01/01/2022

in the GF choices label field/line you see:
loss of vision - 01/01/2022

Thanks

// NOTE: update the ‘221’ to the ID of your form
add_filter( 'gform_pre_render_6’, ‘populate_checkbox’ );
add_filter( 'gform_pre_validation_6’, ‘populate_checkbox’ );
add_filter( 'gform_pre_submission_filter_6’, ‘populate_checkbox’ );
add_filter( 'gform_admin_pre_render_6’, ‘populate_checkbox’ );
function populate_checkbox( $form ) {

foreach( $form['fields'] as &$field )  {

    //NOTE: replace 3 with your checkbox field id
    $field_id = 7;
    if ( $field->id != $field_id ) {
        continue;
    }

    // you can add additional parameters here to alter the posts that are retreieved
    // more info: http://codex.wordpress.org/Template_Tags/get_posts
    $posts = get_posts( 'numberposts=-1&post_status=publish' );

    $input_id = 1;
    foreach( $posts as $post ) {

        //skipping index that are multiples of 10 (multiples of 10 create problems as the input IDs)
        if ( $input_id % 10 == 0 ) {
            $input_id++;
        }

        $choices[] = array( 'text' => $post->event_title, 'value' => $post->event_title );
        $inputs[] = array( 'label' => $post->event_title, 'id' => "{$field_id}.{$input_id}" );

        $input_id++;
    }

    $field->choices = $choices;
    $field->inputs = $inputs;

}

return $form;

}

I found the above code on GF website.
When i give it a go i get the following error trying to save the functions.php in wordpress, for the 2nd line down
add_filter( 'gform_pre_render_6’, ‘populate_checkbox’ );

syntax error, unexpected ‘populate_checkbox’ (T_STRING), expecting ‘)’

What do I need to change to fix this?

Also, do you change the event_title, in choices and inputs to the relevant CPT field?

It looks like some of the code has curly quotes – – rather than standard single quotes – '. This will throw a syntax error when running PHP. You’ll want to ensure any instance of that character is properly replaced.

Thanks for that.
It has worked for that part, but then just uncovered more errors.

foreach( $form[‘fields’] as &$field ) {

This error appears for the line above:
syntax error, unexpected ’ foreach’ (T_STRING)

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.