Skip to content

Instantly share code, notes, and snippets.

@alfredo-wpmudev
Created April 25, 2024 00:06
Show Gist options
  • Save alfredo-wpmudev/8e191fea36b6b6eeb998bc8ec5bef714 to your computer and use it in GitHub Desktop.
Save alfredo-wpmudev/8e191fea36b6b6eeb998bc8ec5bef714 to your computer and use it in GitHub Desktop.
Forminator Compare Fist Name and Last Name, if both are the same hide the Submit button
<?php
/**
* Plugin Name: [Forminator Pro] -Compare first name and last name and if equals hide the submit button.
* Plugin URI: https://wpmudev.com/
* Description:Compare first name and last name and if equals hide the submit button.
* Author: Alfredo Galano Loyola @ WPMUDEV
* Author URI: https://wpmudev.com/
* License: GPLv2 or later
*/
if (!defined('ABSPATH')) {
exit;
}
// No need to do anything if the request is via WP-CLI.
if (defined('WP_CLI') && WP_CLI) {
return;
}
if (!class_exists('WPMUDEV_Forminator_Field_Comparison')) {
class WPMUDEV_Forminator_Field_Comparison
{
/**
* $forms used to define the form and the hidden field that will be used. It's possible use the feature in several forms
* [forminator_form id="1049353"]
*/
private $forms = array(1049353 , 1049394);
private $field1 = 'name-1';
private $field2 = 'name-2';
private static $_instance = null;
public static function get_instance()
{
if (is_null(self::$_instance)) {
self::$_instance = new WPMUDEV_Forminator_Field_Comparison();
}
return self::$_instance;
}
private function __construct()
{
$this->init();
}
public function init()
{
// Do some checks here
if (!defined('FORMINATOR_VERSION') || FORMINATOR_VERSION < '1.12' || !class_exists('Forminator_API')) {
return;
}
add_action('wp_footer', array($this, 'customize_hidden_field_js'), 9999);
}
public function customize_hidden_field_js()
{
?>
<script>
jQuery(document ).ready(function() {
_field_1 = "<?php echo $this->field1;?>";
_field_2 = "<?php echo $this->field2;?>";
<?php
foreach ($this->forms as $form_id) {
?>
jQuery("#forminator-module-<?php echo $form_id;?> input, #forminator-module-<?php echo $form_id;?> select").change(function(){
compareFieldsValues("<?php echo $form_id;?>",_field_1, _field_2);
});
<?php
}
?>
function compareFieldsValues(form_id, field1, field2) {
let value1 = jQuery("#forminator-module-"+form_id+" input[name='"+field1+"']").val();
let value2 = jQuery("#forminator-module-"+form_id+" input[name='"+field2+"']").val();
if (value1 === value2) {
jQuery("#forminator-module-"+form_id+" .forminator-button-submit").hide();
console.log(value1 +"----"+value2+"----> Hidden");
}
else{
jQuery("#forminator-module-"+form_id+" .forminator-button-submit").show();
console.log("Showing");
}
}
});
</script>
<?php
}
}
add_action('plugins_loaded', function () {
return WPMUDEV_Forminator_Field_Comparison::get_instance();
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment