Assigning Role On WordPress Registration Page

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.

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


Comments

20 responses to “Assigning Role On WordPress Registration Page”

  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.

    1. Updated the post as per required. Thanks 🙂

  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!

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

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

    1. @Devexus Noted! Will definitely look into it as soon as time permits 🙂

      Thanks again for dropping by.

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

        1. 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. elitemodelsmexico Avatar
    elitemodelsmexico

    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

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

    1. Hi Julius

      Sorry for the late reply, but yes. You should include the code into functions.php

  9. […] I didn’t expect that the previous article, Assigning Role On WordPress Registration Page gained so much attention. This might be a little late for a follow up, but better late than […]

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

  11. wow! very nice plugin
    thakns to share with us
    cheers 😉

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