Assigning Role
On Wordpress
Registration Page

octalforty shows you how to modify your Wordpress registration page and let your users choose a role of their choosing when registering to your site.

Update! A newer post has been published as a follow-up to this post.

Read Assigning Role On WordPress Registration & Profile Page

These few months, I’ve been playing around with various parts of WordPress theming. It was no easy task as I’m used to how Expression Engine allows me to do most of the stuff I needed without much PHP knowledge needed.

One part that I’ll try to customize most of the time, is the registration page of my WordPress installation itself. Here, I’ll show you how to add a field to the WordPress registration form by leveraging the useful Cimy User Extra Fields plugin.

The plugin

First off, let’s start with downloading Cimy User Extra Fields, upload to your WordPress plugin folder and activate it. Once you’ve done that, you should see a new options aptly named Cimy User Extra Fields under Settings.

Cimy User Extra Field under Settings

Screenshot showing Cimy User Extra Field under Settings.

Next we need to add a new field on the registration form that allows the user to select the role of their choosing.

Settings to add a new field with role options

Screenshot showing settings to add a new field with role options.

Once you’ve added the field and save changes, check your registration page and you should see something like the ones below.

Wordpress registration page with the new role field

Screenshot showing the new role field on the registration page.

Linking it all up

Now that’s done, we’ll need to include the code inside our functions.php 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 parameters.

We’ll need to modify a bit on the code to get it working with our dropdown field though. Below is the modified code, courtesy of my talented friend Rizal.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
<?php
//
// Modify registration form to include roles
//
add_action('user_register', 'register_role');
 
function register_role($user_id, $password="", $meta=array()) {
$userdata = array();
$userdata['ID'] = $user_id;
$userdata['role'] = $_POST['cimy_uef_USERTYPE'];
if ($userdata['role'] == 'A Contributor') {
$userdata['role'] = 'contributor';
}
if ($userdata['role'] == 'An Editor') {
$userdata['role'] = 'editor';
}
 
//only allow if user role is my_role
if (($userdata['role'] == "contributor") or ($userdata['role'] == "editor")){
wp_update_user($userdata);
}
}
?>
view raw functions.php hosted with ❤ by GitHub

And there you have it! Now all users registered to your WordPress site will be assigned a role of their choosing automatically.

23 Responses

  1. If you’re using Cimy User Extra Fields, then add_action(‘register_form’,'show_role_field’) and function show_role_field() { … } are not necessary.

  2. Nice site! Looking forward to more posts from you.

  3. Hi, I’m trying to make this work because I need to let users choose from three different kinds of groups that I’ll be using to send newsletter (one is weekly, one is inmediate once a post is uploaded and the other option is not to receive the newsletter)

    I did everything as it’s stated here, but I get this problem:
    When I register a user (no matter which of the 3 roles I choose), the role that WordPress assigns them is “None”. I’ve checked the code and it seems to be alright, but I put it here anyway so if anyone can see the mistake please let me know

    <?php
     //
    // Modify registration form to include roles
    //
    add_action('user_register', 'register_role');
    
    function register_role($user_id, $password="", $meta=array()) {
        $userdata = array();
        $userdata['ID'] = $user_id;
        $userdata['role'] = $_POST['cimy_uef_USERTYPE'];
        if ($userdata['role'] == 'Actualizaciones inmediatas') {
           $userdata['role'] = 'Inmediato';
        }
        if ($userdata['role'] == 'Actualizaciones semanales') {
           $userdata['role'] = 'Semanal';
        }
         if ($userdata['role'] == 'Ninguna actualizacion') {
           $userdata['role'] = 'Subscriber';
        }
     
        //only allow if user role is my_role
        if (($userdata['role'] == "Inmediato") or ($userdata['role'] == "Semanal") or ($userdata['role'] == "Subscriber")){
           wp_update_user($userdata);
        }
     }
     ?>

    Hope you can help me!

    • Hi Devexus

      I’ve tried creating a custom role called “Tester” and seems to have found the problem. Your role must be in lowercase format.

      In your case, it should look something like:

      <?php
      //
      // Modify registration form to include roles
      //
      add_action('user_register', 'register_role');
      
      function register_role($user_id, $password="", $meta=array()) {
          $userdata = array();
          $userdata['ID'] = $user_id;
          $userdata['role'] = $_POST['cimy_uef_USERTYPE'];
          if ($userdata['role'] == 'Actualizaciones inmediatas') {
             $userdata['role'] = 'inmediato';
          }
          if ($userdata['role'] == 'Actualizaciones semanales') {
             $userdata['role'] = 'semanal';
          }
           if ($userdata['role'] == 'Ninguna actualizacion') {
             $userdata['role'] = 'subscriber';
          }
       
          //only allow if user role is my_role
          if (($userdata['role'] == "inmediato") or ($userdata['role'] == "semanal") or ($userdata['role'] == "subscriber")){
             wp_update_user($userdata);
          }
       }
       ?>

      Try it out and do tell if it works!

  4. Great, that solved the problem!! I thought it had to be exactly the same as the Role, didn’t realize that it was lowcase in the example.

    On another subject, is there a way to make the extra field work not only in registration?
    What I mean is, can someone who chose a role when registering change it themselves to a different one using the extra field in their dashboard? Or do I have to modify it from the admin panel?

    • @Devexus

      As a matter of fact, yes they can if you checked the “Show the field in user’s profile” option when creating a new field with Cimy User Extra Fields.

      The option will then be available in their profile page at /wp-admin/profile.php

  5. @Yassir Yahya First of all, thank you for solving the other problem, I forgot to say it before.

    Now, I did check that option and the users get the dropdown menu in their dashboard. But the problem is that the role doesn’t change when they change the kind of update they want. I checked in the A&U extended plugin, and a new column has been added with that extra field information. If the user changes the option it’s modified in that column, but the WP role stays the same. Of course it wouldn’t be that much of a trouble to check every now and then that page and manually change the roles, but if there’s a way to do it automatically it would be wonderful.

    • @Devexus Noted! Will definitely look into it as soon as time permits :)

      Thanks again for dropping by.

      • Also looking for the right tweak to make this happen….

        I tried this

        add_action(‘personal_options_update’, ‘register_role’);

        but that didn’t work.

      • Hi Nahum. I’ve written a new post on how to achieve this. Take a look and don’t forget to drop some comments on what you think! :)

  6. Hi,

    I’ve tried this steps, but in my case, it keeps taking the default role which is defined at domain/wp-admin/options-general.php

    Am I doing something wrong?

  7. hi was looking for something like that for my site, just that I have a question, where in the functions.php file I insert the code. and it would be possible to have different registration forms with different fields for each user type

    thanks

    • @elitemodelsmexico Hi there. I’ll usually include the codes anywhere in between the opening PHP tag and closing PHP tag

  8. Ive been trying to get the code to work and ive coped the code straight from the page but the users roles are not being changed according to what’s been chosen. What am i doing wrong? I’m supposed to put the code into the functions.php file of the current theme im using, correct?

  9. I tried your code with Cimy User Extra Field, but it still registers user as Subscriber as default.

    the code was added on top of the functions.php

    I’m using wordpress 3.3.1

  10. Tried this on 3.5.1 but roles are still defaulted to Subscriber.

  11. Just wanted to say thank you for this excellent post. I have tried several different ways of achieving this kind of role selection at registration, and this is the only one I was able to get to work. The problem I faced was in connection with woocommerce overiding the role selected with ‘customer’ everytime. I found with a combination of a different field-editing plugin (WP-Members as opposed to Cimy) I was able to use your PHP code to eventually assign my custom user types via the registration. Thank you so much! :)

  12. I want to use Contributor and Author as options. I tested and user can register as Contributor but when I choose Author as role, it assign role as Subscriber.

    function register_role($user_id, $password="", $meta=array()) {
       $userdata = array();
       $userdata['ID'] = $user_id;
       $userdata['role'] = $_POST['cimy_uef_USERTYPE'];
       if ($userdata['role'] == 'Contributor') {
          $userdata['role'] = 'contributor';
       }
       if ($userdata['role'] == 'Author') {
          $userdata['role'] = 'author';
       }
     
       //only allow if user role is my_role
       if (($userdata['role'] == "contributor") or ($userdata['role'] == "author")){
          wp_update_user($userdata);
       }
  13. Hey,

    Does the code go in the functions.php of WordPress or of the Theme your using? It’s not clear on your tutorial.

    Cheers

Leave a Reply

Allowed tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>