Skip to content

Instantly share code, notes, and snippets.

@patrickfreitasdev
Created January 21, 2024 02:22
Show Gist options
  • Save patrickfreitasdev/eef6a443713bfd0881a8b1598b556952 to your computer and use it in GitHub Desktop.
Save patrickfreitasdev/eef6a443713bfd0881a8b1598b556952 to your computer and use it in GitHub Desktop.
<?php
/**
* Plugin Name: Forminator
* Description: Decrease user balance on form submission.
* Author: Patrick Freitas @ WPMUDEV
* Author URI: https://premium.wpmudev.org
* License: GPLv2 or later
*/
add_filter(
'forminator_custom_form_submit_errors',
function ($submit_errors, $form_id, $field_data_array) {
// Add your form IDs here.
$form_ids = array(8);
// Change this to the message that you want to show.
$message = "You don't have enough balance.";
//$meta_key = 'account_balance'; // edit here if it is a different field name
// chnage meta key based on form id
$meta_key_mapping = [
8 => 'account_balance_form_8',
10 => 'account_balance_form_10',
];
if (in_array(intval($form_id), $form_ids, true)) {
$current_user_id = get_current_user_id();
if ($current_user_id) {
// get meta value from logged in user
//$user_balance = get_user_meta($current_user_id, $meta_key, true);
$user_balance = get_user_meta($current_user_id, $meta_key_mapping[$form_id], true);
// not enough balance
if (intval($user_balance) < 1) {
add_filter('forminator_custom_form_invalid_form_message', function ($invalid_form_message, $module_id) use ($message) {
return $message;
}, 9999, 2);
$submit_errors[]['submit'] = $message;
}else{
// decrease balance
//update_user_meta($current_user_id, $meta_key, intval($user_balance) - 1);
update_user_meta($current_user_id, $meta_key_mapping[$form_id], intval($user_balance) - 1);
}
}
}
return $submit_errors;
},
15,
3
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment