cedaro / gravity-forms-iframe

A Gravity Forms add-on to embed a form in an auto-resizing iframe on external sites.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Alternative method

opened this issue · comments

Hey there,

I've recently had to try do this for one of my clients and tried a few methods and eventually got it working.

During the process, after talking with the support team, they pointed me to your plugin which I held as a fall back if I couldn't get what I was doing working.

I was wondering if you could have a look at what I have done, and if you can see why I should instead use the iframe method this plugin uses as my PHP skills aren't great!

I used the switch_to_blog function in my method like so:

<?php
global $blog_id;
$current_blog_id = $blog_id; ?>
<?php if ($blog_id == '1') { ?>

   <?php echo do_shortcode('[gravityform id="16" name="Contact a Distributor" ajax="true"]'); ?>
           
<?php } elseif ($blog_id == '2') {?>
   <?php switch_to_blog(1); ?>

       <?php echo do_shortcode('[gravityform id="17" name="Contact a Distributor" ajax="true"]'); ?>

   <?php restore_current_blog(); ?>
<?php } elseif ($blog_id == '4') {?>
   <?php switch_to_blog(1); ?>

       <?php echo do_shortcode('[gravityform id="18" name="Contact a Distributor" ajax="true"]'); ?>
       
   <?php restore_current_blog(); ?>
<?php } ?>

This is on all 3 of my sub-sites, and basically switches to blog 1, which is where ALL the forms exist to display the form.

This worked but wouldn't allow the submissions to go through, which I solved by using this function:

// Custom gravity post url
add_filter("gform_form_tag", "form_tag", 10, 2);
function form_tag($form_tag, $form) {
    // If the current blog ID is equal to the one that the forms exists on, and the current form ID is that of a form that exists on the blog with the forms
    if( get_current_blog_id() == 1 && ($form["id"] == 17 || $form["id"] == 18)) {
        // Replace the action URL with the base url of the subsite the forms actually are on (in this case the base url)
        $form_tag = preg_replace("|action='(.*?)'|", "action='/'", $form_tag);
    }

    return $form_tag;
}

This just gets the post action url and sets it to the one the forms actually exist on, meaning the data gets posted to the right place. (for anyone wanting to use this, make sure you set the current blog id = to the blog the forms actually exist on and the form ID's to the forms that sit on the sub-sites and the action replacement to the base URL of the sub-site the forms exist on.

Thanks, Harry.

So I've been talking to the Gravity forms guys and this method won't work as changing the action hook causes the validation to break!

Closing due to a lack of activity. If this is still a problem, please feel free to reopen or start a new issue.