Support

Account

Forum Replies Created

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

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

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