WordPress/BuddyPress Add User Types


/ Published in: PHP
Save to your folder(s)

I used this plugin to create new roles/capabilities: http://wordpress.org/extend/plugins/capsman/


Copy this code and paste it in your HTML
  1. Put this in the BuddyPress theme /registration/register.php file:
  2. <label>Account Type:</label>
  3. <select>
  4. <option value="optiona">Option A</option>
  5. <option value="optionb">Option B</option>
  6. <option value="optionc">Option C</option>
  7. </select>
  8.  
  9.  
  10. Put the following code in your theme's functions.php
  11.  
  12. <?php
  13. /* Add sign-up field to BuddyPress sign-up array*/
  14. function bp_custom_user_signup_field( $usermeta ) {
  15. $usermeta['signup_type'] = $_POST['signup_type'];
  16.  
  17. return $usermeta;
  18. }
  19. add_filter( 'bp_signup_usermeta', 'bp_custom_user_signup_field' );
  20.  
  21. /* Add field_name from sign-up to usermeta on activation */
  22. function bp_user_activate_field( $signup ) {
  23.  
  24. update_usermeta( $signup['user_id'], 'signup_type', $signup['meta']['signup_type'] );
  25.  
  26. return $signup;
  27. }
  28. add_filter( 'bp_core_activate_account', 'bp_user_activate_field' );
  29.  
  30. function synchro_wp_usermeta($user_id, $password, $meta) {
  31. global $bp, $wpdb;
  32.  
  33. $type = $meta[signup_type];
  34.  
  35. update_usermeta( $user_id, 'signup_type', $type );
  36.  
  37. }
  38. add_action( 'wpmu_activate_user', 'synchro_wp_usermeta', 10, 3);
  39.  
  40. function synchro_wp_usermeta_blog($blog_id, $user_id, $domain, $path, $site_id, $meta) {
  41. global $bp, $wpdb;
  42.  
  43. $type = $meta[signup_type];
  44.  
  45. update_usermeta( $user_id, 'signup_type', $type );
  46.  
  47. }
  48.  
  49. add_action( 'wpmu_new_blog', 'synchro_wp_usermeta_blog', 10, 6 );
  50.  
  51. // change user role on registration
  52.  
  53. function register_role($user_id, $password, $meta) {
  54.  
  55. global $bp, $wpdb;
  56.  
  57. $userdata = array();
  58. $userdata['ID'] = $user_id;
  59. $userdata['role'] = $meta['signup_type'];
  60.  
  61. //only allow if user role is my_role
  62.  
  63. if ($userdata['role'] == 'optiona' || $userdata['role'] == 'optionb' || $userdata['role'] == 'optionc' ){
  64. wp_update_user($userdata);
  65. }
  66. }
  67.  
  68. add_action('wpmu_activate_user', 'register_role', 10, 3);
  69.  
  70. function register_role_blog($blog_id, $user_id, $domain, $path, $site_id, $meta) {
  71. global $bp, $wpdb;
  72.  
  73. $userdata = array();
  74. $userdata['ID'] = $user_id;
  75. $userdata['role'] = $meta['signup_type'];
  76.  
  77. //only allow if user role is my_role
  78.  
  79. if ($userdata['role'] == 'optiona' || $userdata['role'] == 'optionb' || $userdata['role'] == 'optionc' ){
  80. wp_update_user($userdata);
  81. }
  82.  
  83. }
  84.  
  85. add_action( 'wpmu_new_blog', 'register_role_blog', 101, 6 );
  86. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.