Built exclusively for the Advanced Custom Fields (ACF) plugin, this control allows you to create dynamic block content based on ACF fields. There are thousands of applications for the Advanced Custom Fields control. You are only limited by your own creativity and how you have implemented ACF.
Check out the video below for a brief overview of you can use Block Visibility with the Advanced Custom Fields plugin. Note that this video was recorded in version 1.8. Additional rule set functionality was added to the control in version 1.9, but the overall functionality is the same.
How to Enable
Begin by installing and activating the Advanced Custom Fields plugin. The control will not be available if the plugin is inactive.
Click on any block in the Block Editor and navigate to the Visibility panel. You will notice a vertical ellipsis icon in the Controls Toolbar. See the screenshot below. Click on the ellipsis, and all available visibility controls will appear. Select “Advanced Custom Fields” to add the Advanced Custom Fields control panel to the selected block.
How to Configure
Instructions are always better with examples. Let’s assume that you have an ACF field group entitled “Event”. Each event can have a number of fields, including “Start Date” and “Featured”. You want to configure a block to be visible if the start date occurs in 2021 and the current event is a “Featured” event.
The Advanced Custom Fields control uses rules and rule sets to determine if the block should be visible. A thorough overview of how this works is provided below.
Steps to Complete
- Navigate to the Advanced Custom Fields control settings in the Visibility panel.
- Click on the rules selector and then choose “Featured” from the list of field options.
- Choose “Has any value” from the condition selector.
Note that the “Featured” field is a boolean. When it’s set to false, the field value is evaluated as empty and therefore has no value. Pay close attention to the field types when setting up your rules.
- Next, click on the “Add rule” button to add a second rule.
- Click on the rules selector and then choose “Start Date” from the list of field options.
- Choose “Value contains” from the condition selector.
- Enter 2021 in the value field.
The resulting configuration should resemble the screenshot below.
The Advanced Custom Fields control does have one known issue where blocks do not display as you would expect in the WordPress “preview”. At the top of the editor, there is a preview button that will open the page/post in a new window. It is not (currently) possible to accurately fetch ACF fields on a “preview” page. Therefore the ACF visibility conditions may appear to not be working. But don’t worry. If you view the actual page on the front end of your site, all block conditions will function as you expect.
How to Disable
If you would like to disable the Advanced Custom Fields control at the block level, simply click on the ellipsis icon in the “Controls” toolbar and deselect it. The control can also be disabled globally in the plugin’s Visibility Control Settings.
Note that if this control is disabled globally or the Advanced Custom Fields plugin is deactivated, any blocks that are actively being hidden by the Advanced Custom Fields control will become visible again.
Rules and Rule Sets
Similar to other controls, rules and rule sets power the Advanced Custom Fields control. A “rule” is simply a conditional. A “rule set” is a collection of rules.
All rules within a rule set must be satisfied for the selected block to be visible. However, you can create multiple rule sets. Only one rule set needs to apply for the block to be visible.
Within a rule set, you are creating AND conditions between multiple rules. Between two rule sets, you are creating OR conditions.
There are instances when rule errors can occur. This can happen when rule inputs are not completely filled out or if the rule is based on a product that has since been deleted from your store. In these situations, the plugin defaults to showing the block unless other rules apply, which would hide the block.
Rules in the Advanced Custom Fields control are unique from other controls in Block Visibility in that they are themselves dynamic. The available ACF rules depend on the fields and field groups that you have set up in the Advanced Custom Fields plugin.
When a specific field is selected, you will then have the choice of 6 operators. These include:
- Has any value
- Has no value
- Value is equal to
- Value is not equal to
- Value contains
- Value does not contain
Once the operator is selected, you then enter the value to test the field against.
Rule Operator Limitations
As of Block Visibility version 1.9, the operators are not dependent on the field type selected. For example, if you chose a field that was a numerical value, you could imagine an additional operator like “Is greater than” or “Is less than” appearing in the dropdown. Depending on user demand, such functionality may be included in future versions. Let us know if this is something you are interested in.
Hide When Rules Apply
By default, the block will be visible if the applied rules are satisfied. Simply toggle the “Hide when rules apply” setting to hide the block instead.