WordPress.org

Ready to get started?Download WordPress

Forums

Different User Levels on Registration (8 posts)

  1. Eric Mann
    Member
    Posted 6 years ago #

    I'm trying to set something up that sounds rather simple, but seems to be more complicated once I'm inside the code.

    I want users to be registered as either "Subscriber" or "Premium" automatically. Right now I have cases set up in the wp_login.php file for "action=register" and "action=premregister." The problem lies in making the new users look different inside the database.

    I'm able to pass along information through the role$ variable into the wp-usermeta table, but that doesn't do anything to the wp_cababilities or wp_user_level keys.

    Any tips on where to look for more help or steps I can take to fix the problem?

  2. Eric Mann
    Member
    Posted 6 years ago #

    It seems lately that I'm the only one ever to solve my problems ... but if you find any of this useful, please let me know!

    I was able to set up Premium registration by modifying the wp-login.php and registration.php files slightly.

    In wp-login.php I had to copy the function "register_new_user" to define the function "register_premium_user." From there, I changed:

    $user_id = wp_create_user( $user_login, $user_pass, $user_email );

    To

    $capa='contributor';
    $user_id = wp_create_user( $user_login, $user_pass, $user_email, $capa );

    This sent the appropriate user level to the registration scripts. In order to call this function, though, I also had to copy the case "register" and create a new case "premregister." Premium users can register by pointing to 'wp-login.php?action=premregister'.

    Next I had to change the line:

    $errors = register_new_user($user_login, $user_email);

    To

    $errors = register_premium_user($user_login, $user_email);

    And a little further down, I had to change:

    <form name="registerform" id="registerform" action="wp-login.php?action=register" method="post">

    To

    <form name="registerform" id="registerform" action="wp-login.php?action=premregister" method="post">

    Now we have premium users being directed to the correct registration page and being recognized as premium users. The final changes are in the registration.php file.

    At the very bottom is the function "wp_create_user." I had to change it a little bit to recognize my new value and send it on to the function "wp_insert_user" by changing this:

    function wp_create_user($username, $password, $email = '') {
    	global $wpdb;
    	$user_login = $wpdb->escape($username);
    	$user_email = $wpdb->escape($email);
    	$user_pass = $password;
    	$userdata = compact('user_login', 'user_email', 'user_pass');
    	return wp_insert_user($userdata);
    }

    To this:

    function wp_create_user($username, $password, $email = '', $capable = '') {
    	global $wpdb;
    	$user_login = $wpdb->escape($username);
    	$user_email = $wpdb->escape($email);
    	$user_pass = $password;
    	$role = $wpdb->escape($capable);
    	$userdata = compact('user_login', 'user_email', 'user_pass', 'role');
    	return wp_insert_user($userdata);
    }

    The empty '' after my $capable variable make it optional, so this addition won't affect any other part of WP's operation.

    Unfortunately, WP only writes user roles on updates, not creation, so it takes one last bit of modification to make this all work. Scroll back up to the "wp_insert_user" function and change this:

    if ( $update && isset($role) ) {
    	$user = new WP_User($user_id);
    	$user->set_role($role);
    }
    if ( !$update ) ) {
    	$user = new WP_User($user_id);
    	$user->set_role(get_option('default_role'));
    }

    To this:

    if ( $update && isset($role) ) {
    	$user = new WP_User($user_id);
    	$user->set_role($role);
    }
    if ( !$update && isset($role) ) {
    	$user = new WP_User($user_id);
    	$user->set_role($role);
    } else {
    	$user = new WP_User($user_id);
    	$user->set_role(get_option('default_role'));
    }

    Now, anyone who registers through wp-login.php?action=register will appear as a regular subscriber. Anyone who instead uses wp-login?action=premregister will appear as a contributor (merely for user level 1, it doesn't matter what you call them so long as you stay consistent).

    It took me 2 days to figure this out, let me know if it saves you some headache!

  3. gortsleigh
    Member
    Posted 6 years ago #

    Thank you, Eric! I had the exact same need, you have saved me two days' effort!

  4. Eric Mann
    Member
    Posted 5 years ago #

    I've decided to take this one step further. Hacking the WP source is fine if you want to do it every time you upgrade ... but I'm a bit lazy. So instead I built a plugin that does this all for me!

    What you do is install the plugin, build a new page (http://blog.com/premium) or something along those lines, and place the shortcode '[reglevel]' on the new page. When prospective users go to the new page, they are redirected to the registration screen and WP changes the user level to whatever you have set up in the Options»RegLevels settings.

    You can download the plugin here: http://www.jumping-duck.com/wordpress/. I'm still working to get it hosted on the WP site.

  5. Phil Meyer
    Member
    Posted 5 years ago #

    Hi Eric,

    Sorry for not helping when you were asking for it, but I have the coding know-how of a brick!

    However, I'm really excited about the plugin you wrote - it might just solve an issue or two for me.

    Thanks a bunch,

    Phil

  6. JohnnyPea
    Member
    Posted 5 years ago #

    Yeah! Thank You! Are you God? :P

    I would like to know whether it is possible to set different registration forms for certain types of user levels.
    e.x. Subscribers have to fill just "Username" and "E-mail"
    Editors have to fill "Username","E-mail" and "check-boxed" basic skills. Thank you in advance.

  7. AnkushR
    Member
    Posted 5 years ago #

    Hi Eric!

    I seem to be getting a 404 when I click on the link to your plugin. It seem like it would do everything I need in the user_level and user_capabilities department. Any chance you could point me to the correct link to your plugin?

    I'd really appreciate your help.

    ankush

  8. AnkushR
    Member
    Posted 5 years ago #

    Ahh...just found it...

    http://wordpress.org/extend/plugins/reglevel/

    thanks anyway for teh work on this plugin

Topic Closed

This topic has been closed to new replies.

About this Topic