arie_cristianD

restrict username for registration

Jul 9th, 2025
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.91 KB | None | 0 0
  1.  
  2. add_filter( 'jnews_register_default_role', 'jnews_registrarion_without_spaces', 10 );
  3. add_filter( 'registration_errors', 'wp_registrarion_without_spaces', 10, 3 );
  4.  
  5. /**
  6.  * Restrict username on JNews regisration.
  7.  */
  8. function jnews_registrarion_without_spaces( $default_role ) {
  9.     $user_login = sanitize_user( wp_unslash( $_POST['username'] ) );
  10.     if ( ! preg_match( '/^[a-zA-Z0-9]+$/', $user_login ) ) {
  11.         throw new \Exception( 'Username hanya boleh terdiri dari huruf dan angka tanpa spasi atau simbol' );
  12.     }
  13.     return $default_role;
  14. }
  15.  
  16. /**
  17.  * Restrict username on WordPress regisration.
  18.  */
  19. function wp_registrarion_without_spaces( $errors, $sanitized_user_login, $user_email ) {
  20.     if ( ! preg_match( '/^[a-zA-Z0-9]+$/', $sanitized_user_login ) ) {
  21.         $errors->add( 'username_invalid', __( 'Username hanya boleh terdiri dari huruf dan angka tanpa spasi atau simbol.', 'textdomain' ) );
  22.     }
  23.     return $errors;
  24. }
  25.  
Add Comment
Please, Sign In to add comment