Last active
April 17, 2023 09:07
-
-
Save kjohnson/1c02e6fff6389c5d64be7a43e4eebfdb to your computer and use it in GitHub Desktop.
[WIP] AJAX request to load a form dynamically.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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' ); | |
}); |
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
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.