Using Multiple Instances of the Same Hook for Different User Groups

By: Vince Dimanno | Asked: 11/11/2023
ForumsCategory: Paid Help WantedUsing Multiple Instances of the Same Hook for Different User Groups
Vince DimannoVince Dimanno asked 5 months ago

I am attempting to use this example:
https://formidableforms.com/knowledgebase/formidable_shortcode_atts/

add_action('formidable_shortcode_atts', 'frm_admin_readonly', 20, 2);
function frm_admin_readonly( $atts, $all_atts ) {
   if ( $atts['readonly'] == 'administrator' ) {
      global $frm_vars;
      if ( current_user_can('administrator') ) {
         $frm_vars['readonly'] = 'disabled';
      } else {
         $frm_vars['readonly'] = false;
      }
   }
}

but I want it to apply to more than just administrators. So, I created code like this (following the documentation):
https://formidableforms.com/knowledgebase/formidable-hooks/#kb-duplicating-a-function

add_action('formidable_shortcode_atts', 'frm_admin_readonly1', 20, 2);
function frm_admin_readonly1( $atts, $all_atts ) {
if ( $atts['readonly'] == 'administrator' ) {
global $frm_vars;
if ( current_user_can('administrator') ) {
$frm_vars['readonly'] = 'disabled';
} else {
$frm_vars['readonly'] = false;
}
}
}

add_action('formidable_shortcode_atts', 'frm_admin_readonly2', 20, 2);
function frm_admin_readonly2( $atts, $all_atts ) {
if ( $atts['readonly'] == 'second-group' ) {
global $frm_vars;
if ( current_user_can('second-group') ) {
$frm_vars['readonly'] = 'disabled';
} else {
$frm_vars['readonly'] = false;
}
}
}

I have used this pattern on Hooks before and it worked just fine. I also tried this:

add_action('formidable_shortcode_atts', 'frm_admin_readonly1', 20, 2);
function frm_admin_readonly1( $atts, $all_atts ) {
if ( $atts['readonly1'] == 'administrator' ) {
global $frm_vars;
if ( current_user_can('administrator') ) {
$frm_vars['readonly1'] = 'disabled';
} else {
$frm_vars['readonly1'] = false;
}
}
}

add_action('formidable_shortcode_atts', 'frm_admin_readonly2', 20, 2);
function frm_admin_readonly2( $atts, $all_atts ) {
if ( $atts['readonly2'] == 'second-group' ) {
global $frm_vars;
if ( current_user_can('second-group') ) {
$frm_vars['readonly2'] = 'disabled';
} else {
$frm_vars['readonly2'] = false;
}
}
}

and some other combinations. Then updated my shortcode to read:

[formidable id="3" readonly='administrator' readonly='second-group']

but this is not working. I also tried combinations for the shortcode to match my changes in the filter above like this:

[formidable id="3" readonly1='administrator' readonly2='second-group']
[formidable id="3" readonly='administrator,second-group']

When I use the example and restrict it to only the 'administrator' user, it does work, but every combination of code I have tried will not allow me to apply this to another user type. I have tried a lot of combinations. Can this be done? Does it only work for a single user role? Admins only? I feel it is something simple that I am missing, but Formidable Support defines it as "custom code". I'm happy to pay for someone to code it, despite thinking that I simply have the wrong combination of instance numbers in place. 

Thanks!

4 Answers
Best Answer
Rob LeVineRob LeVine answered 5 months ago
I'm happy to do this for you as a paid job, however, the solution is easy and fundamental to programming. You definitely don't need, nor should you use, multiple functions in this case. You just need basic conditional coding logic. See here. If you have more than 2 groups you can make the code more concise by using in_array() instead of all the separate ifs.
Vince DimannoVince Dimanno replied 5 months ago

Hey Rob!

Thanks for your thoughts. Generally speaking, I try to follow the Formidable guides for items like this. In the past, I have used multiple instances with good success, but I have not seen any documentation on using, or editing a function by using an array. Formidable is great, but one of their weak spots is their documentation. It is very complete, but sometimes not well organized.

I will give your code sample a try and if I have no luck, I'd be happy to get you to do it. I'll follow up with my results.

Much appreciated!!!

Vince

Rob LeVineRob LeVine replied 5 months ago

OK sounds good. I'm 99% sure the page about the duplicating of a function is meant for cases where someone has multiple features or forms that they want to keep things separate. In your case, it's all one happy family and it makes organizational sense to do it in one function. To be clear, it'll work if you do it in multiple functions, it's just that I will have to come after you with my cooking skillet and beat you with it.

Vince DimannoVince Dimanno replied 5 months ago

Ha! Not the skillet!!! I will be on my best behavior 🙂

Vince DimannoVince Dimanno answered 5 months ago
Promised to follow up and I was able to get your code working in conjunction with some conditional statements. I couldn't quite get it working as a standalone due to the final application of the code, but, in testing, it led me to the conditional statements that put it all together. I appreciate your help :) 
Vince DimannoVince Dimanno answered 5 months ago
Hi Rob, I was actually wrong. I was able to get the code to work and it all looked great. However, if I add a row to the repeater, the new row shows all fields as "read-only" again.  Effectively, I have the fields marked as "read-only" and then change their status with the code you provided above, added to the Theme's functions.php file, and some conditional statements. The frustrating part of this is that I don't identify any field IDs in the hook so it should apply to all fields in the form, but this is not working. New repeater rows default back to read-only for all users. I am sure that I am not doing this very elegantly. However, I think there is a requirement for a custom hook here. Something like this, but for multiple user roles and for front end editing. https://formidableforms.com/knowledgebase/frm_setup_new_fields_vars/#sts=Make%20field%20read-only%20for%20non-administrators  At any rate, is this something you would take a look at? If so, I will provide more details.  Thanks, Vince
Rob LeVineRob LeVine replied 5 months ago

I think we're to the point where you're going to need to hire me if you want to move forward. I'm pretty cheap as things go (hourly) and I don't charge if I don't fix it. You can send me a message via the forums if you want to discuss it. https://connect.formidableforms.com/messages

Victor Font answered 5 months ago

@Vince Dimanno: You're experiencing a common issue with AJAX and the HTML DOM. jQuery works only on fields that exist in the DOM when the page is loaded. AJAX adds fields on the fly, so jQuery needs to be reinitialized for every new repeater row. This is how you fix it: https://formidable-masterminds.com/repeaters-and-complex-jquery-operations/

Vince DimannoVince Dimanno replied 5 months ago

Thanks Victor!

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