Support

Account

Home Forums Backend Issues (wp-admin) ACF Custom Locations don't work Reply To: ACF Custom Locations don't work

  • The “§ruleS” was just a spelling mistake in the topic, I did use the original array name in the code.
    I did write an own code, but I now used the snippet from the documentation. The exact code is:

    add_filter('acf/location/rule_types', 'acf_location_rules_types');
    function acf_location_rules_types( $choices )
    {
        $choices['Basic']['user'] = 'User';
     
        return $choices;
    }
    
    add_filter('acf/location/rule_operators', 'acf_location_rules_operators');
    function acf_location_rules_operators( $choices )
    {
        $choices['<'] = 'is less than';
        $choices['>'] = 'is greater than';
     
        return $choices;
    }
    
    add_filter('acf/location/rule_values/user', 'acf_location_rules_values_user');
    function acf_location_rules_values_user( $choices )
    {
        $users = get_users();
     
        if( $users )
        {
            foreach( $users as $user )
            {
                $choices[ $user->data->ID ] = $user->data->display_name;
            }
        }
     
        return $choices;
    }
    
    add_filter('acf/location/rule_match/user', 'acf_location_rules_match_user', 10, 3);
    function acf_location_rules_match_user( $match, $rule, $options )
    {
        $current_user = wp_get_current_user();
        $selected_user = (int) $rule['value'];
     
        if($rule['operator'] == "==")
        {
        	$match = ( $current_user->ID == $selected_user );
        }
        elseif($rule['operator'] == "!=")
        {
        	$match = ( $current_user->ID != $selected_user );
        }
     
        return $match;
    }
    

    The code is placed at the very bottom of my theme’s functions.php to ensure that it isn’t executed too early.