PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

Using Custom Taxonomies in Dropdown field

  1. I need to create a Dropdown Field in a form. This dropdown field must be populated by a specific list in my custom taxonomy.

    So instead of creating a "category" field I want the visitor to be able to select a custom taxonomy.

    Posted 13 years ago on Thursday January 27, 2011 | Permalink
  2. Hi Jacques,

    There is no way to do this out of the box; however, this is something of personal interested to me so I'd like to take a look and see about getting you a snippet as time allows. Could you do me a favor and send me a message to david@ounceoftalent.com with a link to this post?

    Posted 13 years ago on Thursday January 27, 2011 | Permalink
  3. kim.telenet
    Member

    +1!

    Posted 13 years ago on Sunday January 30, 2011 | Permalink
  4. Hey guys! Any progress on this?

    Posted 13 years ago on Sunday January 30, 2011 | Permalink
  5. Hi Jacques, I have not had an opportunity to looks at this but in the mean time here is some old code where I've done something like this before. I put some notes in here on what you will need to update to make this work with you form. Disclaimer: this has not be tested with the latest Gravity Forms and this level of customization is not covered by standard support so bear with me if you run into any issues.

    http://pastie.org/1515277

    Posted 13 years ago on Monday January 31, 2011 | Permalink
  6. +2

    Posted 13 years ago on Thursday February 3, 2011 | Permalink
  7. I am trying to do something similar (I think). I have a site where each blog post represents a different location. Each location offers different programs. I was hoping to create one "Get More Info" Gform to place within a widget section. I need the Program Drop Down menu to be populated by the content found in the Programs Custom Field box of each blog post. Hopefully that makes sense.

    Is that what the code linked to above is written to do? And if so, are we supposed to place it in the functions.php file?

    Thanks for your help.

    Posted 13 years ago on Thursday February 3, 2011 | Permalink
  8. No, this code populates a drop down with the values of a custom taxonomy. Not post meta/custom field data. However, you could certainly modify this code to pull in custom field values. In either event the code would go in your themes functions.php file.

    Posted 13 years ago on Friday February 4, 2011 | Permalink
  9. David

    I tried the script you suggested but get an error. See - http://www.stayanight.co.za/test-taxonomy

    The script is:

    // update the "18" the ID of your form
    add_action('gform_pre_render_10', 'update_gform');
    function update_gform($form) {

    // update "1" to the ID of your dropdown field,
    // update "project" to the slug of your taxonomy
    $form['fields']['1']['choices'] = ounce_posts_as_choices('type');

    return $form;
    }

    // update '18' to the ID of your form
    add_action('gform_post_submission_10', 'process_gform', 10, 1);
    function process_gform($entry, $form) {

    // update 'project' to your taxonomy slug,
    // update the '13' to the to the field ID of your dropdown
    ounce_add_taxonomy_to_post('type', $entry['1'], $entry['post_id']);

    }

    // generate GF choices array based on taxonomy
    function ounce_taxonomy_as_choices($taxonomy = "categories") {

    $taxonomy = get_terms($taxonomy, 'hide_empty=0');

    $choices[0]['text'] = '';
    $choices[0]['value'] = '';
    $i = 1;
    foreach($taxonomy as $category) {
    $choices[$i]['text'] = $category->name;
    $choices[$i]['value'] = $category->name;
    $i++;
    }

    return $choices;
    }

    // add custom taxonomy category to post
    function ounce_add_taxonomy_to_post($taxonomy = "categories", $value, $post_id) {

    $categories = get_terms($taxonomy, array('hide_empty' => 0));

    foreach($categories as $category) {
    if($category->name == $value) {
    $post = $post_id;
    $id = (int) $category->term_id;
    wp_set_object_terms($post, $id, $taxonomy, TRUE);
    }
    }

    }

    Posted 13 years ago on Friday February 4, 2011 | Permalink
  10. I updated the code to correct this error: http://pastie.org/1529752

    Posted 13 years ago on Friday February 4, 2011 | Permalink
  11. I could use a little help with the sample code in http://pastie.org/1529752 - specifically the ounce_taxonomy_as_choices function and whether it might be possible to tweak this to be called via a gform_field_value_FIELDNAME for a dropdown to populate a custom taxonomy?

    I am creating a plugin that will use GF as the submission system for a rideshare board of sorts. The plugin will create a custom post type, taxonomies and hopefully import the form upon activation. The sample you have based on gform_pre_render_## presents a challenge in that with the import I won't necessarily know the form #.

    Can gform_field_value_dropdownfieldname accept array responses to populate the dropdowns? I'm using the Custom Post Types plugin which makes this selection dead simple for the standard dropdown type, but doesn't appear to support custom field dropdowns (http://wordpress.org/support/topic/plugin-gravity-forms-custom-post-types-cant-get-taxonomies-to-list-out?replies=5)

    Posted 13 years ago on Sunday October 23, 2011 | Permalink
  12. Found elegant solution.

    add_filter("gform_pre_render", array(&$this, "populate_rideshare_type"));
    function populate_rideshare_type($form){
    
        	foreach($form['fields'] as &$field) {
    
            	if($field['type'] != 'post_custom_field' && $field['postCustomFieldName'] != 'idealien_rideshare_type' )
                	continue;
    
    			$rideshare_types = get_terms( 'idealien_rideshare_type', array(
     				'hide_empty' => 0
     			) );
    
            	$choices = array(array('text' => 'Select A Type', 'value' => ' '));
    
    			foreach ( $rideshare_types as $type ) {
    				$choices[] = array('text' => $type ->name, 'value' => $type ->name);
         		}
    
            	$field['choices'] = $choices;
    
        	}
    
        	return $form;
    	}
    Posted 13 years ago on Sunday October 23, 2011 | Permalink
  13. Pretty slick. Do you have an example form online where that's in use? I'm sure many people would be interested in seeing it in action.

    Posted 13 years ago on Monday October 24, 2011 | Permalink
  14. I'll post a link here and on the Idealien projects page once I have finished the re-factor into a stand-alone plugin.

    Just have presentation layer left to work out so that the results display checks the plugin directory before the standard theme folder / template hierarchy, which is a known challenge to solve in a method that will play nice for all themes / etc.

    Right now I've got a series of shortcodes in mind that will then be able to call either from content or archive-posttype.php.

    J

    Posted 13 years ago on Monday October 24, 2011 | Permalink