Skip to content

Instantly share code, notes, and snippets.

@kjohnson
Last active April 17, 2023 09:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kjohnson/1c02e6fff6389c5d64be7a43e4eebfdb to your computer and use it in GitHub Desktop.
Save kjohnson/1c02e6fff6389c5d64be7a43e4eebfdb to your computer and use it in GitHub Desktop.
[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
Copy link

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.

@MGParisi
Copy link

MGParisi commented Apr 17, 2023

It would be very nice if you included conditional logic in the examples so that we don't kill our user experience with slow page loads from unnecessary scripts and styles from loading.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment