-
-
Save ThemeCatcher/606691dfccb14f19e6d91cf69a89e956 to your computer and use it in GitHub Desktop.
Remove radio option after 55 submissions
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 | |
/* | |
* Plugin Name: Quform Custom Code | |
* Description: Custom code for Quform. | |
* Version: 1.0.0 | |
*/ | |
// Remove the radio option before the form is displayed if the option is no longer available | |
add_action('quform_pre_display_1', function (Quform_Form $form) { | |
$count = my_get_radio_value_count('1_3', 'option_2'); | |
if ($count >= 55) { | |
$radioElement = $form->getElement('quform_1_3'); | |
$options = $radioElement->getOptions(); | |
foreach ($options as $key => $option) { | |
if ($option['value'] == 'option_2') { | |
unset($options[$key]); | |
} | |
} | |
$radioElement->setOptions($options); | |
} | |
}); | |
// Prevent submission and show an error if the option is no longer available | |
add_filter('quform_element_valid_1_3', function ($valid, $value, $element) { | |
if ($value == 'option_2' && my_get_radio_value_count('1_3', 'option_2') >= 55) { | |
$element->setError('option_2 is no longer available'); | |
$valid = false; | |
} | |
return $valid; | |
}, 10, 3); | |
// Return a count of the number of entries where $elementId has $value | |
function my_get_radio_value_count($elementId, $value) | |
{ | |
global $wpdb; | |
$repository = Quform::getService('repository'); | |
list($formId, $elementId) = explode('_', $elementId); | |
$query = $wpdb->prepare( | |
"SELECT COUNT(*) FROM " . $repository->getEntriesTableName() . " LEFT JOIN " . $repository->getEntryDataTableName() . " ON id = entry_id WHERE form_id = %d AND element_id = %d AND value = %s;", | |
$formId, | |
$elementId, | |
$value | |
); | |
return (int) $wpdb->get_var($query); | |
} |
Hi, I want to use this code. Can u tell me in which folder I have to place the file?
Thanks in advance,
Tim
You can add the code to the WP theme functions.php file or see this page for other options.
Thanks! Im gonna try it.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
1
with the form ID1_3
with the unique ID of the Radio/Select fieldoption_2
with the value of the option you want to limit55
with the maximum allowed selections for that optionYou can duplicate lines 9-34 and change all of the IDs within the duplicated code as above to apply the limiting to other values.