Hello there @elbradey
This will require the addition of a hidden field that stores the user ID. And then need to fetch all the entries for that user ID to find the selected options, to disable/remove that option from the select field. The following MU plugin should help, please install it first in a staging/dev environment for testing, here’s how: https://wpmudev.com/docs/using-wordpress/installing-wordpress-plugins/#installing-mu-plugins
Please mind changing the form ID 2910
in the code with the form ID of your installation (same as the integer number found in the shortcode).
Also, we are assuming hidden field ID as hidden-1
and select field ID as select-1
, so please also change them if needed.
<?php
add_filter( 'forminator_cform_render_fields', function( $wrappers, $model_id ) {
if( ! is_user_logged_in() ){
return $wrappers;
}
if( $model_id != 2910 ){
return $wrappers;
}
$select_fields_data = array(
'select-1' => 'options',
);
foreach ( $wrappers as $wrapper_key => $wrapper ) {
if ( ! isset( $wrapper[ 'fields' ] ) ) {
continue;
}
if ( isset( $select_fields_data[ $wrapper[ 'fields' ][ 0 ][ 'element_id' ] ] ) && ! empty( $select_fields_data[ $wrapper[ 'fields' ][ 0 ][ 'element_id' ] ] ) ) {
$user_id = get_current_user_id();
$last_entry = wpmudev_get_entries_by_user( $model_id, $user_id );
if ( ! empty( $last_entry ) ) {
foreach ( $last_entry as $l_entry ) {
if ( ! empty( $l_entry->entry_id ) ) {
$entry = Forminator_API::get_entry( $model_id, $l_entry->entry_id );
if ( ! empty( $entry ) ) {
if ( ! empty( $entry->meta_data['select-1'] ) ) {
$selected_value = $entry->meta_data['select-1']['value'];
if ( ! empty( $selected_value ) ) {
foreach ( $wrappers[ $wrapper_key ][ 'fields' ][ 0 ][ 'options' ] as $opt_key => $opt_val ) {
if ( $opt_val['label'] == $selected_value ) {
unset( $wrappers[ $wrapper_key ][ 'fields' ][ 0 ][ 'options' ][$opt_key] );
}
}
}
}
}
}
}
}
}
}
return $wrappers;
},10,2);
function wpmudev_get_entries_by_user( $form_id, $user_id ){
global $wpdb;
$table_name = Forminator_Database_Tables::get_table_name( Forminator_Database_Tables::FORM_ENTRY_META );
$entry_table_name = Forminator_Database_Tables::get_table_name( Forminator_Database_Tables::FORM_ENTRY );
$sql = "SELECT m.<code>entry_id</code> FROM {$table_name} m LEFT JOIN {$entry_table_name} e ON(e.<code>entry_id</code> = m.<code>entry_id</code>) WHERE e.<code>form_id</code> = %d AND m.<code>meta_key</code> = %s AND m.<code>meta_value</code> = %s order by m.<code>meta_id</code>";
$entry_id = $wpdb->get_results( $wpdb->prepare( $sql, $form_id, 'hidden-1', $user_id ) );
if ( ! empty( $entry_id ) ) {
return $entry_id;
}
return false;
}
Thank you,
Dimitris