bp_core_validate_user_signup

Validate a user name and email address when creating a new user.

Description

(array) bp_core_validate_user_signup( (string) $user_name, (string) $user_email ); 

Returns (array)

Results of user validation including errors, if any.

Parameters (2)

0. $user_name (string)
Username to validate.
1. $user_email (string)
Email address to validate.

Usage

  1. if ( !function_exists( 'bp_core_validate_user_signup' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'buddypress/bp-members/bp-members-functions.php'; 
  3.  
  4. // Username to validate. 
  5. $user_name = ''; 
  6.  
  7. // Email address to validate. 
  8. $user_email = ''; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = bp_core_validate_user_signup($user_name, $user_email); 
  12.  

Defined (1)

The function is defined in the following location(s).

/bp-members/bp-members-functions.php  
  1. function bp_core_validate_user_signup( $user_name, $user_email ) { 
  2.  
  3. // Make sure illegal names include BuddyPress slugs and values. 
  4.  
  5. // WordPress Multisite has its own validation. Use it, so that we 
  6. // properly mirror restrictions on username, etc. 
  7. if ( function_exists( 'wpmu_validate_user_signup' ) ) { 
  8. $result = wpmu_validate_user_signup( $user_name, $user_email ); 
  9.  
  10. // When not running Multisite, we perform our own validation. What 
  11. // follows reproduces much of the logic of wpmu_validate_user_signup(),  
  12. // minus the multisite-specific restrictions on user_login. 
  13. } else { 
  14. $errors = new WP_Error(); 
  15.  
  16. /** 
  17. * Filters the username before being validated. 
  18. * @since 1.5.5 
  19. * @param string $user_name Username to validate. 
  20. */ 
  21. $user_name = apply_filters( 'pre_user_login', $user_name ); 
  22.  
  23. // User name can't be empty. 
  24. if ( empty( $user_name ) ) { 
  25. $errors->add( 'user_name', __( 'Please enter a username', buddypress ) ); 
  26.  
  27. // User name can't be on the blacklist. 
  28. $illegal_names = get_site_option( 'illegal_names' ); 
  29. if ( in_array( $user_name, (array) $illegal_names ) ) { 
  30. $errors->add( 'user_name', __( 'That username is not allowed', buddypress ) ); 
  31.  
  32. // User name must pass WP's validity check. 
  33. if ( ! validate_username( $user_name ) ) { 
  34. $errors->add( 'user_name', __( 'Usernames can contain only letters, numbers, ., -, and @', buddypress ) ); 
  35.  
  36. // Minimum of 4 characters. 
  37. if ( strlen( $user_name ) < 4 ) { 
  38. $errors->add( 'user_name', __( 'Username must be at least 4 characters', buddypress ) ); 
  39.  
  40. // No underscores. @todo Why not? 
  41. if ( false !== strpos( ' ' . $user_name, '_' ) ) { 
  42. $errors->add( 'user_name', __( 'Sorry, usernames may not contain the character "_"!', buddypress ) ); 
  43.  
  44. // No usernames that are all numeric. @todo Why? 
  45. $match = array(); 
  46. preg_match( '/[0-9]*/', $user_name, $match ); 
  47. if ( $match[0] == $user_name ) { 
  48. $errors->add( 'user_name', __( 'Sorry, usernames must have letters too!', buddypress ) ); 
  49.  
  50. // Check into signups. 
  51. $signups = BP_Signup::get( array( 
  52. 'user_login' => $user_name,  
  53. ) ); 
  54.  
  55. $signup = isset( $signups['signups'] ) && ! empty( $signups['signups'][0] ) ? $signups['signups'][0] : false; 
  56.  
  57. // Check if the username has been used already. 
  58. if ( username_exists( $user_name ) || ! empty( $signup ) ) { 
  59. $errors->add( 'user_name', __( 'Sorry, that username already exists!', buddypress ) ); 
  60.  
  61. // Validate the email address and process the validation results into 
  62. //errormessages. 
  63. $validate_email = bp_core_validate_email_address( $user_email ); 
  64. bp_core_add_validation_error_messages( $errors, $validate_email ); 
  65.  
  66. // Assemble the return array. 
  67. $result = array( 
  68. 'user_name' => $user_name,  
  69. 'user_email' => $user_email,  
  70. 'errors' => $errors,  
  71. ); 
  72.  
  73. // Apply WPMU legacy filter. 
  74. $result = apply_filters( 'wpmu_validate_user_signup', $result ); 
  75.  
  76. /** 
  77. * Filters the result of the user signup validation. 
  78. * @since 1.2.2 
  79. * @param array $result Results of user validation including errors, if any. 
  80. */