Created
February 25, 2021 19:10
-
-
Save wpmudev-sls/72268e0259c07bfd19f20abcc956e717 to your computer and use it in GitHub Desktop.
Forminator] - Auto Check Radio fields
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 | |
/** | |
* Plugin Name: [Forminator] - Auto Check Radio fields | |
* Plugin URI: https://premium.wpmudev.org/ | |
* Description: A workaround to pre-check specified radio fields for specified form id. | |
* Task: SLS-1130 | |
* Author: Panos Lyrakis @ WPMUDEV | |
* Author URI: https://premium.wpmudev.org/ | |
* License: GPLv2 or later | |
*/ | |
if ( ! defined( 'ABSPATH' ) || ( defined( 'WP_CLI' ) && WP_CLI ) ) { | |
return; | |
} | |
add_action( | |
'wp_footer', | |
function() { | |
global $post; | |
if ( ! $post instanceof WP_Post || ! has_shortcode( $post->post_content, 'forminator_form' ) ) { | |
return; | |
} | |
?> | |
<script type="text/javascript"> | |
(($,d)=>{ | |
if ( window.forminator_auto_check_fields ) { | |
return; | |
} | |
window.forminator_auto_check_fields = { | |
form_id: 518, | |
init: function() { | |
var _self = window.forminator_auto_check_fields; | |
$(d).ajaxComplete( function( event, xhr, settings ) { | |
var response_action = _self.get_query_var( 'action', unescape( settings.data ) ), | |
form_id = _self.get_query_var( 'id', unescape( settings.data ) ); | |
if ( 'forminator_load_cform' === response_action && form_id == _self.form_id ) { | |
var radio_fileds_ids = [ 'radio-1', 'radio-2' ]; | |
radio_fileds_ids.forEach(function ( field_id, index ) { | |
$(`#forminator-module-${_self.form_id} input:radio[name=${field_id}]:first`).trigger( 'click' ).prop('checked', false); | |
}); | |
} | |
}); | |
}, | |
get_query_var: function( name, url ) { | |
if ( ! url ) { | |
return false; | |
} | |
name = name.replace(/[\[\]]/g, "\\$&"); | |
var regex = new RegExp(name + "(=([^&#]*)|&|#|$)"), | |
results = regex.exec(url); | |
if (!results) return false; | |
if (!results[2]) return false; | |
return decodeURIComponent( results[2].replace(/\+/g, " ") ); | |
} | |
}; | |
$(d).ready( forminator_auto_check_fields.init ); | |
})(jQuery,document); | |
</script> | |
<?php | |
}, | |
9999 | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment