• Resolved Agencia 221B

    (@agencia221b)


    Hi there!

    I’m needing to limit submission in forms by one submission per logged user.

    I found a snippet here in support that supposely works, but for me it isn’t. Here’s the snippet:

    <?php
    
    add_filter(
    	'forminator_render_form_markup',
    	function( $html, $form_fields, $form_type, $form_settings, $form_design, $render_id ) {
    		if ( is_user_logged_in() ) {
    			global $wpdb;
    			$user_id_field_name = 'hidden-1';
    			$user_id            = get_current_user_id();
    			$message            = __( 'You can submit this form only once' );
    
    			$user_entries = $wpdb->get_var(
    				$wpdb->prepare(
    					"SELECT COUNT(e.entry_id) FROM {$wpdb->prefix}frmt_form_entry as e
    					INNER JOIN {$wpdb->prefix}frmt_form_entry_meta as m ON e.entry_id=m.entry_id
    					WHERE m.meta_key=%s AND m.meta_value=%d 
    					LIMIT 1",
    					$user_id_field_name,
    					$user_id
    				)
    			);
    
    			if ( ! empty( $user_entries ) || 0 < $user_entries ) {
    				return $message;
    			}
    		}
    
    		return $html;
    	},
    	10,
    	6
    );

    This snippet is found in this thread: https://www.ads-software.com/support/topic/limit-submission-by-user/

    My problem is that right now this snippet is locking every logged user for every form in my site, regardless if the form have the ‘hidden-3’ filed set to user id or not.

    Please, can you guys help me to solve it? TIA

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Support Zafer – WPMU DEV Support

    (@wpmudevsupport15)

    Hi @agencia221b,

    I hope you are doing well today!

    regardless if the form have the ‘hidden-3’ filed set to user id or not.

    As I can see the hidden form field’s name on your form for User ID is hidden-3. Therefore, you need to adjust the code accordingly.

    Your form will need to have a Hidden field added with default value set to “User ID”. If the field is named hidden-1, no adjustments are needed.

    If the field has a different name, you’ll need to change the line:

    $user_id_field_name = 'hidden-1';

    To match the identifier of the field.

    Please let us know if you need further help or clarification on this.

    Kind regards,
    Zafer

    Thread Starter Agencia 221B

    (@agencia221b)

    Hi Zafer. Thank for your answer.

    I’m so sorry, I failed to mention that in the code that I’ve uploaded to my site I already edited this field name to ‘hidden-3’ to match the forms that I want the submission limit.

    But the problem is that even the forms that doesn’t have hidden fields are being block for logged in users to fill it. Like a simple ‘Talk to us’ form in the footer… If the user is logged in the site, all forms stop to accept submission, regardless to have hidden fields with user or id or even if the user never submitted to that form.

    As soon as I upload this snippet to mu-plugins folder, all forms stops to accept submission by logged in users.

    Hi @agencia221b,

    Hope this message finds you well. Could you test the new snippet below? Kindly, replace the 9 in this line:

    if ( $form_settings['form_id'] != 9 ) {

    With your own form ID, you will be able to get the form ID when you edit the form in the URL:

    https://prnt.sc/9ZqhuQwD17K1

    add_filter(
    	'forminator_render_form_markup',
    	function( $html, $form_fields, $form_type, $form_settings, $form_design, $render_id ) {
            if ( $form_settings['form_id'] != 9 ) {
                return $html;
            }
    		if ( is_user_logged_in() ) {
    			global $wpdb;
    			$user_id_field_name = 'hidden-3';
    			$user_id            = get_current_user_id();
    			$message            = __( 'You can submit this form only once' );
    
    			$user_entries = $wpdb->get_var(
    				$wpdb->prepare(
    					"SELECT COUNT(e.entry_id) FROM {$wpdb->prefix}frmt_form_entry as e
    					INNER JOIN {$wpdb->prefix}frmt_form_entry_meta as m ON e.entry_id=m.entry_id
    					WHERE m.meta_key=%s AND m.meta_value=%d 
    					LIMIT 1",
    					$user_id_field_name,
    					$user_id
    				)
    			);
    
    			if ( ! empty( $user_entries ) || 0 < $user_entries ) {
    				return $message;
    			}
    		}
    
    		return $html;
    	},
    	10,
    	6
    );

    Also, please double check that the User ID value is set in the hidden field:

    https://prnt.sc/XvZbPdkJvx8V

    Let us know the results.

    Best regards,
    Laura

    Thread Starter Agencia 221B

    (@agencia221b)

    Yes, now it works perfectly! Thank you so much.

    But (i’m sorry) I need to be able to add this functionality to more than one form… right now I can only target one form ID (replacing the 9 as oriented)… I tried to alter the code to allow more than one ID, but I failed greatly and broke my site, lol.

    Could you do me this last help and edit the snippet to allow multiple form ID’s so the functionality works and more than one form, please? TIA

    Plugin Support Nithin – WPMU DEV Support

    (@wpmudevsupport11)

    Hi @agencia221b,

    Please check and see whether the following helps:

    <?php
    
    add_filter(
    	'forminator_render_form_markup',
    	function( $html, $form_fields, $form_type, $form_settings, $form_design, $render_id ) {
    
            // Define an array of form IDs
            $allowed_form_ids = array(9, 10, 15); // Add your form IDs here
    
            // If current form ID is not in the allowed list, return original HTML
            if ( !in_array($form_settings['form_id'], $allowed_form_ids) ) {
                return $html;
            }
    
    		if ( is_user_logged_in() ) {
    			global $wpdb;
    			$user_id_field_name = 'hidden-3';
    			$user_id            = get_current_user_id();
    			$message            = __( 'You can submit this form only once' );
    
    			$user_entries = $wpdb->get_var(
    				$wpdb->prepare(
    					"SELECT COUNT(e.entry_id) FROM {$wpdb->prefix}frmt_form_entry as e
    					INNER JOIN {$wpdb->prefix}frmt_form_entry_meta as m ON e.entry_id=m.entry_id
    					WHERE m.meta_key=%s AND m.meta_value=%d 
    					LIMIT 1",
    					$user_id_field_name,
    					$user_id
    				)
    			);
    
    			if (!empty($user_entries) || 0 < $user_entries ) {
    				return $message;
    			}
    		}
    
    		return $html;
    	},
    	10,
    	6
    );
    

    In the above code, the following line is where multiple form IDs can be added:
    $allowed_form_ids = array(9, 10, 15);

    Where 9, and 10,15 in the above example would be the form ID, you can add multiple IDs as above.

    Please check and see whether it helps.

    Best Regards,

    Nithin

    Thread Starter Agencia 221B

    (@agencia221b)

    Perfect!

    Works flawlessly.

    You guys rock, best form plugin and best support.

    Thanks!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Limit one submission by logged user’ is closed to new replies.