Different/Multi Role Registration – WordPress

Posted by Me (Kris Jay) & Written on Aug.13, 2010

hrbg

Did you know you can have different role registration on sign-up?!

What I mean by this is if you are allowing people to register on your website (multi-author sites, themed site, etc...), and you have different or custom roles users can sign up for (subscriber, editor, custom, etc...), the current way of allowing this is by manually setting the users to the role that you wish to have them hold AFTER they have signed up.

This works well for most cases (when their roles are editors, new admins, etc...). But what if you have a member site, lets say a Music Site, that has a 'Fan' or 'Listener' role; but also a 'Band' or 'Artist' role, each with differnt capabilities. This wouldn't be a huge task if the site was small, and didn't have a huge sign-up rate. But what if the site became a hit? Think of the task of sorting through all of the sign-ups daily, and setting the role on each one. I'd consider that a pretty shitty day in my books.

Well I'm going to show you a quick and easy way to set a custom role on your registration form that will let your users sign up for the role of their choice by using different Registration URLs and the $_GET method.

Here is an example. You would use this URL for a 'listener' :

http://yourwebsite.com/wp-login.php?action=register&role=listener

And this URL for a 'band' :

http://yourwebsite.com/wp-login.php?action=register&role=band

It's not so hard.....really :)

First off, we will be working with our functions.php file. If you don't have one in your theme folder, just create it and upload it. The functions.php file is a magical, tappable resource that can be utilize to your advantage. But thats a whole new topic in itself.

So to start, open up your functions.php file and add this (I'll explain below):

add_action('register_form', 'register_role_custom');

function register_role_custom(){ ?>

<input id="role" type="hidden" tabindex="0" size="0" value= "<?php if (isset($_GET['role'])){echo $_GET['role'];} ?>" name="role"/>

<?php
}

What we are doing here is telling WordPress to add a new function called 'register_role_custom' to the 'register_form'. Within that function we add a simple html hidden input field that states what the custom role will be by using $_GET. The $_GET method is grabbing the 'role' query from the URL we are passing to it, and adds it to the form automaticly and silently without having the user select it.

Now that is only half our task. We still need to register the role to the user when he/she/them signs up and submits the register form. This is done by adding a new function to your functions.php file.

Add the following to your functions.php file (explanation below):

add_action('user_register', 'custom_user_role');

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

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

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

Now in this new addition, what we are doing here is telling WordPress to add a new function called ‘custom_user_role’ when the new user submits the registration form using ‘user_register’. In the function called ‘custom_user_role’ we are grabbing the submitted $userdata, including the pre-determained role, breaking it down into an array and checking using the 'if' statment weather or not that role exists ('your_custom_role' is used as an example), before updating the users info using 'wp_update_user'.

If you had multi roles you're letting users register for, you would simply replace your 'if' statment to:

if (
($userdata['role'] == "your_custom_role")or
($userdata['role'] == "another_custom_role")or
($userdata['role'] == "third_custom_role")
{
wp_update_user($userdata);
}

Make sure 'or' is directly after each new statment, except for the closer (last) role.

That's it! Now all you have to do is direct your users to the right sign-up form, making different roles automatic.

hrbg

Kristjan Jonasson has written 47 topics

hrbg
hrbg

hrbg

the tags: , , , ,

hrbg
» «
hrbg
Creative Commons License

comments powered by Disqus

hrbg

Nordic Knot Tattoo Skin – Female

If you head over to the Nexus, you'll find that I uploaded two new tattoo packs for CCBT. They're both the Nordic Knot for females, but one is the finished ski...

read more

Nordic Knot Tattoo PSD – Male

Here is a favorite tattoo design of mine based off of Icelandic artistic knots. I created the design to be released with CCBT in the first place, but decided t...

read more

House Telvanni Male Tattoo PSDs

This is the PSD resource for my House Telvanni Tattoo Skin for CCBT (Skyrim) that is available for download on the Nexus. I had this on the Nexus originally, bu...

read more


WordPress Tetris – WordPress Plugin

This Plugin is just for fun, and can take up a bunch of your time once you start playing. Everyone knows/has played the game Tetris, well I decided it was time that WordP...

read more

Pre Loaded – WordPress Plugin

This is a very simple, but useful WordPress plugin that will pre-load images located in CSS files that are linked to your WordPress powered website. It is set-up to pre-l...

read more

Grapefile (File Sharing) – WordPress Plugin

Grapefile is a plugin for wordpress that lets your users share IMAGES, AVIs, MP3s, and ZIPs via their User Pannel (Admin Pannel). This is a complete Plug n Play Wordpress...

read more



"Ignorance is the foundation of atheism, and freethinking the cure of it"
Anthony Collins - 1676-1729