How to change a users role on the wordpress registration form

I recently was trying to figure out a way to change the users role on the WordPress registration form.  I had no luck finding a way to do this from other websites, so I studied the WordPress core files and found a way to do this.  Follow the steps below:

All my edits are done in the function.php file within the theme, but this could easily be put into a plug-in.

1. Add a field to the registration form.  I am going to make this a hidden field so that it will not distract from the other fields.  Also, I am grabbing the role from the URL with the $_GET variable.  This is so I could have different registrations forms for different types of users.  (For example: URL is http://example.com/wp-login.php?action=register&role=my_role)

add_action('register_form','show_role_field');
function show_role_field(){ ?>
<input id="role" type="hidden" tabindex="20" size="25" value= "<?php if (isset($_GET['role'])){echo $_GET['role'];} ?>"  name="role"/>
	<?php
}

2. Next thing is to register that role when the user has submitted the registration form.

add_action('user_register', 'register_role');

function register_role($user_id, $password="", $meta=array()) {

   $userdata = array();
   $userdata['ID'] = $user_id;
   $userdata['role'] = $_POST['role'];

   //only allow if user role is my_role

   if ($userdata['role'] == "my_role"){
      wp_update_user($userdata);
   }
}

You will obviously have to change my_role to the role you want them to register for.  Also because we are taking the role from a $_GET variable we can modify this form so that other roles are passed to it.  If you want to have more than one role allowed to be registered then change the if statement to:

//allow other roles
if (
($userdata['role'] == "my_role1")or
($userdata['role'] == "my_role2")or
($userdata['role'] == "my_role3")
){
   wp_update_user($userdata);
}

As a security note, you DO NOT want to allow for a role with administration rights.  This is why the if statement above is so important to make sure a user will not register as an administrator.


21 Responses to “How to change a users role on the wordpress registration form”

  1. Halldór says:

    Thank you, thank you, thank you!

    I can’t believe somebody finally posted something about this. Been searching for a while now.

    You da man!

  2. venug says:

    What should I enter here at “my_role”?

    add_action(‘user_register’, ‘register_role’);

    function register_role($user_id, $password=”", $meta=array()) {

    $userdata = array();
    $userdata['ID'] = $user_id;
    $userdata['role'] = $_POST['role'];

    //only allow if user role is my_role

    if ($userdata['role'] == “my_role”){
    wp_update_user($userdata);
    }
    }
    Thanks !

  3. venug says:

    I tried :
    //allow other roles
    if (
    ($userdata['role'] == “my_role1″)or
    ($userdata['role'] == “my_role2″)or
    ($userdata['role'] == “my_role3″)
    {
    wp_update_user($userdata);
    }

    But the entire site is changing to blank

  4. Venug,
    Wordpress has several user roles already built in. You can see a description of those at http://codex.wordpress.org/Roles_and_Capabilities

    Also, if you want to add more roles you would have to use a plug-in that manages user roles. One that I have used in the past is from Justin Tadlock http://wordpress.org/extend/plugins/members/

  5. venug says:

    I got it .but if I use

    //allow other roles
    if (
    ($userdata['role'] == “my_role1″)or
    ($userdata['role'] == “my_role2″)or
    ($userdata['role'] == “my_role3″)
    {
    wp_update_user($userdata);
    }

    The entire site is changing to blank .It is ok when i use

    //only allow if user role is my_role

    if ($userdata['role'] == “my_role”){
    wp_update_user($userdata);
    }
    }
    Could you help me with it ?

    Thanks !

  6. Venug,
    Have you defined each role? my_role1, my_role2, and my_role3? You will have to either use the built in users roles or define your own through a role management plugin like the “members” one that I listed above.

  7. unrelated media says:

    Howdy. Just played around with this and had some issues.

    Issue: a real error is forgetting that the database defaults user roles to lower case. If your variable is ‘Widget_User’ it gets inserted in the defined roles as ‘widget_user’.

    Fix: use strtolower($_POST['role'])

    Full code:

    //create custom user role at registration
    ## example link: http://example.com/wp-login.php?action=register&role=my_role
    //
    add_action(‘register_form’,'show_role_field’);
    function show_role_field(){ ?>

    <input id="role" type="hidden" tabindex="20" size="25" value= "” name=”role”/>
    <?php
    }
    add_action('user_register', 'register_role');

    function register_role($user_id, $password="", $meta=array()) {

    $userdata = array();
    $userdata['ID'] = $user_id;
    $userdata['role'] = strtolower($_POST['role']);

    wp_update_user($userdata);

    }
    //end create user role at registration

  8. How can this be modified so that the user can select their own role and it uses the default registration page? I am thinking of my students from different courses who can select their role (i.e. course name) from a drop down box.

    Thanks!

  9. @Anthony – all you would have to do is use a drop down menu, “select” “option”, instead of the hidden field shown in the example. Also there would not be any reason to grab the $_GET variable when doing this.

  10. Rutwick says:

    Hi,
    I’ve been looking for a way to intercept the user registration process from the dashboard, and create a custom post as the user’s profile. I’m using the user_register hook, but I can’t seem to access the POST array. I tried echoing blah blah, but no luck, but surprisingly wp_die and exit work in the function. Here’s my code…

    add_action(‘user_register’, ‘createProfilePost’);

    function createProfilePost($user_id, $password=”", $meta=array())
    {
    print_r($_POST) //Doesn’t work!
    exit; //Works!
    }

    What is the right way to access all the data that is entered for user registration?

    Please let me know!
    Thanks
    Rutwick

  11. Rutwick,
    The code that you posted looks like it should work, although there should be a semicolon after the print_r($_POST);

  12. yeviko says:

    Thanks a lot for this! Just a few lines of code can make miracle!

  13. Lindsay says:

    I am trying to put together a quick volunteer signup website for a non profit event and I am just a little stuck.

    I would lke to create the drop down as suggested by Anthony but I am stuck at one part. I obviously know how to build a physical drop down, but how do I populate each option with my roles that I had defined int he following code ie: my_role1

    My code right now is

    add_action(‘register_form’,'show_role_field’);
    function show_role_field(){ ?>

    photographer
    photographer
    photographer

    <?php
    }
    add_action('user_register', 'register_role');

    function register_role($user_id, $password="", $meta=array()) {

    $userdata = array();
    $userdata['ID'] = $user_id;
    $userdata['role'] = $_POST['role'];

    //only allow if user role is my_role
    if (
    ($userdata['role'] == "photographer")or
    ($userdata['role'] == "general")or
    ($userdata['role'] == "makeup artist")
    ){
    wp_update_user($userdata);

    }
    }

    Obviously I need to populate the option fields but I just don't know how.

  14. Lindsay says:

    I also found one more little issue with the code as described.

    If someone is filling out the info and they make a mistake in the form it refreshes the form and removed the url query to a simple “registration” url which then removes the automatically added role.

    I am amazed that this is as hard as it is. I never built a website based off of users before but I had simply assimed there were better ways to manage users!!!

  15. Lindsay,
    On your second question, you might be able to set up a hidden field to store the role in which will then be posted back to the page if there is an error. For instance:

    < ?php
    echo "<input type='hidden' name='role' value='";

    if (isset($_GET['role'])){
    echo $_GET['role'];
    }else if (isset($_POST['role'])){
    echo $_POST['role'];
    }

    echo "' />";

    ?>

    You can take this same concept to the form creation.

  16. Lindsay,
    On your first question, if I understand the question correctly, I would do it with a foreach and an array….

    < ?php
    $myroles = array('photographer', 'general', 'makeup artist');
    echo"<select name='list_of_roles'>";
    foreach ($myroles as $myrole){
    echo "<option value='$myrole'>$myrole</option>";
    }
    echo "</select>";
    ?>

    If you want to get all roles within wordpress you can do the following:

    global $wp_roles;
    $all_roles = $wp_roles->roles;

    Then you can run that through your foreach. This is dangerous since it will also include administration roles… this is why hand selecting which roles are accepted provide for the security.

    You can also use the array as part of your security check instead of a bunch of if statements:

    //allow other roles
    if (in_array($userdata['role'], $myroles)){
    wp_update_user($userdata);
    }

  17. [...] file. I was having troubles figuring out how to do this at first, but managed to find this article. In that article, it shows us how to do something similar with the use of URL [...]

  18. Ano says:

    Thanks!

  19. naveen says:

    i want to create multiple registrations forms for different category users, can any one can help in searching theme or plugin for wordpress site….

  20. Pr says:

    Hi! I want this: when someone fill the form (or survey) and do submit, i want to change his role. I have a role for the people who don’t fill the survey and another one for the people who has filled it (theese ones can see full content). Would it be posible? Thanks!

  21. Heinz says:

    Thanks for posting!

Leave a Reply

« | »