Skip to content

Instantly share code, notes, and snippets.

@ThemeCatcher
Created February 2, 2023 10:27
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 ThemeCatcher/b2e910b6dae037a55e209512ca265fa0 to your computer and use it in GitHub Desktop.
Save ThemeCatcher/b2e910b6dae037a55e209512ca265fa0 to your computer and use it in GitHub Desktop.
Entry count filter to count field values
<?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;
}
@ThemeCatcher
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment