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.

Get checked checkboxes

  1. escapade_carbet
    Member

    Hello,

    I'm using last version of Gravity Forms (1.4.3.1) and it was very easy to set up a complex and lengthy form. The purpose of the form is to create a custom post type. This part is working thanks to hints I found on this forum. I'm trying now to set up checkboxes that will populate custom taxonomies when values are checked.

    More details here:
    - List A of checkboxes (id=24) for custom taxonomy 'access'
    - List B of checkboxes (id=25) for custom taxonomy 'services'

    Here is my PHP code:

    add_filter("gform_post_submission", "insert_custom_taxonomies",10,2);
    function insert_custom_taxonomies($entry, $form){
    
    	if($form["id"] !=1)
            return;
    
        $post_id = $entry["post_id"];
    
        $choices_services = "";
        $choices_access = "";
    
        foreach($form["fields"] as &$field){
            if($field["id"] == 25){
            	foreach($field["choices"] as $choice){
    	            $field_value = !empty($choice["value"]) || $field["enableChoiceValue"] ? $choice["value"] : $choice["text"];
        	        $choices_services.= $field_value.',';
        		}
            }
    
            if($field["id"] == 24){
            	foreach($field["choices"] as $choice){
    	            $field_value = !empty($choice["value"]) || $field["enableChoiceValue"] ? $choice["value"] : $choice["text"];
        	        $choices_access.= $field_value.',';
        		}
            }
        }
    
        wp_set_object_terms($post_id, explode(",", $choices_service), "service");
        wp_set_object_terms($post_id, explode(",", $choices_access), "access");    
    
    }

    This does not work as my vars 'choices_access' and 'choices_services' are empty before calling 'wp_set_object_terms'.

    I've tried var_dump($choice) for debugging purpose in "foreach" loop, but even if I check a checkbox in the form, "IsSelected" property is set to "false" after submission.

    - Am I using a wrong hook to populate my taxonomies? I've tried 'gform_pre_submission' hook, without any success.
    - Is there a way to know if a checkbox was checked before hitting submission button?

    Anyone can help?
    Thanks!

    Vincent

    Posted 14 years ago on Wednesday September 29, 2010 | Permalink
  2. escapade_carbet
    Member

    I found a solution with a different approach after noticing that $entry array was properly filled with values of checked checkboxes. Here is my code snippet:

    add_filter("gform_post_submission", "insert_custom_taxonomies",10,2);
    function insert_custom_taxonomies($entry, $form){
    
    	if($form["id"] !=1)
            return;
    
        $post_id = $entry["post_id"];
    
        $choices_services = "";
        $choices_access = "";
    
    	$entry_ids = array_keys($entry);
    
    	foreach($entry_ids as $entry_id){
            if(substr($entry_id,0,2) == 25) $choices_services.= $entry[$entry_id].',';
            if(substr($entry_id,0,2) == 24) $choices_access.= $entry[$entry_id].',';
        }
    
        wp_set_object_terms($post_id, explode(",", $choices_services), "service");
        wp_set_object_terms($post_id, explode(",", $choices_access), "access");
    
    }

    I hope this will help somebody else.

    Posted 14 years ago on Thursday September 30, 2010 | Permalink
  3. I am working on a very similar issue, but I am attempting to place checkbox values into custom fields with each value under the same key. It seems like I figured out how to do it but it is not working.
    Here is my code

    <?php
    //Add checkbox info from gravity forms to the custom fields of each post created by each entry
    add_action("gform_post_submission", "set_post_custom_field", 10, 2);
    function set_post_custom_field($entry, $form){
    
    $postid = $entry["$post_id"]; //get post id associated with entry
    $field_ids = array_keys($entry); //get all field ids of entry
    foreach($field_ids as &$field_id){
    	if(substr($field_id,0,2) == 89){    //define $parts as all values from field ids beginning with 89
    $parts = $entry[$field_id];}
    }
    	//updating custom fields with each value from above into same key
    	foreach($parts as $part){
    		if(!empty($part)){
    		update_post_meta($postid, 'part_of_plant_used', $part);
    		}
    		}
    } ?>

    This is pasted into my functions.php.
    I can't see that it would matter, but the post is a custom post type.
    Here is the form page: http://herbalrepertory.throughwoods.com/post-a-new-herb/
    Any help would be greatly appreciated.

    Posted 13 years ago on Tuesday April 19, 2011 | Permalink
  4. I have worked on this more.

    //Add checkbox info from gravity forms to the custom fields of each post
    add_action("gform_post_submission_2", "set_post_custom_field", 10, 2);
    function set_post_custom_field($entry, $form){
    
    $postid = $entry["$post_id"]; //get post id associated with entry 
    
     $parts = "";
    $field_ids = array_keys($entry); //get all field ids of entry
     foreach($field_ids as &$field_id){
    	if(substr($field_id,0,2) == 89){    //define $parts as all values from field ids beginning with 89
    $parts.= $entry[$field_id] . ',';}
    }
    
    $parts = explode(",", $parts);
    	//updating custom fields with each value from above into same key
    	foreach($parts as &$part){
    		if(!empty($part)){
    		update_post_meta($postid, 'part_of_plant_used', $part);
    		}
    		}
    }

    I am getting an error back because $entry is an object and not an array. So the array_keys() function is not working (line 8). Does anyone have another way of getting all the keys of the entry as an array?

    Posted 13 years ago on Tuesday April 19, 2011 | Permalink
  5. Here is a more universal solution that should fulfill your requirements as well as anyone else who is trying to use a checkbox field as a post custom field:

    1. Paste this code into your theme's functions.php:
      http://pastie.org/1813050
    2. Now add a CSS class to the field in the GF admin using the following format:
      custom_field_{custom_field_name}

      So to add the selected checkbox inputs as values for a custom field titled "sports" you would add the CSS class "custom_field_sports".

    That's it. Let me know how it works.

    Posted 13 years ago on Tuesday April 19, 2011 | Permalink
  6. This works great!! Sorry I didn't reply earlier. Thank you so much.

    Posted 13 years ago on Monday May 16, 2011 | Permalink

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