Gravity Population


I made this blog because I keep reusing code snippets for different things and I keep needing to put them somewhere incase I forget what I did. It not only saves time, but lets me share what I’ve learned. Remember preschool: “Sharing is Caring!” So I get to test out my tumblr theme’s interaction with prettify by sharing two snippets for Gravity Forms. If you do not know what Gravity Forms is you should, because it’s awesome. Basically, it’s a super-awesome form plugin for WordPress (Yes, I just said awesome three times) which can be used for anything from a contact form to an entire custom post type work flow solution (aka eManager). The first snippet is a pretty popular one, which populates a dropdown select field with a custom post type. The second does the same, but with a taxonomy. Cool right?

Populate with a Post Type


<?php
add_filter("gform_pre_render", "gform_prepopluate_populate_books");
add_filter("gform_admin_pre_render", "gform_prepopluate_populate_books");

function gform_prepopluate_populate_books($form){
    $posttype = 'books'; // Post type to query
    $formid = 3; // Form Id
    $fieldid = 8; // Field Id

    if($form["id"] != $formid)
        return $form;

    $posts = query_posts( array( 'post_type' => $posttype ) );
    $items = array(); /* Add Blank */
    $items[] = array("text" => "", "value" => "");

    foreach($posts as $post)
        $items[] = array("value" => $post->ID, "text" => $post->post_title);

    foreach($form["fields"] as &$field)
        if($field["id"] == $fieldid ){            
            $field["choices"] = $items;
        }
    return $form;

}

Populate with a Taxonomy

<?php

add_filter("gform_pre_render", "gform_prepopluate_populate_books");
add_filter("gform_admin_pre_render", "gform_prepopluate_populate_books");

function gform_prepopluate_populate_books($form){

    //Grab all Terms Associated with a Specific Taxonomy;
    global $post;
    $taxonomy = 'genres';
    $formid = 5;
    $fieldid = 14;

    if($form["id"] != $formid)
        return $form;
    $terms = get_terms($taxonomy, 'hide_empty=0&orderby=none');

    //Creating drop down item array.
    $items = array();

    //Adding initial blank value.
    $items[] = array("text" => "", "value" => "");

    //Adding term names to the items array
    foreach($terms as $term){
        $is_selected = $term->name == "testing" ? true : false;
        $items[] = array("value" => $term->name, "text" => $term->name, "isSelected"=> $is_selected);
    }

    //Adding items to field id 1. Replace 1 with your actual field id. You can get the field id by looking at the input name in the markup.
    foreach($form["fields"] as &$field)
        if($field["id"] == $fieldid){
            $field["type"] = "select";
            $field["choices"] = $items;
        }

    return $form;
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.