Skip to content

Instantly share code, notes, and snippets.

@kjohnson

kjohnson/footer.php

Last active May 9, 2019
Embed
What would you like to do?
[WIP] AJAX request to load a form dynamically.
<?php
add_action( 'init', function(){
if( ! isset( $_REQUEST[ 'async_form' ] ) ) return;
global $wp_scripts;
// Clear default loaded scripts.
unset( $wp_scripts->registered );
$form_id = absint($_REQUEST['form_id']);
ob_start();
$form = do_shortcode( "[ninja_forms id='{$form_id}']");
ob_get_clean();
ob_start();
NF_Display_Render::output_templates();
$templates = ob_get_clean();
$response = [
'form' => $form,
'scripts' => $wp_scripts->registered,
'templates' => $templates
];
echo json_encode( $response );
die();
});
add_action( 'wp_enqueue_scripts', function(){
// Make sure that backbone is enqueued on the page.
wp_enqueue_script( 'backbone' );
});
@VolodymyrTesliuk

This comment has been minimized.

Copy link

@VolodymyrTesliuk VolodymyrTesliuk commented May 9, 2019

Hello, @kjohnson Kyle B. Johnson!
I have a questions for you.
Why we need backbone library? Why you declatated appendChild and never read the value? Explain me please.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.