Last updated on September 13, 2023

ACF Custom Location Rules

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Add custom location rules in ACF.

To add custom location rules in Advanced Custom Fields (ACF), you can use the acf/location/rule_types filter to register your own custom rule types. This allows you to define specific conditions for when a field group should be displayed.

Here’s an example code snippet that demonstrates how to add a custom location rule based on the current user’s role:

 function wpsnippets_add_custom_location_rule_types($rule_types) {
     $rule_types['User Role'] = 'user_role';
     return $rule_types;
 }

 add_filter('acf/location/rule_types', 'wpsnippets_add_custom_location_rule_types');

 function wpsnippets_add_custom_location_rules($choices, $rule_type) {
     if ($rule_type === 'user_role') {
         $choices['current_user_role'] = 'Current User Role';
     }
     return $choices;
 }

 add_filter('acf/location/rule_values/user_role', 'wpsnippets_add_custom_location_rules', 10, 2);

 function wpsnippets_match_custom_location_rule($match, $rule, $options) {
     if ($rule['param'] === 'current_user_role') {
         $user = wp_get_current_user();
         $match = in_array($user->roles[0], $rule['value']);
     }
     return $match;
 }

 add_filter('acf/location/rule_match/user_role', 'wpsnippets_match_custom_location_rule', 10, 3);


In this example, we’re adding a custom location rule type called “User Role”. We register this rule type using the acf/location/rule_types filter. Then, we define the available rule values for this rule type using the acf/location/rule_values/{rule_type} filter. In our case, we add a rule value called “Current User Role”.

Finally, we implement the logic for our custom location rule using the acf/location/rule_match/{rule_type} filter. In this example, we check if the current user’s role matches the selected role(s) in the field group settings.

You can modify this code snippet to add your own custom location rules based on different conditions, such as post type, taxonomy, or any other criteria that suits your needs.

Examples

Example 1: Adding a custom location rule based on post type

This example demonstrates how to add a custom location rule in Advanced Custom Fields (ACF) based on the post type. The code snippet adds a new location rule called “Post Type” and checks if the current post type matches the specified value.


function wpsnippets_acf_location_rule_post_type( $rule ) {
    $rule['post_type'] = 'page'; // Replace 'page' with your desired post type
    return $rule;
}
add_filter( 'acf/location/rule_types', 'wpsnippets_acf_location_rule_post_type' );

Explanation: The code snippet adds a new filter hook acf/location/rule_types to modify the available location rules in ACF. The wpsnippets_acf_location_rule_post_type function is hooked to this filter and adds a new location rule called “Post Type”. The function sets the post_type key in the $rule array to the desired post type value (e.g., ‘page’). This allows you to show or hide ACF fields based on the selected post type.

Example 2: Adding a custom location rule based on taxonomy

This example demonstrates how to add a custom location rule in ACF based on the taxonomy. The code snippet adds a new location rule called “Taxonomy” and checks if the current post has a specific taxonomy term assigned.


function wpsnippets_acf_location_rule_taxonomy( $rule ) {
    $rule['taxonomy'] = 'category'; // Replace 'category' with your desired taxonomy
    $rule['operator'] = '==';
    $rule['value'] = 'news'; // Replace 'news' with your desired term slug
    return $rule;
}
add_filter( 'acf/location/rule_types', 'wpsnippets_acf_location_rule_taxonomy' );

Explanation: Similar to the previous example, this code snippet adds a new location rule called “Taxonomy” by hooking into the acf/location/rule_types filter. The wpsnippets_acf_location_rule_taxonomy function sets the taxonomy, operator, and value keys in the $rule array to define the desired taxonomy and term. This allows you to conditionally display ACF fields based on the selected taxonomy term.

Example 3: Adding a custom location rule based on user role

This example demonstrates how to add a custom location rule in ACF based on the user role. The code snippet adds a new location rule called “User Role” and checks if the current user has a specific role.


function wpsnippets_acf_location_rule_user_role( $rule ) {
    $rule['param'] = 'user_role';
    $rule['operator'] = '==';
    $rule['value'] = 'editor'; // Replace 'editor' with your desired user role
    return $rule;
}
add_filter( 'acf/location/rule_types', 'wpsnippets_acf_location_rule_user_role' );

Explanation: This code snippet adds a new location rule called “User Role” by hooking into the acf/location/rule_types filter. The wpsnippets_acf_location_rule_user_role function sets the param, operator, and value keys in the $rule array to define the desired user role. This allows you to conditionally display ACF fields based on the user role of the current user.

Last updated on September 13, 2023. Originally posted on August 24, 2023.

Leave a Reply