Guest User

Untitled

a guest
Mar 17th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.89 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * Filters the errors encountered when a new user is being registered.
  5. *
  6. * The filtered WP_Error object may, for example, contain errors for an invalid
  7. * or existing username or email address. A WP_Error object should always returned,
  8. * but may or may not contain errors.
  9. *
  10. * If any errors are present in $errors, this will abort the user's registration.
  11. *
  12. * @since 2.1.0
  13. *
  14. * @param WP_Error $errors A WP_Error object containing any errors encountered
  15. * during registration.
  16. * @param string $sanitized_user_login User's username after it has been sanitized.
  17. * @param string $user_email User's email.
  18. */
  19. add_filter( 'registration_errors', 'smyles_allow_wp_login_register_empty_user_login', 9999, 3 );
  20.  
  21. /**
  22. * Allow empty user_login (username) from wp-login.php registration form
  23. *
  24. *
  25. * @since @@version
  26. *
  27. * @param $errors WP_Error
  28. * @param $sanitized_user_login
  29. * @param $user_email
  30. *
  31. * @return mixed
  32. */
  33. function smyles_allow_wp_login_register_empty_user_login( $errors, $sanitized_user_login, $user_email ){
  34.  
  35. // First remove empty_username error code to make sure there aren't any other errors
  36. $errors->remove( 'empty_username' );
  37. $error_codes = $errors->get_error_codes();
  38.  
  39. // Return errors and don't process further (we only want to proceed when empty_username is only error code)
  40. if( ! empty( $error_codes ) ){
  41. return $errors;
  42. }
  43.  
  44. return $errors;
  45. }
  46.  
  47. /**
  48. * Generate sequential padded user_login
  49. *
  50. * @author Myles McNamara
  51. *
  52. * @return string
  53. */
  54. function smyles_generate_next_seq_user_login(){
  55.  
  56. // Use 0 as default (if option does not exist yet), as 1 will be used for first user
  57. $last_user_num = get_option( 'smyles_custom_seq_usernames_last_id', 0 );
  58.  
  59. // Create padded username total length of 10 characters, increasing last ID by 1
  60. $gen_user_login = str_pad( (int) $last_user_num + 1, 10, 0, STR_PAD_LEFT );
  61.  
  62. if( username_exists( $gen_user_login ) ){
  63. // If generated new user login exists, update our last id +1 and do recursive call
  64. update_option( 'smyles_custom_seq_usernames_last_id', (int) $last_user_num + 1 );
  65. return smyles_generate_next_seq_user_login();
  66. }
  67.  
  68. return $gen_user_login;
  69. }
  70.  
  71. add_filter( 'pre_user_login', 'smyles_custom_username_seq_pre_user_login' );
  72.  
  73. /**
  74. * Set user_login to generated value, only if passed value is empty
  75. *
  76. * @author Myles McNamara
  77. *
  78. * @param $sanitized_user_login
  79. *
  80. * @return string
  81. */
  82. function smyles_custom_username_seq_pre_user_login( $sanitized_user_login ){
  83.  
  84. $sanitized_user_login = trim( $sanitized_user_login ); // to match wp_insert_user handling
  85.  
  86. /**
  87. * The user_login should be empty string when creating from wp-login.php registration page,
  88. * otherwise will contain a value when called by something else.
  89. *
  90. * There is a chance this will be called for updating a user (which is incorrect as wp_update_user should be called),
  91. * but even when updating a user, the passed user_login should have some type of value.
  92. */
  93. if( empty( $sanitized_user_login ) || $sanitized_user_login === 'GENERATE_CUSTOM_SEQ_USERNAME' ){
  94. $sanitized_user_login = smyles_generate_next_seq_user_login();
  95. }
  96.  
  97. return $sanitized_user_login;
  98. }
  99.  
  100. /**
  101. * Filters a user's meta values and keys immediately after the user is created or updated
  102. * and before any user meta is inserted or updated.
  103. *
  104. * Does not include contact methods. These are added using `wp_get_user_contact_methods( $user )`.
  105. *
  106. * @since 4.4.0
  107. *
  108. * @param array $meta {
  109. * Default meta values and keys for the user.
  110. *
  111. * @type string $nickname The user's nickname. Default is the user's username.
  112. * @type string $first_name The user's first name.
  113. * @type string $last_name The user's last name.
  114. * @type string $description The user's description.
  115. * @type bool $rich_editing Whether to enable the rich-editor for the user. False if not empty.
  116. * @type bool $syntax_highlighting Whether to enable the rich code editor for the user. False if not empty.
  117. * @type bool $comment_shortcuts Whether to enable keyboard shortcuts for the user. Default false.
  118. * @type string $admin_color The color scheme for a user's admin screen. Default 'fresh'.
  119. * @type int|bool $use_ssl Whether to force SSL on the user's admin area. 0|false if SSL is
  120. * not forced.
  121. * @type bool $show_admin_bar_front Whether to show the admin bar on the front end for the user.
  122. * Default true.
  123. * }
  124. *
  125. * @param WP_User $user User object.
  126. * @param bool $update Whether the user is being updated rather than created.
  127. */
  128. add_filter( 'insert_user_meta', 'smyles_custom_username_seq_verify', 10, 3 );
  129.  
  130. /**
  131. * Verify user was created, and then increase option value
  132. *
  133. *
  134. * @author Myles McNamara
  135. *
  136. * @param $meta
  137. * @param $user WP_User
  138. * @param $update
  139. *
  140. * @return mixed
  141. */
  142. function smyles_custom_username_seq_verify( $meta, $user, $update ){
  143.  
  144. // Don't want to verify if this is just an update call
  145. if( ! $update && $user && $user->user_login ){
  146.  
  147. // Check what the last user num was stored as
  148. $last_user_num = get_option( 'smyles_custom_seq_usernames_last_id', 0 );
  149.  
  150. // Verify that user_login of the user that was created, matches what is supposed to be the next user_login
  151. if( (int) $last_user_num + 1 === (int) $user->user_login ){
  152. // Update our option after verification
  153. update_option( 'smyles_custom_seq_usernames_last_id', (int) $user->user_login ); // Type casting to int causes 0000000001 to be 1 (trim leading zeros)
  154. }
  155.  
  156. }
  157.  
  158. return $meta;
  159. }
  160.  
  161. add_action( 'register_form', 'smyles_hide_wp_login_username_field' );
  162.  
  163. function smyles_hide_wp_login_username_field(){
  164.  
  165. // Hide the first <p> element in registerform, which is the userlogin input
  166. echo '<style>#registerform p:first-of-type { display: none; }</style>';
  167.  
  168. }
Add Comment
Please, Sign In to add comment