Support » Plugin: Advanced Custom Fields: reCAPTCHA Field » Disabled for logged in users

  • Resolved codevelop

    (@codevelop)


    It would be amazing if we could disable the captcha for users who are logged in – seeing as they’ve already been validated through their login credentials.

    A step further would be the ability to specify which role(s) the members would need to possess to bypass it.

Viewing 9 replies - 1 through 9 (of 9 total)
  • Plugin Author Irvin Lim

    (@irvinlim)

    Hi,

    That’s a great idea for a use case, but I think that such functionality should be left up to the ACF framework itself.

    However, you can make use of ACF’s Location Rules under the field group to only display a particular field group if a user is not logged in. Otherwise, if you are adding the field programmatically, it should be simple enough to do so using some custom PHP code.

    Do give this a try and let me know if it works for you. Thank you!

    codevelop

    (@codevelop)

    Thanks for the quick response.

    The problem with my current use-case is that the only field in the entire group that I’d want to disable for logged-in users is the captcha field. I don’t want to create one set of fields for logged-in users, and other for logged out, where the only difference is that one captcha field.

    I dabbled in the code and have managed to add this setting within the admin, and have the field not appear if set to “true” and the user is logged in.

    The only part that’s not quite right is that I’m still left with the label, which I end up needing to hide by adding a class to the field.

    If you’re interested, I can share my work through github?

    Plugin Author Irvin Lim

    (@irvinlim)

    Hi,

    I see what you mean. I was actually thinking of creating a field group with a single reCAPTCHA field, with the location rule for being not logged in, and putting the rest of your fields in a separate field group. You can actually put multiple field groups in one form, and they will all be validated together.

    However, I just tried it and found out that when you use acf_form() with field_groups, it doesn’t obey location rules set on the field group. You can still use the UI (or do it programmatically with acf_add_local_field_group) to add field groups and insert the field groups into the correct page using location rules. Just make sure you set a different Order No. for the two field groups.

    I’m not sure if this will still work for your use case.

    And yes, please open a PR on GitHub if you wish! I can take a look and consider incorporating this into acf-recaptcha. However, I am still strongly in favour of leaving this up to ACF location rules since it’s already included – if we do include this within acf-recaptcha, it’s kind of a slippery slope for feature bloat in the future.

    codevelop

    (@codevelop)

    I’d tried the same thing as well (great minds think alike!) but it sounds like we came to the same discovery about the Clone field.

    I totally see where you’re coming from about the slippery slope. I think a more versatile solution would actually be if, there were more Conditions for fields in the ACF core, including User Role.

    Since my form is on the front, I’d want to use the “exclude” argument for acf_form – but it’s not the cleanest solution, plus I’m actually using Advanced Forms plugin for this set of fields, as it’s a contact form :/ The circumstances are making this tricky!

    I realized my code modifications are not quite as complete as I thought… the JS is throwing an error since the div with the corresponding ID is missing. I’ll see if I can advance this some more. On that note – it would actually be great if the JS / CSS dependencies only get loaded after verifying whether there’s a form on the page that has a Captcha field. I noticed, at least the JS, is loaded on every page when the plugin is installed. Although, I guess once the script gets cached that no longer matters.

    Plugin Author Irvin Lim

    (@irvinlim)

    I’m sorry, I didn’t get what you meant by the Clone field… Does it work if you have separate field groups?

    I’m not familiar with Advanced Forms either, sorry. Is the exclude argument part of the acf_form() API? Perhaps if you showed me some of your code used to add your form to your template I could get a better understanding.

    One more way I can think of is to make use of ACF hooks to remove any reCAPTCHA fields when the user is logged in, as such:

    function remove_acf_recaptcha_if_logged_in($field) {
        if (is_user_logged_in()) {
            return null;
        }
    
        return $field;
    }
    
    add_action( 'acf/load_field/type=recaptcha', 'remove_acf_recaptcha_if_logged_in', 10, 1 );

    And you will have to disable ACF reCAPTCHA Protection as well in your form:

    acf_form(array(
        ...
        'recaptcha' => !is_user_logged_in(),
    ))
    • This reply was modified 3 years ago by Irvin Lim.
    • This reply was modified 3 years ago by Irvin Lim.
    codevelop

    (@codevelop)

    Having separate field groups works, but isn’t really a viable option

    What I meant about the clone field, is that I explored the following idea before opening a ticket:

    – Create a group of all fields minus the reCAPTCHA
    – Create another group with 2 fields. One field a clone of the above field group (so as to include all of those fields, without needing to manage 2 sets of the same fields in parallel) and the other field being the reCAPTCHA.

    In the end, I wrote a quick plugin that adds some field settings to any field:
    – show only for: (ignore / all logged in users / all logged out users / [select role(s)]
    – hide for: (ignore / all logged in users / all logged out users / [select role(s)]

    Plugin Author Irvin Lim

    (@irvinlim)

    Actually, what I meant was to have these two field groups, added to your page using location rules:

    acf_add_local_field_group(array (
      'key' => 'group_recaptcha',
      'title' => 'Field group for non-logged in',
      'fields' => array (
        array (
          'key' => 'field_recaptcha',
          'label' => 'reCAPTCHA',
          'name' => 'recaptcha',
          'type' => 'recaptcha',
          'site_key' => '',
          'secret_key' => '',
        ),
      ),
      'menu_order' => 0,
      'position' => 'normal',
      'style' => 'default',
      'label_placement' => 'top',
      'instruction_placement' => 'label',
      'hide_on_screen' => '',
      'active' => 1,
      'description' => '',
      'recaptcha' => true,
      'location' => array (
        array (
          array (
            'param' => 'current_user',
            'operator' => '!=',
            'value' => 'logged_in',
          ),
          array (
            'param' => 'page_template',
            'operator' => '==',
            'value' => 'my-page-template.php',
          )
        )
      )
    ));
    
    acf_add_local_field_group(array (
      'key' => 'group_other_fields',
      'title' => 'Field group for all users',
      'fields' => array (
        array (
          'key' => 'field_text',
          'label' => 'Text Field',
          'name' => 'text_field',
          'type' => 'text',
        ),
      ),
      'menu_order' => 1,
      'position' => 'normal',
      'style' => 'default',
      'label_placement' => 'top',
      'instruction_placement' => 'label',
      'hide_on_screen' => '',
      'active' => 1,
      'description' => '',
      'location' => array (
        array (
          array (
            'param' => 'page_template',
            'operator' => '==',
            'value' => 'my-page-template.php',
          )
        )
      )
    ));

    This works, and you can do this in the UI as well. The only caveat is that you don’t explicitly pass the field group ID when calling acf_form(), but rather, you use location rules to specify where they should appear instead.

    In the end, I wrote a quick plugin that adds some field settings to any field

    This is also a good solution – I agree this could make up for the missing functionality in ACF itself. In fact I think this is a better solution than trying to hack around both the ACF core and ACF reCAPTCHA plugins.

    • This reply was modified 3 years ago by Irvin Lim.
    Plugin Author Irvin Lim

    (@irvinlim)

    Sorry, I realised that the method using Location Rules above should not be used, as bots can bypass the form. This is noted in my README: https://github.com/irvinlim/acf-recaptcha#field-group-setting

    You should go ahead with your custom plugin method instead. If you do publish the plugin on the WordPress Plugins repository, do reply with a link to your plugin, in case it may help other users in the future.

    I’m closing this topic as resolved, since you have found a solution.

    • This reply was modified 3 years ago by Irvin Lim.
    codevelop

    (@codevelop)

    Thanks! If ever I get around to adding my plugin to WordPress.org, I’ll be sure to update the thread.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Disabled for logged in users’ is closed to new replies.