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.
the tags: automatic, functions, php, registration, roles







