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.


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

  1. 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 !

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

  3. 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/

  4. 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 !

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

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

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

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

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

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

« | »
 
jasar Web Solutions