Support

Account

Home Forums Backend Issues (wp-admin) Supporting Multiple User Roles in Location Rules

Unread

Supporting Multiple User Roles in Location Rules

  • Wordpress’ user system, by design, allows a user to be assigned multiple roles. The most common usage of this is in bbPress, which will assign users an additional role on top of their WordPress role. I personally use a third set of roles, on top of WordPress and bbPress. It appears that ACF does not support this.

    The User Role location rule will only work if the selected role is the primary role assigned to a user (aka the first one in the “roles” array). This means that if I try to use this location rule for the “Keymaster” role (a bbPress role), ACF won’t recognize it.

    However, thank goodness for the extensibility of ACF! I think support for multiple roles should be added into the plugin, but for the time being, the snippet of code below (added to my theme’s functions.php) is getting me by. Turns out, using the current_user_can function will check for roles as well as caps. In fact, as far as I can tell, a User Capability location rule could easily be merged with the User Role rule.

    add_filter( 'acf/location/rule_match/user_role', 'acf_location_rules_match_user_role', 10, 3 );
    function acf_location_rules_match_user_role( $match, $rule, $options ) {
    	if( $rule['operator'] == '==' ) {
    		$match = current_user_can( $rule['value'] );
    	} elseif ( $rule['operator'] == '!=' ) {
    		$match = ! current_user_can( $rule['value'] );
    	}
    	return $match;
    }
Viewing 1 post (of 1 total)

The topic ‘Supporting Multiple User Roles in Location Rules’ is closed to new replies.