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.

gform_pre_render errors when simply trying to get form ID

  1. keeneyemedia
    Member

    This should be simple... I'm trying to grab the form ID on the current page using gform_pre_render and then based on the form ID within the function gform_pre_render is calling do some work... (I don't always know which form is going to be loaded on the page) - so here's the simple version of the code:

    $i_this_form_id = add_filter("gform_pre_render", "update_the_fields");
    echo $i_this_form_id;
    
    function update_the_fields ($form){
    	return $form["id"];
    }

    The thing is - no matter what I do or don't do with the gform_pre_render - it just puts a whole bunch of " Invalid argument supplied for foreach()" errors on my form. BUT the echo in there returns the correct form ID.
    Is there a better way to make the determination of which form loaded on a page by ID so I can then run the correct gform_field_value_* operation on the form? This seems to work but there's all those errors where the form goes - why?

    Posted 11 years ago on Monday December 17, 2012 | Permalink
  2. I've never seen the WordPress function "add_filter" used like that.

    The gform_pre_render filter is designed to set the value of a field, not return a variable. Can you explain how different forms may be loaded on the page but you do not know the form ID? Maybe that will help me come up with a solution with you.

    Posted 11 years ago on Tuesday December 18, 2012 | Permalink
  3. keeneyemedia
    Member

    This code (and a bunch more) lives in an include across a series of multiple pages. Based on the page that loads there's a different form. Since this is include code - I need to know WHICH form is on that page and then do certain things based on the form ID.
    And here's the thing... line 2 above WORKS - the displayed value is the form ID.
    The PRESENCE of the add_filter (setting a variable or not) causes the errors

    add_filter("gform_pre_render", "update_the_fields");

    results in many:
    Warning: Invalid argument supplied for foreach() in /home/thisguy/public_html/wp-content/plugins/gravityforms/form_display.php on line X or Y or Z

    Ultimately what I need to do is invoke a series of add_filter("gform_field_value_FIELDX", "populate_FIELDX"); for the unique fields on each unique form.

    Posted 11 years ago on Tuesday December 18, 2012 | Permalink
  4. keeneyemedia
    Member

    I decided to not try and return the form ID as it appears you can't set a variable (global declaration does not work) outside an add_filter call in WP (not a G form thing).

    So - instead inside the "gform_pre_render" I'm getting the form ID and then via a case/switch I'm calling various "gform_field_value_FIELDid" and doing what I need to do - then the form renders with all the fields the way I want them...

    Work fine.

    Thanks!

    Posted 11 years ago on Tuesday December 18, 2012 | Permalink