Skip to content

Instantly share code, notes, and snippets.

@Lonsdale201
Created June 5, 2024 20:35
Show Gist options
  • Save Lonsdale201/1c0c07b5c4325e4792f89256ad43c82b to your computer and use it in GitHub Desktop.
Save Lonsdale201/1c0c07b5c4325e4792f89256ad43c82b to your computer and use it in GitHub Desktop.
JetFormBuilder - Call hook check badwords (banlist)
// This hook will connect to a specified field and check its contents.
// It checks the specified "dictionary" to see if there are any words that are in the banned list.
// If so, it does not run through the form, but returns failed. A custom message can be added.
// place the code in the child theme functions.php or a custom code snippets plugin, like FluentSnippets
// How to use. Go to your jetformbuilder form. Add a new Call hook post submit actions top of the action list. Paste the following hook name:
// badword-validator
// HELP IMAGE: https://prnt.sc/R4E5mMrAVOSA
// paste the following code:
// Everyhere in the code change the: my_custom_field_content with your target form field NAME
// overwrite the offensive word array with yout own banned words
use Jet_Form_Builder\Exceptions\Action_Exception;
/**
* Validate the custom field before the form submission.
* If the custom field contains any offensive words, throw an error and prevent the form submission.
*/
add_filter('jet-form-builder/custom-filter/badword-validator', function($result, $request, $action_handler) {
// List of offensive words to check
$offensive_words = array(
'badword1', 'badword2', 'badword3', 'badword4', 'badword5',
'badword6', 'badword7', 'badword8', 'badword9', 'badword10',
'badword11', 'badword12', 'badword13', 'badword14', 'badword15',
'badword16', 'badword17', 'badword18', 'badword19', 'badword20'
);
// Check if the custom field exists and contains any offensive words
if (isset($request['my_custom_field_content'])) {
$content = strtolower($request['my_custom_field_content']); // Convert content to lowercase for case-insensitive comparison
foreach ($offensive_words as $word) {
if (strpos($content, strtolower($word)) !== false) {
// Throw an error with a custom message and the standard 'failed' status
throw new Action_Exception(
__('The provided content contains inappropriate language.'), // Custom error message
'failed' // Standard error status
);
// Return false to indicate validation failure
$result = false;
}
}
}
return $result;
}, 10, 3);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment