Search code examples
phpwordpressadvanced-custom-fieldsgravity-forms-plugin

ACF and Gravity Forms Plugin Integration (Using a for loop)


I'm using Advanced Custom Fields for Wordpress in combination with a plugin that integrates Gravity Forms.

The plugin makes available a gravity_forms function in which to display a specific gravity form to the frontend.

As per the plugin's documentation, I am using a for loop to loop through all gravity forms and display the form selected in the backend on the page.

My code looks as follows:

if( have_rows('content_area') ):
while ( have_rows('content_area') ) : the_row(); 
    if( get_row_layout() == 'gravity_form' ):
        $form = get_sub_field('form'); 
            foreach($forms as $form) {
                gravity_form($form, true, true, false, '', true, 1); 
    }
    endif;
endwhile;
else :
// do nothing
endif;

When I remove the foreach loop and pass a specific form ID into the gravity_form function (instead of the $form variable) it outputs the correct form to the page.

My PHP skills aren't that great so I suspect it is something I am missing that the documentation hasn't included.

Here are some screenshots of my ACF setup.

enter image description here

enter image description here

it may, or may not be worth noting that I also tried with:

$form = get_field('form');


Solution

  • Remove the foreach because the var $forms do not exists, so it can't works.

    Try instead :

    if( have_rows('content_area') ):
    while ( have_rows('content_area') ) : the_row(); 
        if( get_row_layout() == 'gravity_form' ):
           if( get_row_layout() == 'gravity_form' ):
               gravity_form(get_sub_field('form'), true, true, false, '', true, 1); 
           }
        }
        endif;
    endwhile;
    else :
    // do nothing
    endif;
    

    EDIT: If you are returning more than one form, you can use the foreach but with the right variable so juste replace $form = get_sub_field('form'); by $forms = get_sub_field('form')