2

When a user click on a link to register it should assign them a different role, and not the default role. I've tried this link but it doesnt seem to work. I've placed it in the functions.php.

Separate registration and login for different roles

I dont need a plugin just the php code.

For example if the user click on this link it should give them the seller role automatically http://example.com/wp-login.php?action=register&role=seller If the user click on this link http://example.com/wp-login.php?action=register it should give them the default role.

Thanks

2

1 Answer 1

2

Ok, first you the register_form action hook is applied to the default WP registration form, so keep this in mind if you don't use that.

Secondly, the add_action is calling an unknown function. So most likely this is why it's not working.

So here's the corrected code. With added validation with error messages.

//create a hidden field for role
add_action('register_form','wpse_add_hidden_role_field');
  function wpse_add_hidden_role_field(){
    if ( isset( $_GET[ 'role' ] ) ){
      echo '<input id="user_role" type="hidden" tabindex="20" size="25" value="' . $_GET[ 'role' ] . '" name="role"/>';
  }
}


//validate we have permitted roles if not, don't allow subscription
add_filter( 'registration_errors', 'wpse_role_check' );
function wpse_role_check( $errors ){

  if( isset( $_POST[ 'role' ] ) ) {

    //only allow registration if roles are in this array.
    $permitted_roles = array(
        'buyer',
        'seller',
    );

    if( ! in_array( $_POST[ 'role' ], $permitted_roles ) ){

        $errors->add( 'role_not_allowed', __( '<strong>ERROR</strong>: This registration is not allowed.', 'my_textdomain' ) );

    }

  }

  // Else disallow public registration (i.e. no role query string found )
  // If you don't take this into account, anyone can register as subscribers
  else {

    $errors->add( 'public_not_allowed', __( '<strong>ERROR</strong>: Public registration is not allowed.', 'my_textdomain' ) );

  }

  return $errors;

}

//update user profile that have passed registration validation
add_action('user_register', 'wpse_update_role');
function wpse_update_role( $user_id ) {
  if ( isset( $_POST[ 'role' ] ) ){
    $userdata = array();
    $userdata[ 'ID' ] = $user_id;
    $userdata[ 'role' ] = $_POST[ 'role' ];

    wp_update_user( $userdata );

  }
} 
8
  • do I have to add anything in the wp-login.php page? like do_action( 'register_form' ); Aug 26, 2016 at 20:36
  • no, this should work as is. You should not edit core files. Unless you are using a custom form registration, then yes, you will need to add a custom hook into your form, like custom_form_hook and hook there
    – bynicolas
    Aug 26, 2016 at 20:38
  • Glad you got it working. Cheers! Mark it as solve so we can keep this site tidy :)
    – bynicolas
    Aug 26, 2016 at 20:44
  • Also if the users use this link example.com/wp-login.php?action=register can they go straight to subscriber role? Because at the moment if the user use that link it says public registration is not allowed Aug 26, 2016 at 20:56
  • Yes, they can, remove the else statement in wpse_role_check and it should work
    – bynicolas
    Aug 26, 2016 at 20:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.