Load gravity form via ajax using do_shortcode
I am reloading my form using ajax upon confirmation.
This is working perfectly and I am parsing my form_id
to the action function fine...
// code to trigger stuff when forms have submitted
$(document).on('gform_confirmation_loaded', function(event, form_id){
// run code for white label site editor form
if(form_id == 12) {
// reload our form
$.ajax({
cache: false,
timeout: 30000,
url: admin_ajax_url,
type: 'GET',
data: {
action : 'gf_reload_editor_form',
post_id : post_id,
form_id : form_id,
isAjax : true
},
success: function (data) {
// remove the old confirmed form
$('#gform_ajax_wrapper_'+form_id).remove();
// reload form into our site editor aside
$('#site_editor').prepend(data);
}
});
}
});
The above code is working great.
The problem is reloading the form using do_shortcode
.
class WhiteLabel
{
public function __construct()
{
// ajax reload form actions
add_action('wp_ajax_nopriv_gf_reload_editor_form', [ $this, 'gf_reload_editor_form' ], 20 );
add_action('wp_ajax_gf_reload_editor_form', [ $this, 'gf_reload_editor_form' ], 20 );
}
public function gf_reload_editor_form ()
{
// get the form id
$form_id = $_GET['form_id'];
// do shortcode to output gravity form
echo do_shortcode('[gravityform id="'.$form_id.'" title="false" description="false" ajax="true" update]');
die();
}
}
new WhiteLabel();
OK, so everything above looks hunky dori, that is because it is. I've tested my other custom do_shortcode
functions and they output fine.
The problem lies with the do_shortcode('[gravityform ... ]')
You are probably wondering why don't I just use the gravity_form()
php function...
gravity_form( $form_id, false, false, false, '', true );
This works fine too! This reloads a perfectly working ajax form.
The issue is, I need to use do_shortcode
because I need to include the update
property. The update
property can't be used with the gravity_form()
php function.
The update
property is introduced using Gravity Forms: Post Update plugin which enables my form to update the current post, rather than create a new one.
So this got me thinking, when I am loading the form using the same do_shortcode
method on the initial page load, I am using do_shortcode
inside the loop. This is how the form must know what post data to get and pre-populate my form inputs with etc.
So I tried this..
public function gf_reload_editor_form ()
{
// get the post id and set post data
$post_id = $_GET['post_id'];
$post = get_post($post_id);
setup_postdata($post);
// get the form id
$form_id = $_GET['form_id'];
// do shortcode to output gravity form
echo do_shortcode('[gravityform id="'.$form_id.'" title="false" description="false" ajax="true" update]');
die();
}
..thinking the the do_shortcode
might think it's inside the loop. But still, this did not work either.
Any ideas please would be great thanks as I am stumped.
Topic plugin-gravity-forms shortcode Wordpress
Category Web