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

I Have Written 46 Posts So Far!

hrbg
Getting The Latest Tweet...
hrbg
Looking For Custom WordPress Web Development? Contact Graphic Dreams Studio

hrbg

the tags: , , , ,

hrbg
» «
hrbg
Creative Commons License

Leave a Reply

1 Trackback or Pingback for this entry


hrbg

Grunge Style Backgrounds – Download

The second thing I'm tossing out for download tonight is a pack of 24 Grunge-Style backgrounds in a nice little .zip (well 31.5 MB's) for you to snag. Each back...

read more

Blood Splatter Screensaver – Dexter

Popping on today because I was a little bored at work, so I figured I'd make something to give away and be able to use myself. If you know me, you know that I l...

read more

Carbon Fiber Texture

Here is a carbon fiber texture that could be used for backgrounds, or chopping up to use for smaller projects. The file is 1000px by 1000px, so it gives you som...

read more


Cmin.us Social Bookmarks – WP Plugin

Cmin.us Social Bookmarks WordPress Plugin simplifys the way you and your surfers get ShortURLs and Submit Your Posts to Social Media/Bookmarking Sites.The Plugin places a...

read more

Author Tweets – WordPress Plugin

This is a WordPress Plugin for multi-author, and personal websites/blogs. It displays the 'About the Author' box at the end of all posts, with a Twitter Twist. I have com...

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




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