Last active
October 3, 2022 08:35
-
-
Save adczk/06f24899b1512e24f4757d7296591bdf to your computer and use it in GitHub Desktop.
Forminator 1.18.2 - show number of remaining submissions for limited "select" fields
This file contains hidden or 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 | |
################################# | |
# | |
# Show number of submissions remaining for SELECT type fields | |
# | |
# use as MU plugin | |
# | |
# tested with Forminator 1.18.2 only | |
# | |
################################# | |
add_filter( 'forminator_cform_render_fields', function( $wrappers, $model_id ) { | |
$GLOBALS['form_id'] = $model_id; | |
function wpmu_forminator_select_limit_display( $html, $id, $required, $options ) { | |
global $form_id; | |
$list_form_ids = array(1177); // set form ID(s) here | |
$select_ids = array( 'select-1' ); // set select field(s) IDs here | |
if( in_array( $form_id, $list_form_ids ) ) | |
{ | |
$tostrip = 'forminator-form-' . $form_id . '__field--'; | |
$field_name_arr = explode( '_', str_replace( $tostrip, '', $id ) ); | |
$field_name = $field_name_arr[0]; | |
if ( in_array( $field_name, $select_ids ) ) { | |
foreach ( $options as $option ) { | |
$option_value = "%"; | |
$option_label = isset( $option[ 'label' ] ) ? $option[ 'label' ] : false; | |
if ( ! $option_value ) { | |
continue; | |
} | |
$entries = Forminator_Form_Entry_Model::select_count_entries_by_meta_field( | |
$form_id, | |
$field_name, | |
$option_value, | |
$option_label | |
); | |
$remaining = (int) $option[ 'limit' ] - $entries; | |
if ( 0 >= $remaining ) { | |
continue; | |
} | |
// adjust message text in two lines below | |
$html .= "<p>" . $option['label']. " submission limit: " . $option['limit']; | |
$html .= " Still another {$remaining} to go, so hurry up!"; | |
} | |
} | |
} | |
return $html; | |
} | |
add_filter ( 'forminator_field_single_markup', 'wpmu_forminator_select_limit_display', 10, 4 ); | |
return $wrappers; | |
}, 10, 2); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment