-
-
Save ThemeCatcher/b2e910b6dae037a55e209512ca265fa0 to your computer and use it in GitHub Desktop.
Entry count filter to count field values
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 | |
add_filter('quform_entry_count_1', function () { | |
return my_get_field_value_count('1_15'); | |
}); | |
add_filter('quform_element_valid_1_15', function ($valid, $value, $element) { | |
$form = $element->getForm(); | |
$currentCount = my_get_field_value_count($element->getIdentifier()); | |
$remaining = $form->config('entryLimit') - $currentCount; | |
if ((int) $value > $remaining) { | |
$element->addError("Sorry, there are only $remaining passes left"); | |
$valid = false; | |
} | |
return $valid; | |
}, 10, 3); | |
function my_get_field_value_count($elementId) { | |
global $wpdb; | |
$repository = quform('repository'); | |
list($formId, $elementId) = explode('_', $elementId); | |
$values = $wpdb->get_col( | |
$wpdb->prepare( | |
"SELECT value FROM " . $repository->getEntryDataTableName() . " | |
LEFT JOIN " . $repository->getEntriesTableName() . " ON entry_id = id | |
WHERE form_id = %d AND element_id = %d", | |
$formId, | |
$elementId | |
) | |
); | |
$count = 0; | |
if (is_array($values)) { | |
foreach ($values as $value) { | |
$count += (int) $value; | |
} | |
} | |
return $count; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Replace the number
1
on line 3 with the form ID.On lines 4 and 7, replace
1_15
with the unique ID of the "How many passes?" field.If you are copy & pasting this into another PHP file or plugin, do not copy line 1 or it may cause an error