I'm looking for a tidy piece of PHP to drop into my functions.php
file that will assign the user a role based on the information collected from their Facebook Account on Sign-Up.
Fetching from Facebook is sorted and is working. What I want to do is assign a user a role based on their Age_range
that comes back from Facebook.
Specifically, when a user's Age_range
comes back as 13, I want to assign them the role of "under18", which I have already custom made and dropped into Wordpress.
N.B. Facebook's Min Age Array will be either 13, 18 or 21.
The default role is "Customer" for everyone else that doesn't meet this rule and is already assigned from Wordpress.
What I have at the moment:
// Age Rule if under 18 tag them under 18
add_action( 'user_register', 'w85t_set_role_by_age' );
function w85t_set_role_by_age( $user_id ){
$user = get_user_by( 'id', $user_id );
$age = substr(
strrchr(
$user->data->yim, //Using Yahoo Instant Messenger field for now
"@"
), 1
); //Get Domain
$under_18 = array( '13' );
if( in_array( $age, $under_18 ) ){
foreach( $user->roles as $role )
$user->remove_role( $role ); //Remove existing Roles
$user->add_role( 'under18' ); //Add role
}
}