WordPress.org

Ready to get started?Download WordPress

Forums

Assign User Role and or User Group at Registration based on user name (7 posts)

  1. Rhand
    Member
    Posted 2 years ago #

    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?

  2. Damian Gostomski
    Member
    Posted 2 years ago #

    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);
    $user->add_role($user_role);
  3. Rhand
    Member
    Posted 2 years ago #

    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!

  4. VirtbizGuy
    Member
    Posted 2 years ago #

    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!

  5. VirtbizGuy
    Member
    Posted 2 years ago #

    Hello Damian,

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

    Thanks!

  6. Rhand
    Member
    Posted 2 years ago #

    @ 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.

  7. VirtbizGuy
    Member
    Posted 2 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic