Skip to content

Instantly share code, notes, and snippets.

@isuke01
Last active May 28, 2018
Embed
What would you like to do?
Wordpress + GravityForm + VUE + React etc
<?php
// Hook up the AJAX actions
add_action( 'wp_ajax_nopriv_gf_button_get_form', 'gf_button_ajax_get_form' );
add_action( 'wp_ajax_gf_button_get_form', 'gf_button_ajax_get_form' );
function gf_button_ajax_get_form(){
$form_id = isset( $_GET['form_id'] ) ? absint( $_GET['form_id'] ) : 0;
/*
used in case if you need additional dynamic fields
$to = (isset( $_GET['to'] ) && $_GET['to'] ) ? $_GET['to'] : null;
$from = (isset( $_GET['from'] ) && $_GET['from'] ) ? $_GET['from'] : null;
*/
$ajax = true ;
$fields = null;
/* $fields = array(
'to_address' => $to,
'from_address' => $from,
); */
// Render an AJAX-enabled form.
// https://www.gravityhelp.com/documentation/article/embedding-a-form/#function-call
gravity_form( $form_id, true, true, false, $fields , $ajax );
die();
}
/*
Override shortcode to hook form
*/
function overwrite_shortcode()
{
function custom_gform_code( $atts ) {
$formID = $atts['id'];
ob_start();
?>
<div id="gf_button_form_container_<?php echo $formID ?>"></div>
<a class="call-form" style="display: none;" data-to="" data-form="<?php echo $formID ?>"></a>
<?php
$return = ob_get_clean();
return $return;
}
remove_shortcode('gravityform');
add_shortcode( 'gravityform', 'custom_gform_code' );
}
add_action( 'wp_loaded', 'overwrite_shortcode' );
/**
* Gravity forms
*/
if(function_exists('gravity_form_enqueue_scripts')){
gravity_form_enqueue_scripts( 1, true );
}
let gformLoader = jQuery(document).find('.call-form');
//console.log('query: ', this.$route.query);
if (gformLoader.length) {
let to = '';
let from = '';
if(this.$route.query.send_from){
from = this.$route.query.send_from;
}
if(this.$route.query.send_to){
to = this.$route.query.send_to;
}
var fromID = jQuery(gformLoader).attr('data-form');
jQuery(document).find('#gf_button_form_container_'+fromID).html('Loading form ...').fadeIn();
jQuery.get(rtwp.ajaxurl + '?action=gf_button_get_form&to='+to+'&from='+from+'&form_id=' + fromID, function (response) {
jQuery(document).find('#gf_button_form_container_' + fromID).html(response);
jQuery(gformLoader).remove();
if (window['gformInitDatepicker']) { gformInitDatepicker(); }
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment