Skip to content

Instantly share code, notes, and snippets.

@chrisegg
Created April 12, 2024 03:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisegg/13e730d688e63bfa9c533cfecea7a4ea to your computer and use it in GitHub Desktop.
Save chrisegg/13e730d688e63bfa9c533cfecea7a4ea to your computer and use it in GitHub Desktop.
This snippet with check a specific checkbox field in a specific form to see if all choices have been selected, if it they are the entry will be marked as spam.
<?php
//Copy everything below this line
/**
*
* Mark entry as spam if all checkbox choices are selected
*
* This is a unique use case, but if you have a checkbox that selecting all choices is unlikely you can use this as a spam prevention method.
*
*
* @Version: 1.0
* @Author: Chris Eggleston
* @license: GPL-2.0+
* @link:
*
*/
//replace 1 in gform_entry_is_spam_1 with your forms ID number
add_filter( 'gform_entry_is_spam_1', function ( $is_spam, $form, $entry ) {
$checkbox_field_id = '1'; // Replace 1 with your checkbox field ID
// Attempting to accurately count the selected checkboxes
$field = GFFormsModel::get_field( $form, $checkbox_field_id );
if ( ! $field instanceof GF_Field_Checkbox ) {
return $is_spam; // Ensure this is a checkbox field
}
// Initialize count
$selected_count = 0;
// Loop through all inputs (checkboxes) of the field and count the ones that have a value
foreach ( $field->inputs as $input ) {
$input_id = (string) $input['id']; // Cast to string to match array keys
if ( ! empty( $entry[ $input_id ] ) ) {
$selected_count++;
}
}
$total_choices = count( $field->choices );
return $is_spam;
}, 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment