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.

Dynamically populate checkboxes

  1. I'd like to have, based on the URL that the form is embedded on, dynamically populate the checkbox field.

    The net effect I want is that if the user arrives on /contact/ it wouldn't check the box for "trouble ticket", but, if they went to /report-an-issue/ it would.

    Is there any way to do that?

    http://www.wolfsnap.com/contact/

    Posted 13 years ago on Sunday February 27, 2011 | Permalink
    • I set up my checkbox field values like so: http://grab.by/9bu8
    • I set up my checkbox dynamic population option like so: http://grab.by/9bua (under advanced tab)
    • I set up a little code snippet that uses the gform_field_value_*parameter* hook to pre-populate the correct checkbox: http://pastie.org/1612873
    • Let me know if you have any questions. :)

    Posted 13 years ago on Sunday February 27, 2011 | Permalink
  2. You rock!

    Silly question, where did you put the code? I'm guessing the page template file?

    Posted 13 years ago on Sunday February 27, 2011 | Permalink
  3. Actually put that right in your functions.php file. Let me know if you have any other questions. :)

    Posted 13 years ago on Sunday February 27, 2011 | Permalink
  4. cool.. Thanks!

    Posted 13 years ago on Sunday February 27, 2011 | Permalink
  5. Anonymous
    Unregistered

    Hmm, this is NOT working for me. My code:

    add_action("gform_field_value_project_need_cat", "gf_populate_project_need_cat");

    function gf_populate_project_need_cat($value){
    if ($_GET['post_id']) {
    $needs = get_the_terms( $_GET['post_id'], 'need' );
    print_r( $needs[0]->slug);
    return $needs[0]->slug;}
    else return $value; }

    I have the checkboxes set up with the correct values, I'm getting the correct slug back from $needs, and the form field is set to populate dynamically. Yet nothing is happening. I can dynamically populate other kinds of fields just fine, but checkboxes aren;t working.

    Two more questions:
    1. Most examples of pre-populating in this forum use add_filter(gform_field_value_whatever), why are you using add_action?
    2. I know real documentation is time consuming, but could you at least publish a list of hooks? It would save a lot of hunting and guessing!

    Thanks!

    Posted 13 years ago on Thursday March 3, 2011 | Permalink
  6. Hi Mobi,

    Can you share a screenshot of how you have your checkbox field setup?

    In regards to your other questions:

    1 - add_filter() is correct; add_action() will still interpret this as a filter and work appropriately, but it was just a typo in my code.
    2 - We are very close to releasing the new documentation which has extensive coverage of all the available Gravity Form hooks (even the GF Add-ons!). Sorry for the wait, but just a little longer now. :)

    Posted 13 years ago on Thursday March 3, 2011 | Permalink
  7. a question related to this topic, i have a page with 4 links.

    lets say when the first link is click, it will be redirected to page where the form is, and 1 out of 4 checkbox should be selected..

    now, if i click the 2nd link, it will be redirected to page where the form is, and 2 out of 4 checkbox should be selected.. and so on and so forth..

    how can i do that?

    Posted 12 years ago on Friday July 29, 2011 | Permalink
  8. charger15
    Member

    @webpuzzlemaster:

    I was in a very similar situation and solved the problem like this. This code is placed inside index.php

    //Give every link an ID (in my case it was post ID as I show all posts on one site) and add it at the end of the url as query string
    $addLink = get_page_link(16)."?ref=".$post->ID;
    echo "<div class='add'><a href='$addLink'>A link to the form</a></div>";

    I then could get the parameter value on the site where the form was placed and use it to prepopulate certain fields with values from the post I was referring to in the link. For example (code inside functions.php):

    // populate category
    add_filter("gform_field_value_record_link_category", "usr_populate_record_link_category");
    function usr_populate_record_link_category(){
      $ref_id = $_GET['ref'];
    
      if(isset($ref_id)){
        $post = get_the_category($ref_id);
        $category = $post[0]->cat_ID;
        return $category;
      }
    }
    
    // populate title
    add_filter("gform_field_value_record_link_title", "usr_populate_record_link_title");
    function usr_populate_record_link_title(){
      $ref_id = $_GET['ref'];
    
      if(isset($ref_id)){
        $post = get_post($ref_id);
        $title = $post->post_title;
        return $title;
      }
    }
    
    // set parent post
    add_filter("gform_post_data", "usr_set_post_parent", 10, 2);
    function usr_set_post_parent($post_data, $form){
      $ref_id = $_GET['ref'];
    
      if($form["id"] == 1){
        $post_data["post_parent"] = "$ref_id";
      }
      return $post_data;
    }

    Hope this helps.

    Posted 12 years ago on Saturday August 27, 2011 | Permalink
  9. I have the same issue as mobitinker.
    I am trying to populate dropdowns with - for testing purposes - just the string "boom".

    i assigned the parameter language_populate to my drop down field (under "populate this dynamically"), and defined following function in my functions.php:

    <?php
    add_action('gform_field_value_language_populate', 'get_language_populate');
    function get_language_populate($value){
        return 'boom!';
    }
    ?>

    though, I am experiencing the same problem as mobitinker:
    single lines of text get populated normally, but my drop-down field keeps the default values (first choice, second choice, etc) and is not getting populated.

    any ideas why this is happening?

    thanks ahead, regards,

    kubante

    Posted 12 years ago on Friday October 7, 2011 | Permalink
  10. Hi Kubante,

    The method you're using would select that option by default if the value exists; however, to add "new" values or overwrite the existing values in a drop down, take a look at this approach:

    http://www.gravityhelp.com/documentation/page/Dynamically_Populating_Drop_Down_Fields

    Posted 12 years ago on Monday October 10, 2011 | Permalink
  11. Hi David,
    thanks for your Documentation Link. Blame on my I didn't have a look there first.

    After some tries, i could successfully dynamically populate my drop-down. I am struggling with the query of the posts though. After a few hours of tries I decided to come back to you, maybe you have an idea.

    I am using This as reference, for populating my dropdown.

    So far so good, my titles from custom_posts show up. I am using post_type=myCustomPostType for this. I would now like to filter the titles, to only those of a certain taxonomy. For this I use the function get_posts like this:

    [php]
    $posts = get_posts('taxonomy=myTaxonomy&tag_ID=22&post_type=myCustomPostType');

    Unfortunately, this always queries all of the custom posts, and not only those in the taxonomy i would like. The filter doesn't apply.

    I also tried the variant with the building of the args array:

    $args = array(
    
    	'post_type' => 'myCustomPostType',
    	'tag_ID' => '22',
    	'post_status' => 'publish'
    );

    i even tried the 'suppress_filters' => false, argument, coz i had read about it in connection to the get_posts() function. In addition, i tried swapping get_posts() with query_posts(). Still no Luck .
    any ideas why my filters are not being applied?

    Thanks ahead and kind regards,

    kubante

    Posted 12 years ago on Tuesday October 11, 2011 | Permalink
  12. @kubante, does this information help at all, using the $args array as you have it up there?

    Custom Post Type Category (Taxonomy)
    http://codex.wordpress.org/Template_Tags/get_posts#Custom_Post_Type_Category_.28Taxonomy.29

    Posted 12 years ago on Wednesday October 12, 2011 | Permalink
  13. Hi Chris,

    thanks for your answer. Yes it works. I am able to filter one post title only with the slug filtering.

    How can I now filter everything from one taxonomy only? or as in my case, even from one taxonomy entry (e.g. "german") ? I have a taxonomy called languages, filled with languages (German, English, etc). I would like to filter e.g. only "german titles".

    I had another few tries with the example underneath (Custom Post Type Category (Taxonomy) ), but no luck... All post titles come up unfiltered. This is driving me crazy ! :)

    $args=array(
    
    	'numberposts' => 8,
    	'orderby' => 'rand',
      	'post_type' => 'myCustomPostType',
      	'myCustomPostType_category' => 'Languages' ,
      	'post_status' => 'publish'
    
    );

    any other ideas? Help totally appreciated!! :)

    Thanks !

    kubante

    Posted 12 years ago on Wednesday October 12, 2011 | Permalink
  14. I even tried using new WP_Query. Solved one problem, another one came up! Again, driving me crazy. :)

    Now i can filter correctly the languages i want, but unfortunately, only one post is paginated!

    http://codex.wordpress.org/Class_Reference/WP_Query

    is explaining that

    Pagination Parameters

    showposts (int) - number of post to show per page. Deprecated as of Version 2.1 in favor of 'posts_per_page'.
    posts_per_page (int) - number of post to show per page (available with Version 2.1). Use 'posts_per_page'=>-1 to show all posts. Note if the query is in a feed, wordpress overwrites this parameter with the stored 'posts_per_rss' option. To reimpose the limit, try using the 'post_limits' filter, or filter 'pre_option_posts_per_rss' and return -1

    Unfortunately this doesn't work. I am not able to override neither with post_limits nor with pre_option_blabla returned to -1.

    I also noticed that the form field is now not anymore editable via the wysiwig Gravity forms section in wp-admin. Which is not a big issue for me, I wouldnt need to edit anything anymore for this field from the wysiwig, if it worked. :)

    Any idea?

    my retrieval code looks like this at the moment:

    $args = array(
    	'tax_query' => array(
    		array(
    			'taxonomy' => 'languages',
    			'post_limits' => -1,
    			'field' => 'slug',
    			'terms' => 'deutsch'
    		)
    	)
    );
    
    $posts = new WP_Query( $args );

    As said, correct filtering, but only one post title retrieved.

    any help super appreciated.

    regards,

    kubante

    Posted 12 years ago on Wednesday October 12, 2011 | Permalink
  15. Solved!!!!

    Thanks to

    http://return-true.com/2008/12/using-wp_query-to-make-custom-queries-within-wordpress-templates/

    The solution is to have following little switches:

    $temp = $wp_query;
    $wp_query = null;
    $wp_query = new WP_Query();
    $wp_query->query($args);
    $temp_posts = $posts;
    $posts = null;
    $posts = $wp_query->posts;

    you can then use any tags to filter or do whatever you want!! (check my previous posts, or answer this post if you want the complete code, i will paste it!)

    thanks god. Love you guys.

    cu,

    kubante

    Posted 12 years ago on Wednesday October 12, 2011 | Permalink

This topic has been resolved and has been closed to new replies.