Gravity Forms support
Chisel includes a lightweight integration with Gravity Forms, providing helper methods for safe form rendering, form list retrieval, and plugin availability checks.
Overview
Chisel’s Gravity Forms integration is minimal and focused on providing safe helper methods to work with Gravity Forms in your theme. The integration consists of:
- Plugin guard: Helper methods that check for Gravity Forms availability before executing
- Form helpers: Safe wrappers for common Gravity Forms operations
- Form list retrieval: Easy access to available forms for use in custom fields or templates
All Gravity Forms features are accessed through the GravityFormsHelpers class located in core/Helpers/GravityFormsHelpers.php
Helper API
GravityFormsHelpers (core/Helpers/GravityFormsHelpers.php)
is_gf_active()
is_gf_active(): bool
Checks if the Gravity Forms plugin is active. Use this method to guard Gravity Forms-specific code paths and prevent errors when the plugin is not installed.
Returns: true if the GFForms class exists, false otherwise
Usage:
<?php
use Chisel\Helpers\GravityFormsHelpers;
if ( GravityFormsHelpers::is_gf_active() ) {
// Gravity Forms-specific code
}PHPget_forms_list()
get_forms_list(): array
Returns a list of all available Gravity Forms as an associative array with form IDs as keys and form titles as values. This is particularly useful for populating select fields in ACF options pages or custom admin interfaces.
Returns: Array in format [form_id => form_title], or empty array if Gravity Forms is not active
Implementation:
- Returns empty array if Gravity Forms is not active
- Uses
GFAPI::get_forms()to retrieve all forms - Maps forms to
[id => title]format
Usage:
<?php
use Chisel\Helpers\GravityFormsHelpers;
$forms = GravityFormsHelpers::get_forms_list();
// Returns: [1 => 'Contact Form', 2 => 'Newsletter Signup', ...]
// Use in ACF field
add_filter( 'acf/load_field/name=form_selector', array( $this, 'load_forms_choices' ) );
public function load_forms_choices( $field ) {
$field['choices'] = GravityFormsHelpers::get_forms_list();
return $field;
}PHPget_form()
get_form( int $form_id, bool $display_title = false, bool $display_description = false, bool $display_inactive = false, ?array $field_values = null, bool $ajax = true, int $tabindex = 0, bool $echo = false ): mixed
Safe wrapper for Gravity Forms’ gravity_form() function. This method generates a Gravity Form with the specified parameters and returns null if Gravity Forms is not active, preventing errors in your theme.
Parameters:
$form_id(int) – The ID of the form to display$display_title(bool) – Whether to display the form title (default:false)$display_description(bool) – Whether to display the form description (default:false)$display_inactive(bool) – Whether to display inactive forms (default:false)$field_values(?array) – Array of field values to pre-populate (default:null)$ajax(bool) – Whether to enable AJAX submission (default:true)$tabindex(int) – Starting tab index for form fields (default:0)$echo(bool) – Whether to echo the form or return it (default:false)
Returns:
- Form HTML string when
$echo = false - Void when
$echo = true(outputs directly) nullif Gravity Forms is not active
Usage:
<?php
use Chisel\Helpers\GravityFormsHelpers;
// Return form HTML
$form_html = GravityFormsHelpers::get_form( 1, true, true, false, null, true );
// Echo form directly
GravityFormsHelpers::get_form( 1, true, false, false, null, true, 0, true );
// Pre-populate fields
$field_values = array(
'first_name' => 'John',
'email' => '[email protected]'
);
$form = GravityFormsHelpers::get_form( 2, false, false, false, $field_values );PHPUsage in Twig:
{# Render form with title #}
{{ fn('Chisel\\Helpers\\GravityFormsHelpers::get_form', 1, true) }}
{# Render form without AJAX #}
{{ fn('Chisel\\Helpers\\GravityFormsHelpers::get_form', 2, false, false, false, null, false) }}TwigEditor and rendering
Gutenberg block: Gravity Forms provides a native “Gravity Forms” block for the block editor. Use this block for most use cases as it provides the best user experience in the editor.
Manual rendering: Use GravityFormsHelpers::get_form() when you need to render a form programmatically:
- In custom PHP templates
- In Twig templates that aren’t block-based
- When you need to pre-populate form fields with dynamic data
- In custom page builders or template logic
AJAX is enabled by default in the get_form() helper for better user experience.
Styling
Gravity Forms styling is handled by the plugin itself through its built-in theme options. Chisel does not override or force specific form styles, allowing you to:
- Use Gravity Forms’ native “Gravity Theme” or custom themes
- Add custom styles in your theme’s CSS
- Leverage Gravity Forms’ extensive styling options
For custom form styling, add your styles to your theme’s SCSS files and target Gravity Forms’ CSS classes.
Best practices
Always check plugin availability
Guard all Gravity Forms code with the is_gf_active() check to prevent errors when the plugin is deactivated:
<?php
if ( GravityFormsHelpers::is_gf_active() ) {
$forms = GravityFormsHelpers::get_forms_list();
}PHPUse the helper methods
Always use the provided helper methods instead of calling Gravity Forms functions directly. The helpers provide:
- Automatic plugin availability checking
- Consistent error handling
- Safe defaults for common operations
Prefer AJAX submissions
The get_form() helper enables AJAX by default ($ajax = true) for better user experience. Only disable AJAX when specifically required by your use case.
Pre-populate strategically
When pre-populating form fields, use the $field_values parameter to pass data from your template context:
<?php
$context = Timber::context();
$field_values = array(
'user_email' => $context['current_user']->user_email,
'user_name' => $context['current_user']->display_name
);
$form = GravityFormsHelpers::get_form( 3, false, false, false, $field_values );PHP