Home Forums Add-ons Options Page Extend conditional logic to be set from options page


Extend conditional logic to be set from options page

  • Hi,

    I’m looking for a way to set a global condition from an options page, which fields on posts can be shown/hidden via conditional logic.

    I want to set up something like this. On the options page it would have a true/false input that asks if you want to show social media options on posts.

    On the post then would show the social media fields IF the option was set to true on the ACF options page.

    I have searched around but I can’t seem to find anything on the topic. Can anyone help?

  • What you are looking to do is to dynamically set the location rules for field groups based on the values of an options page. This can be done. If you are not using JSON files you can use the acf/load_field_group hook

    add_filter('acf/load_field_group', 'your_function_name', 20);
    function your_function_name($group) {
      if ($group['key'] == 'group_YOUR_GROUP_KRY')) {

    if you are using json files, of just to be sure, you should use the acf/validate_field_group hook. The above hook is not called when loading json files.

    add_filter('acf/validate_field_group', 'your_function_name', 20);
    function your_function_name($group) {
      if ($group['key'] == 'group_YOUR_GROUP_KRY')) {

    In your filter you can check your options and set the location rules dynamically based on your checks. For examples of how the location rules work I would set some location rules in ACF and do an export to see how this value of the group needs to be added.

  • @hube2 Thanks. I will give it a go

  • The proper way to do this is to use the ‘acf/prepare_field’ filter as the ‘acf/validate_field_group’ filter does not exist.

    For example:

    /* Conditional Logic to display field if an options page field is set */
    function my_conditional_acf_field($field) {
        if(get_field('option_page_field_to_check', 'option')){
            return $field;
    // Make sure to use correct field key instead of 'XXXXXXX'
    add_filter('acf/prepare_field/key=field_XXXXXXXX', 'my_conditional_acf_field', 20);
  • … ‘acf/validate_field_group’ filter does not exist

    This hook exists, but it is not documented, as is the acf/load_field_group hook.

    The OP was asking how to hide entire field groups, or that is the impression I got, based on an option setting. acf/prepare_field is only useful for single fields and not entire groups. Using the hooks I mentioned you can alter the location setting for a field group to dynamically set on what post types it should appear.

  • When I tried using the ‘acf/validate_field_group’ for my own project I received lots of PHP errors about it not existing. Not sure if I’m using the wrong version of ACF Pro or what, but I couldn’t get it to work whatsoever. Perhaps it was removed in a newer version? I didn’t spend a lot of time trying to debug it, I just moved on to the alternative that worked for me.

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

The topic ‘Extend conditional logic to be set from options page’ is closed to new replies.