UPDATE 22 May 2021: The plugin has been updated to work with Gravity Forms 184.108.40.206
If you read my post last week, which demonstrates how to display Gravity Forms in WordPress admin pages, it may have got you thinking about all the possibilities it opens up. Perhaps you realised that it’s also possible to load Gravity Forms via WordPress AJAX since that also runs in the admin context.
Of course Gravity Forms has long supported AJAX-enabled forms with the shortcode, but what if you don’t know which form to load until the user selects an option from a list? Or perhaps you only want to load the form only if a certain button is clicked? In this case the form has to be loaded via AJAX not during the first page request.
I’ve put together a rudimentary plugin to demonstrate how to load Gravity Forms via WordPress AJAX. It renders a button along with an empty container ready for the form. When the button is clicked the form is loaded into the container. It supports multiple forms both in admin and in front-end pages.
Here’s an example of how to use it followed by a functioning implementation.
[gravityforms action="button" id=1 text="Load the contact form"]
Notice the sense of tighter integration with Gravity Forms that comes from using the core [gravityforms] shortcode instead of inventing a new one.
A couple of caveats:
1. The single file upload field won’t work but the multi-file field will.
2. Remember this is just a demonstration and a proof of concept intended to help point you in the right direction. If you intend to use the code in a production environment please adjust it according to your needs.
The complete plugin is on GitHub if you’d like to follow or fork it.
If you find this useful, or if you implement a different twist on the idea, please do let me know in the comments below.