Skip to content

Instantly share code, notes, and snippets.

@ThemeCatcher
Last active February 23, 2021 12:30
Show Gist options
  • Save ThemeCatcher/606691dfccb14f19e6d91cf69a89e956 to your computer and use it in GitHub Desktop.
Save ThemeCatcher/606691dfccb14f19e6d91cf69a89e956 to your computer and use it in GitHub Desktop.
Remove radio option after 55 submissions
<?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);
}
@Plutiim
Copy link

Plutiim commented Feb 22, 2021

Hi, I want to use this code. Can u tell me in which folder I have to place the file?

Thanks in advance,
Tim

@ThemeCatcher
Copy link
Author

You can add the code to the WP theme functions.php file or see this page for other options.

@Plutiim
Copy link

Plutiim commented Feb 23, 2021

Thanks! Im gonna try it.

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