How to limit the number of form submissions based on a user role?

By: Joe Mateus | Asked: 01/01/2023
ForumsCategory: How-toHow to limit the number of form submissions based on a user role?
Joe Mateus asked 1 year ago
I have two types of user roles (public and business) when users register for an account using a registration form (form#1). I have  a form ("form#2") that I want to limit the amount of times it can be submitted based on the user role. For example the maximum number of form#2 that can be submitted for user role "public" will be limited to 50. The maximum number of form#2 that can be submitted for a user role of "business" will be limited to 100.  How can I achieve this?
Joe Mateus replied 1 year ago

Just an update I was given this code by FF subport to "Limit the number of entries per user".

I missed this code while searching for an answer.

I think this would do, at least for me. Hope it can help others as well.


add_action('frm_display_form_action', 'check_entry_count', 8, 3);
function check_entry_count($params, $fields, $form){
global $user_ID;
remove_filter('frm_continue_to_new', '__return_false', 50);
if($form->id == 5 and !is_admin()){ //replace 5 with the ID of your form
$count = FrmEntry::getRecordCount("form_id=". $form->id ." AND user_id=".$user_ID);
if($count >= 2){ //change 2 to your entry limit
echo 'This form is closed';
add_filter('frm_continue_to_new', '__return_false', 50);
}
}
}

Victor Font replied 1 year ago

This code does nothing about user roles. It bases the count off of the WordPress user id. You still have to add code to test if the users are business or public.

Joe Mateus replied 1 year ago

Yes, you are correct, but at least with this code I can put a cap on the same number of form submissions across the board even though it's the same for both public and business. It takes away their ability to be able to submit unlimited submissions.

I really do wish and prefer that I was able to separate the number of submissions based on the user role instead.

This is now a work in progress for the foreseeable future for me.

If worse comes to worse, I might just hire a developer.

Victor Font replied 1 year ago

You can get the code from this snippet to get the user role: https://formidable-masterminds.com/conditionally-require-fields-by-user-role/

Victor Font replied 1 year ago

Here's the code Formidable gave modified for the business and public user roles. https://gist.github.com/vfontjr/5d072ab98c24568ca3a1ab9a690ef3ed

David BN replied 11 months ago

<p>Thanks Victor and Joe for mentioned this ones, could we make the count entries in a specifc range of date or current day? then it could do limit number of submission by user role per day or in a period</p>

1 Answers
Victor Font answered 1 year ago
This is complicated and requires custom PHP code and makes two assumptions. Assumption 1 is that all users are registered in WordPress, and 2, that you are capturing user_id in your form entries. The custom code needs to fire early in the form display process, perhaps in the frm_setup_new_fields_vars or frm_before_display_content filter. Your custom function needs to access the wp_user object to return the value of the current user's WordPress ID, then using the user's ID as a parameter, run a SQL statement that counts the number of entries to return a value. If the value is equal to to your target goal, then reroute the page to a message that says they've reached the threshold. The user_id field is in wp_frm_items. Here is the schema: https://formidableforms.com/knowledgebase/database-schema/
Joe Mateus replied 1 year ago

Thanks Victor, hmmm.....yes, that does seem overly complicated.
Maybe I should just rethink what I want the form to do and not go overboard.
Instead of having separate limits for the two different user roles, which seems to be the major issue here, can I instead put a limit of say 100 entries per user no matter the user role?

Victor Font replied 1 year ago

You could, but you still have to count previous entries to make sure they are <=100. It doesn't matter what number you choose, the process is the same. Another possibility is to create a custom user meta field to store the entry count, but that means you have to update the field with the new count every time an entry is saved.

It wouldn't take very long for a developer to do this for you. If you need help, please search our Developers Directory at <a href="https://formidable-masterminds.com/developers-directory/&quot; rel="nofollow">https://formidable-masterminds.com/developers-directory/</a&gt;.

Michael ClarkMichael Clark replied 1 year ago

It'd be nice to have a Formidable extension that expanded the Form Limits in Form Settings with conditional logic based on user role and user meta:

"Limit number of entries based on User Role.." and "If value in User Meta "custom_meta_selector_here" is =< "50", then close form and display message..

🙂

Victor Font replied 1 year ago

I suppose if we were to design something like this, user role would probably have to be some kind of multi-select that wouldn't be subject to a single role/count.

There would have to be a way to assure every entry created updates the count.

What about entry deletions? Does the count decline?

It might be helpful to have a count reset if the count somehow gets off.

Any other ideas about how this could work?

Making the Best WordPress Plugin even better - Together

Take on bigger projects with confidence knowing you have access to an entire community of Formidable Experts and Professionals who have your back when the going gets tough. You got this!
Join the community
crossarrow-right