0

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
    }
}

1 Answer 1

0

This assumes that what you said about only needing to check for the minimum value is true. These conditional statements work for this array.

Facebook's Min Age Array will be either 13, 18 or 21.

$age_range = array( 13, 18, 21); // I don't know what actual data you have here.

if( min( $age_range ) == 13 ) {
    $age_range = 'under18';
}
elseif( min( $age_range ) >= 18 ) {
    $age_range = 'over18';
}
else {
    // Unset $age_role if no required values found in $age_range.
    unset( $age_role );
}


if( isset( $age_role ) ) {
    // Here you can assign the role $age_role to the user.
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.