How to create and retrieve data from a special registration form?

The following code creates a new role. In fact is simply clones the subscriber role, so if you are not using that role currently, you may as well just use that.

The following function need only be run once (it run’s when a user is ‘admin side’)

//Creates a custom role called 'my_new_role'.
//Run this once then feel free to comment out the next line:
add_action('admin_init', 'my_custom_role');
function my_custom_role(){
    global $wp_roles;

    $subscriber = $wp_roles->get_role('subscriber');

    //Adding a 'new_role' with subscriber caps
    $wp_roles->add_role('my_new_role', 'My New Role', $subscriber->capabilities);
    //Optional add/remove caps, like the capability to access the dashboard
    //$wp_roles->remove_cap('my_new_role','read');  
}

See the codex for more information on capabilities.

Wherever your are processing the form you’ll want to create a new user and assign them the role ‘my_new_role’. To do this use the wp_insert_user. As a brief example:

wp_insert_user( array (
    'user_login' => 'JoeBloggs',
    'user_pass' => 'a_password_43463', 
    'first_name' => 'Joseph',
    'last_name' => 'Bloggs',
    'role'=>'my_new_role') ) ;

Prior to the above you should have performed all the nonce-checks, data validation and any other checks.

You may wish to redirect these new users to a different page (not the dashboard) when they log-in. To do this use the login_redirect filter.

add_filter('login_redirect', 'dashboard_redirect');
function dashboard_redirect($url) {
    global $current_user;
    get_currentuserinfo();

    if (current_user_can('my_new_role')) {
             //current user is 'my_new_role', take them somewhere else...
             $url = home_url(); 
        }
        return $url;
    }

Leave a Comment