1

I'm using Gravity Forms to create several checkbox sections on my form each pre-populated with a specific taxonomy term from a custom post type.

I found an example of how to do this on the Gravity Forms site: https://www.gravityhelp.com/documentation/article/dynamically-populating-drop-down-fields/ and have modified it work as a checkbox and show the post thumbnails, but the example only works for a single checkbox.

The only way I know how to have this work for all the site taxonomies is to copy/paste the function and modify. This results in a bloated functions.php file. I figure there may be a way to do a 'foreach' loop for each of the taxonomy terms, but I am not good with foreach loops and have no idea how it would be constructed.

This is an example of the code I have copy/pasted for each instance of the taxonomy term:

add_filter( 'gform_pre_render_3', 'list_diesel_heater' );
add_filter( 'gform_pre_validation_3', 'list_diesel_heater' );
add_filter( 'gform_pre_submission_filter_3', 'list_diesel_heater' );
add_filter( 'gform_admin_pre_render_3', 'list_diesel_heater' );
function list_diesel_heater( $form ) {

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

    if ( $field->type != 'checkbox' || strpos( $field->cssClass, 'diesel-heater' ) === false ) {
        continue;
    }

    $posts = get_posts( 'numberposts=-1&post_type=product&product_cat=diesel-heaters' );

    $choices = array();

    foreach ( $posts as $post ) {
        $thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'thumbnail' );
        $url = $thumb['0'];
        $choices[] = array( 'text' => '<img src="' . $url . '"><p>'. $post->post_title .'</p>', 'value' => $post->post_title );
    }

    $field->placeholder = 'Select a Post';
    $field->choices = $choices;
}
return $form;
}

I was hoping it would be possible to have a foreach loop within the function and have the instances of 'diesel-heater' replaced with the taxonomy term. I am not good with foreach loops and was hoping someone could please provide some guidance.

5
  • You can ask this question using gravity forms support. Plugin related questions are off topic here at the moment. Commented Jul 24, 2015 at 6:00
  • While I am using the Gravity Forms plugin, the question revolves more around creating a for each loop using a taxonomy term query. I'm not asking for Gravity support, I'm looking for help to dynamically insert the taxonomy terms (the example shows diesel-heaters) from a custom post type. Commented Jul 24, 2015 at 19:32
  • Can the solution be tested without using Gravity forms? Commented Jul 25, 2015 at 6:02
  • I don't know the solution, so I am not sure if it can be tested without Gravity Forms. Gravity Forms support said they could not help. Commented Jul 25, 2015 at 22:39
  • 1
    @chriswhiteley despite this question relating strictly to Gravity Forms, which is considered off-topic here, I've posted an answer for you. It's not simply that they could not help but rather they don't want to help you, which is piss poor for a premium plugin you've paid for considering that your request is fairly basic.
    – Adam
    Commented Jul 26, 2015 at 5:18

1 Answer 1

2

In your foreach statement you are both trying to get and assign properties on a non-object, in essence, the $field variable is actually not an object, but rather an array. I don't know why the example code on the Gravity Forms website is using object notation, perhaps the underlying API has changed.

Anyway here is what you need:

add_filter( 'gform_pre_render_3', 'list_diesel_heater' );
add_filter( 'gform_pre_validation_3', 'list_diesel_heater' );
add_filter( 'gform_pre_submission_filter_3', 'list_diesel_heater' );
add_filter( 'gform_admin_pre_render_3', 'list_diesel_heater' );
function list_diesel_heater( $form ) {

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

        if ( $field['type'] != 'checkbox' ) {
            continue;
        }

        $posts = get_posts( 'numberposts=-1&post_type=product&product_cat=clothing' );

        $choices = array();

        foreach ( $posts as $post ) {
            $thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'thumbnail' );
            $choices[] = array( 'text' => '<img src="' . $thumb['0'] . '"><p>'. $post->post_title .'</p>', 'value' => $post->post_title );
        }

        $field['placeholder'] = 'Select a Post';
        $field['choices'] = $choices;

    }

    return $form;

}

I have changed the product_cat above and removed the second statement in the first conditional so as to test this code on my WooCommerce development envioronment using the WooCommerce dummy data XML file, therefore you need to change things back to your specifications.

Here is an example of the output:

enter image description here

You will need to style the output yourself accordingly

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.