0

I'm looking for a way to set all users that register with specific domain extensions to be given a non default role. For example if a user signs up with a '.co.uk' address then their role could be Author rather than the default Subscriber. Does anyone know of a plugin or code that can achieve this?

Any help would be greatly appreciated.

0

Here is a snippet that assign author role to a new registered member whose email is somtehing@anotherthing.co.uk where you need to modify to fit your needs:

add_action( 'user_register', 'user_register_cb', 10, 1 );
function user_register_cb( $user_id ){
    $user_info = get_userdata( $user_id );
    $email = $user_info->user_email;
    $domain = '.co.uk';
    $pos = strpos( $email, $domain );
    if( $pos === 0 ){
        // You can assign any other role
    }else{
        $user_info->set_role( 'author' );
    }
}

You can add those codes in your functions.php in the theme, if you think your theme won’t be changed. Otherwise mu-plugins is the best solution. To use mu-plugins, go to /wp-content/ and find the folder with name 'mu-plugins'. If there is no folder in that name, then create a folder, name it 'mu-plugins', create a file inside that, give any name you like and paste the code in there. You don't need to activate that plugin. Mu-plugins means must use plugins, so it will be activated automatically always. If you use mu-plugins then add a php start tag at the beginning of the code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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