Support » Plugins » Assign User Role and or User Group at Registration based on user name

  • We want to create a private LMS for a small school. We are using the Members and Role Scoper plugin to restrict access and show different content for different groups of students. Members plugin is used so we can filter widgets using widget logic based on role and Role Scoper for hiding menu items, content and categories when need be.
    We want to get the students to register themselves which is possible. What we need is them to enter their user id number and get WordPress to connect that number to a specific user role and or add it to a specific Role Scoper user group. Is there already code out there that does this or at least helps us?

Viewing 6 replies - 1 through 6 (of 6 total)
  • I’ve not used Role Scoper or Members, but would assume both work of the standard WP roles system (and if not, they should)

    I also assume you’ve fine getting the user role based on their student ID (either a simple database lookup or using an API)

    Here is the code to set the user role which should set you off in the right direction

    // Create the user as normal
    $user_id = wp_create_user($username, $password, $email);
    // Get the user role by ID number from somewhere
    $user_role = get_user_role_by_student_id($student_id);
    // Get the user object and add the new user role
    $user = new WP_User($user_id);

    Will go through your code and see how I can possibly integrate this with the WordPress standard registration system. Might be a bit out of my league though, but we shall see!

    Hello Rhand,

    I have a similar situation, this one for a large physician group who would be automatically assigned a user role (Author) based on the registration number they receive when joining the organization.

    Did you have any luck with Damian’s code? If so, can you share your solution?

    Thank you!

    Hello Damian,

    Your suggested code looks clean and good. Where (in what file) would I use the code?


    @ VirtbizGuy Nope. Been adding users to user groups manually using backend registration and Role Scoper assignment to a specific user group. If the current project is expanded I will look into this again. Would love the functionality, but have not had time/$ to add it to this project.

    Thanks Rhand. I will keep working on this and if I get a solution, I will post it here for you and others. I sure like the direction that you and Damian are going here, and it is feasible.

    My other issue is that I will need to get the registration number that assigns the user permission from a database external to this site, but I am assuming that if that site give me access permission, I can do that fairly easily. You may have the same issue. The workaround to that, of course, is to add that registration # (in your case, student ID) information into my on-site database, but that requires frequent manual updating and would not be current at all times.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Assign User Role and or User Group at Registration based on user name’ is closed to new replies.