Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save wpmudev-sls/faef1ac88d140c4d2e96f647a0be85f8 to your computer and use it in GitHub Desktop.

Select an option

Save wpmudev-sls/faef1ac88d140c4d2e96f647a0be85f8 to your computer and use it in GitHub Desktop.
[Forminator] Examples of custom messages for payment errors
<?php
/**
* Plugin Name: [Forminator] Examples of custom messages for payment errors
* Plugin URI: https://wpmudev.com/
* Description: IMPORTANT: you can replace the "alerts" with your custom code like modals or pop-ups.
* Author: Glauber Silva @ WPMUDEV
* Author URI: https://wpmudev.com/
* Task: SLS-2024
* License: GPLv2 or later
*
* @package Forminator_Examples_Of_Custom_Messages_For_Payment_Errorss
*/
defined( 'ABSPATH' ) || exit;
function wpmudev_forminator_custom_msg_payment_errors() {
ob_start();
?>
<script type="text/javascript">
function readyHandler() {
function maybeDisplayCustomMsg( target ) {
if ( ! target.hasAttribute('tabindex') ) {
return false;
}
var erros = target.querySelectorAll( 'span' );
if ( typeof( erros ) != 'undefined' && erros != null ) {
for ( var n = 0, max = erros.length; n < max; n++ ) {
var ERROR = erros[n].textContent;
//console.log('Error: ', ERROR);
setTimeout( function(){
if ( ERROR.includes( 'Error! Something went wrong when verifying the payment' ) ) {
alert( 'CUSTOM MSG FOR: ' + ERROR ); // You can replace it with your custom code like modals or pop-ups.
}
if ( ERROR.includes( 'PayPal amount must be greater than 0.' ) ) {
alert( 'CUSTOM MSG FOR: ' + ERROR ); // You can replace it with your custom code like modals or pop-ups.
}
}, 100 );
}
}
}
function observeThis( target ) {
// Create an observer instance
var observer = new MutationObserver( function( mutations ) {
//console.log('Target Changed: ', target);
maybeDisplayCustomMsg( target );
});
// Configuration of the observer
var config = {
attributes: true,
childList: true,
characterData: true
};
// Pass in the target node, as well as the observer options
observer.observe( target, config );
}
var errorBoxes = document.querySelectorAll( '.forminator-error' );
if ( typeof( errorBoxes ) != 'undefined' && errorBoxes != null ) {
for ( var n = 0, max = errorBoxes.length; n < max; n++ ) {
observeThis( errorBoxes[ n ] );
}
}
}
// Check if the DOMContentLoaded has already been completed
if ( document.readyState !== 'loading' ) {
readyHandler();
} else {
document.addEventListener( 'DOMContentLoaded', readyHandler );
}
</script>
<?php
$javascript = ob_get_clean();
echo $javascript; // phpcs:ignore
}
add_filter( 'wp_footer', 'wpmudev_forminator_custom_msg_payment_errors', PHP_INT_MAX );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment