Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Hoping someone can assist as I am very new to the whole functions.php/wordpress coding side of things but I basically have the following use case that I am trying to achieve through the use of Gravity Forms plugin as well as some other coding.

Use Case: Present a user with a drop-down/select list with a variety of occasions, for example:

Using WordPress v3.7.1

<select name="input_1" id="input_1_1">
  <option>Christening</option>
  <option>Birthday</option>
  <option>Wedding</option>
</select>

I also have another drop-down/select list that based on what the user selects here, all media library images that have a tag, matching the selection above, will only be displayed in the drop-down list. FYI, I have also installed the plugin called WordPress Media Tags that has provided me the ability to tag all files within my media library.

Now, I have the process to filter the media library into my select list but at the moment I only know how to hard code the tax_query terms value, that I have in my child theme functions.php file, which looks like this:

<?php

add_filter('gform_pre_render_2', 'populate_images');
function populate_images($form){

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

        if($field['type'] != 'select' || strpos($field['cssClass'], 'populate-images') === false)
            continue;

        $args = array(
            'post_type' => 'attachment', 
            'post_status' => 'inherit',
            'order' => 'ASC',
            'posts_per_page' => -1,
            'tax_query' => array(
                    array(
                        'taxonomy' => 'media_tag',
                        'terms' => 'wedding', // want to pass value here as a parameter
                        'field' => 'slug',
                    )
                )
        );

        $loop = new WP_Query($args);
        $images = array(array('text' => '-- Select an Image --', 'value' => ' '));
        foreach ( $loop->posts as $image) { 
            $images[] = array('text' => $image->post_title, 'value' => wp_get_attachment_url( $image->ID ));
        }
        $field['choices'] = $images;                
    }
    return $form;
}

As you can see, I have hardcoded the tax_query array terms with the value of 'wedding', which will return to me all media files with the tag 'wedding'. This I have working.

What I am after but unsure how to do, is that I want to pass as a parameter to my populate_images function, within functions.php, say $term, which will be the value selected from the Occasion's select/drop-down above, so my new tax_query would in actual fact look like:

    'tax_query' => array(
            array(
                'taxonomy' => 'media_tag',
                'terms' => $term, //want to pass value here as a parameter
                'field' => 'slug',
            )
        )

So, I am unsure how to link value from select drop-down, pass into populate_images as the parameter $term and then have the functions.php file perform a new Wp_Query based off $term parameter.

If the user, selects 'birthday' from drop-down, call populate_images with the parameter of 'birthday' and present to the user within the second drop-down, all the images with the tag of 'birthday'.

I guess also, if there is a better means of performing what I am after, using Gravity Forms plugin above, pls let me know.

Hope the above makes sense and really hope someone can point me in the right direction.

Thanks.

share|improve this question

1 Answer 1

The problem is that gform_pre_render is called as the form is being rendered, but the occasion isn't known until the user clicks a value.

If the number of occasions is small and only rarely changing, one solution is to add a drop down for each occasion and then use conditional logic to display only the drop down that corresponds to the occasion that is currently selected. The code on the server just loops over the list of occasions to insert choices for each of the fields:

add_filter('gform_pre_render_2', 'populate_images');

function populate_images($form) {
    foreach ($form['fields'] as &$field) {
        if ($field['type'] !== 'select') {
            foreach (get_occasions() as $occasion) {
                if (is_occasion_field($field, $occasion)) {
                    $field['choices'] = get_occasion_choices($occasion);
                    break;
                }
            }
        }
    }
    return $form;
}

function is_occasion_field($field, $occasion) {
    // Each occasion field has a class of its own.
    $class = 'populate-images-' . $occasion;
    return strpos($field['cssClass'], $class) !== false;
}

function get_occasions() {
    return array('wedding', 'birthday', 'christening');
}

function get_occasion_choices($occasion) {
    $args = array(
        'post_type' => 'attachment',
        'post_status' => 'inherit',
        'order' => 'ASC',
        'posts_per_page' => -1,
        'tax_query' => array(
            array(
                'taxonomy' => 'media_tag',
                'terms' => $occasion,
                'field' => 'slug')));

    $loop = new WP_Query($args);
    $default = array('text' => '-- Select an Image --', 'value' => '');
    $images = array($default);
    foreach ($loop->posts as $image) {
        $images[] = array(
            'text' => $image->post_title,
            'value' => wp_get_attachment_url($image->ID));
    }
    return $images;
}

Other solutions involve automatically adding the extra fields to the form or writing JavaScript code to monitor what the user is selecting.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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