Support

Account

Home Forums Backend Issues (wp-admin) Conditional logic from other field group

Solving

Conditional logic from other field group

  • Hi!
    I have two field groups on my site, one which sets up general features and settings for the whole site, and one my clients use to add more specific data.

    I’m trying to set up an option on the first field group that would affect the appearance of the second one. Basically, a logic that would say “if that checkbox from the options field group is checked, then make this field available on the other field group”.

    However, when I try to set up a conditional logic for my field, the only available fields are those from that same field group.

    Is there a way to make the external field groups available as well? Or maybe another way to do it I haven’t thought of?

    Thank you for your help

  • Are both of these field groups available to be edited on the same admin page?

    From your description it sounds like one group is on a post and the other is on an options page.

  • Hi John, thank you for your response.

    Indeed, they’re on two different option pages.

  • Alright, I came up with something if anybody stumbles upon this topic and has the same problem. Basically, I’m hiding the fields I want to disable using CSS when they’re disabled in the main options page.

    Here goes in functions.php:

    // ACF admin CSS
    function acf_css() { ?>
        <style type="text/css">
    
        /* checks if option is checked on main option page*/
        <?php if (!get_field('papier', 'options')) { ?>
            
            /* hides option in admin */
            th[data-name="cache_papier"], td[data-name="cache_papier"] {
                display: none;
            }
    
        <?php } ?>
    
        </style>
        <?php
    }
    
    // adds the CSS above to ACF's admin
    add_action('acf/input/admin_head', 'acf_css');

    Of course you need to replace get_field(‘papier’, ‘options’) for the field that disables the other, as well as data-name=”cache_papier” for the field to hide, with your own names if you do it this way. I hope that’s clear 🙂

    It’s not perfect since it’s not actually disabled, it’s just hidden, but it’s better than nothing.

  • What you need to do is to use an acf/prepare_field filter for each of the fields that you want to be conditional. In the filter check the option from the other options page that the field is conditional on and based on this return the field to allow it to be shown or return false to hide it.

  • Nice, I didn’t know this existed! It works, although I get a PHP notice when it doesn’t render the field:

    Notice: Trying to access array offset on value of type bool in /home/website/www/wp-content/plugins/user-role-field-setting-for-acf/acf-user-role-field-setting.php on line 47

    Anything I could do about it?

  • I don’t know what is causing the error. Is this on all fields or just one, have you made more than one conditional like this? The error should not be happening.

  • It’s on all fields, but I have multiple fields I need to hide that have the same name (they’re in repeaters). Maybe this could be related?

Viewing 8 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.