Support

Account

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

Solving

ACF Custom Locations don't work

  • Hi!
    I’m trying to customize ACF for my purposes by adding custom location rules, but I found out, that with the latest version of ACF, you can’t create Custom Location Rules as described in the documentation. The problem is, that the $rules array is being detected as undefined, even if you use exactly the code, that is provided in the documentation. Does anyone know any solution or approach to this issue?
    Thanks in advance,
    rjay

  • Hi @rjay

    Thanks for the feedback.

    I havent’ experienced this issue before, are you able to post your full code for me to trial?

    Also, perhaps it was a spelling mistake while writing the topic, but there is no $rules array, it is $rule.

    Thanks
    E

  • 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.

  • Hi @rjay

    Can you please specify what is and isn’t working about the rules?

    Thanks
    E

  • Hi!
    I chose “admin” as the user to which the fields are displayed, but using the administrator account, the fields I setup were never displayed. Therefore I did a little test, trying to print out the §rule array using print_r. Instead of the array’s content, I always get this:

    Notice: Undefined variable: rule in [MyPath]\htdocs\wordpress\wp-admin\test.php on line 12

    Just tell me, if you need any more infos.

  • Hi @rjay

    Could you post your code that contains the print_r. I don’t understand how the $rule parameter can be undefined…

    Thanks
    E

  • <html>
    	<body>
    		<h1>The selected value</h1>
    		<p><?php
    			print_r($rule);
    			?>
    		</p>
    	</body>
    </html>

    That’s all. It’s placed in the space where normally my edit.php would be. But the problem obviously is not this piece code, it must be somewhere else.

  • Hi @rjay

    Placing this code in your edit.php (not sure where this is used) wont work. You need to debug WITHIN the location rule function.

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

The topic ‘ACF Custom Locations don't work’ is closed to new replies.