0

I have customised the worpress registration page by adding new fields using the Cimy User Extra Fields plugin so my site is now fine for allowing users to register as contributors.

I now want to create a different link to allow registrations as subscribers but with different fields displaying on the registration form.

What is the best way to do this? Another plugin? PHP functions?

Thanks.

1
1

I don't know how "Cimy User Extra Fields" works, but you can add fields to the user registration page with:

function extra_user_profile_fields_wpse_92164() {
  echo '<label for="extra_field">',_("Extra Field").'</label>';
  echo '<input type="text" name="extra_field" value="" size="25" id="extra_field" />';
}
add_action( 'register_form', 'extra_user_profile_fields_wpse_92164' );

To switch subscriber types you could pass in $_GET parameters, which is how the default wp-login.php forms works. So...

function extra_user_profile_fields() {
  if (!isset($_GET['utype'])) return false;
  echo '<input type="hidden" name="register_as" value="'.esc_attr($_GET['utype']).'" />';
  if ('subscriber' == $_GET['utype']) {
    echo '<label for="extra_field_sub">',_("Extra Field Subscriber").'</label>';
    echo '<input type="text" name="extra_field_sub" value="" size="25" id="extra_field_sub" />';
  } else {
    echo '<label for="extra_field">',_("Extra Field").'</label>';
    echo '<input type="text" name="extra_field" value="" size="25" id="extra_field" />';
  }
}
add_action( 'register_form', 'extra_user_profile_fields' );

Now you can pass data to the form like: http://example.com/wp-login.php?action=register&utype=subscriber

You can then save the date pretty much as demonstrated in the Codex for the user_register hook.

 function myplugin_registration_save($user_id) {

    if ( isset( $_POST['extra_field'] ) ) {
      update_user_meta($user_id, 'extra_field', $_POST['extra_field']);
    }
    
    if ( isset( $_POST['extra_field_sub'] ) ) {
      update_user_meta($user_id, 'extra_field_sub', $_POST['extra_field_sub']);
    }
    
    // alter the user Role
    if ('subscriber' == $_POST['register_as'] || 'contributor' == $_POST['register_as']) { 
      wp_update_user(array('ID'=>$user_id,'role'=>$_POST['register_as'])); 
    }
}
add_action('user_register', 'myplugin_registration_save');

Warning

Consider this code 'beta'. I wrote the function to only allow role changes to 'contributor' or 'subscriber' which are low-permission roles, but the function could stand better data validation before being put into service.

7
  • how does that tell it to create the user as a subscriber rather than the default setting of contributor? – user28323 Mar 24 '13 at 19:42
  • LOl... you are right. You could parse the extra fields and infer that but I should have included a hidden field. I'll edit things. – s_ha_dum Mar 24 '13 at 19:44
  • also...what would then happen if the example.com/wp-login.php?action=register page was accessed without adding one of the &utype=subscriber or example.com/wp-login.php?action=register&utype=contributor? Which fields would show? Neither? That would be a problem... – user28323 Mar 24 '13 at 19:56
  • On my system, the default form appears and the user is registered as a subscriber. "Cimy" may have changed that for you. – s_ha_dum Mar 24 '13 at 20:18
  • yeah I follow that, that's great. How would validation checks work/affect the form submission? – user28323 Mar 24 '13 at 20:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy