Last active
April 30, 2022 08:06
-
-
Save wpchannel/8fcfa32c5130dcd493478068a791404c to your computer and use it in GitHub Desktop.
Best tweaks for Gravity Forms plugin for creating forms with WordPress. Enhance Bootstrap 4 compatibility and increase performances. More informations in this tutorial: https://wpchannel.com/wordpress/tutoriels-wordpress/astuces-optimiser-formulaires-gravity-forms
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 if (!defined('ABSPATH')) die('Restricted Area'); | |
/* | |
* Plugin Name: Gravity Forms Enhancements | |
* Description: Tweaks for Gravity Forms plugin. | |
* Version: 20191102 | |
* Author: Aurélien Denis | |
* Author URI: https://wpchannel.com/ | |
*/ | |
/* Check if Gravity Forms is available */ | |
include_once(ABSPATH . 'wp-admin/includes/plugin.php'); | |
if (is_plugin_active('gravityforms/gravityforms.php')) { | |
/* JS in Footer */ | |
add_filter('gform_init_scripts_footer', '__return_true'); | |
function wpc_gform_cdata_open($content = '') { | |
$content = 'document.addEventListener("DOMContentLoaded", function() { '; | |
return $content; | |
} | |
add_filter('gform_cdata_open', 'wpc_gform_cdata_open'); | |
function wpc_gform_cdata_close($content = '') { | |
$content = ' }, false);'; | |
return $content; | |
} | |
add_filter('gform_cdata_close', 'wpc_gform_cdata_close'); | |
/* Change Submit Button */ | |
function wpc_gf_input_to_button($button, $form) { | |
$dom = new DOMDocument(); | |
$dom->loadHTML($button); | |
$input = $dom->getElementsByTagName('input')->item(0); | |
$new_button = $dom->createElement('button'); | |
$new_button->appendChild($dom->createTextNode($input->getAttribute('value'))); | |
$input->removeAttribute('value'); | |
foreach($input->attributes as $attribute) { | |
$new_button->setAttribute($attribute->name, $attribute->value); | |
} | |
$input->parentNode->replaceChild($new_button, $input); | |
return $dom->saveHtml($new_button); | |
} | |
add_filter('gform_next_button', 'wpc_gf_input_to_button', 10, 2); | |
add_filter('gform_previous_button', 'wpc_gf_input_to_button', 10, 2); | |
add_filter('gform_submit_button', 'wpc_gf_input_to_button', 10, 2); | |
/* Add Bootstrap 4 Classes on Submit Button */ | |
function wpc_gf_custom_css_classes($button, $form) { | |
$dom = new DOMDocument(); | |
$dom->loadHTML($button); | |
$input = $dom->getElementsByTagName('button')->item(0); | |
$classes = $input->getAttribute('class'); | |
$classes .= " btn btn-primary text-uppercase"; | |
$input->setAttribute('class', $classes); | |
return $dom->saveHtml($input); | |
} | |
add_filter('gform_submit_button', 'wpc_gf_custom_css_classes', 10, 2); | |
/* Add Bootstrap 4 Classes on Fields */ | |
function wpc_gf_bs4($content, $field, $value, $lead_id, $form_id) { | |
if ($field["type"] != 'hidden' && $field["type"] != 'list' && $field["type"] != 'checkbox' && $field["type"] != 'html' && $field["type"] != 'address') { | |
$content = str_replace('class=\'medium', 'class=\'form-control medium', $content); | |
$content = str_replace('class=\'large', 'class=\'form-control large', $content); | |
} | |
if ($field["type"] == 'name' || $field["type"] == 'address') { | |
$content = str_replace('<input ', '<input class=\'form-control\' ', $content); | |
} | |
if ($field["type"] == 'textarea') { | |
$content = str_replace('class=\'textarea', 'class=\'form-control textarea', $content); | |
} | |
if ($field["type"] == 'consent') { | |
$content = str_replace('<input ', '<input class=\'custom-control-input\' ', $content); | |
$content = str_replace('gfield_consent_label', 'gfield_consent_label custom-control-label', $content); | |
} | |
return $content; | |
} | |
add_filter('gform_field_content', 'wpc_gf_bs4', 10, 5); | |
/* Add Bootstrap 4 Validation Message */ | |
function wpc_gf_validation($message, $form) { | |
return "<div class='validation_error alert alert-danger'>" . esc_html__('There was a problem with your submission.', 'gravityforms') . ' ' . esc_html__('Errors have been highlighted below.', 'gravityforms') . '</div>'; | |
} | |
add_filter('gform_validation_message', 'wpc_gf_validation', 10, 2); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment