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 2 years 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 2 years 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 2 years 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 2 years 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 2 years 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 2 years ago

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

David BN replied 2 years 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>

2 Answers
Victor Font answered 2 years 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 2 years 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 2 years 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 2 years 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 2 years 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?

Joe Mateus answered 5 months ago

I decided to come back to this issue because after 1 year hiatus because I needed this to work for me on another website. I FINALLY got it to work based on the user roles. I took code from here and there and added my own code, it may not be pretty but it works and would be great if others add to it to make it even better. I just about know enough where I am dangerous. To refresh the issue I had, I have 2 user roles on the website, one for the public and one for businesses when they register. I have a form that allows the public to submit a form only 1 time and for businesses they can submit the same form for up to 25 times (You can change this number as well as adding more functions if you have more user roles). This is the code I came up with, All you need to do is change the count to each user roll that you want to set it to.
add_action('frm_display_form_action','check_entry_count','my_get_current_user_roles', 10,3 );
function check_entry_count($params, $fields, $form){
global $user_ID;
remove_filter('frm_continue_to_new', '__return_false', 50);
if($form->id == 49 and !is_admin()){ //replace 49 with the ID of your form$user = new WP_User( $user_ID );
if ( !empty( $user->roles ) && is_array( $user->roles ) ) {
foreach ( $user->roles as $role );}
if ($role == public) {
echo 'You are registered as a private seller. You can list 1 item.';
}
elseif ($role == business) {
echo 'You are registered as a business, you can list up to 25 items.';
}
$count = FrmEntry::getRecordCount("form_id=". $form->id ." AND user_id=".$user_ID);if($count >= 1 && $role == 'public'){ //change 1 to your entry limit
echo 'This form is closed - you have reached your 1 item limit';
add_filter('frm_continue_to_new', '__return_false', 50);
}if ($count >= 25 && $role == business) { //change 25 to your entry limit
echo 'This form is closed. - You have reached your 25 item limit.';add_filter('frm_continue_to_new', '__return_false', 50);
}
}
}

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