[wp-trac] [WordPress Trac] #28092: Ability to filter or remove error codes from WP_Error class

WordPress Trac noreply at wordpress.org
Thu May 1 16:39:25 UTC 2014


#28092: Ability to filter or remove error codes from WP_Error class
------------------------------------+------------------------------
 Reporter:  AdamCapriola            |       Owner:
     Type:  feature request         |      Status:  new
 Priority:  normal                  |   Milestone:  Awaiting Review
Component:  Login and Registration  |     Version:  2.1
 Severity:  normal                  |  Resolution:
 Keywords:  reporter-feedback       |     Focuses:
------------------------------------+------------------------------

Comment (by AdamCapriola):

 Here's something I'm working on. I am filtering the username validation
 and want to give a more specific error message to the registrant than
 "ERROR: This username is invalid because it uses illegal characters.
 Please enter a valid username." when the username is invalid. It would be
 nice if I could remove that error message since it becomes extraneous.

 {{{
 // Make sure username only contains letters, numbers, and underscores and
 is between 3 and 25 characters long
 add_filter( 'validate_username', 'ac_validate_username', 10, 2 );

 function ac_validate_username( $valid, $username ) {

         if ( preg_match( '/^[A-Za-z0-9_]{3,25}$/', $username ) )
                 $valid = true;
         else
                 $valid = false;

         return $valid;

 }

 // Give helpful error messages when registration fails
 add_filter( 'registration_errors', 'ac_registration_errors', 10, 3 );

 function ac_registration_errors( $errors, $sanitized_user_login,
 $user_email ) {

         // Unset 'user_login' error

         // Character issue
         if ( ! preg_match( '/^[A-Za-z0-9_]*$/', $_POST['user_login'] ) )
                 $errors->add( 'user_login_characters', __(
 '<strong>ERROR</strong>: Usernames may contain only letters, numbers, and
 underscores.' ) );

         // Length issue
         if ( ! preg_match( '/^.{3,25}$/', $_POST['user_login'] ) )
                 $errors->add( 'user_login_length', __(
 '<strong>ERROR</strong>: Usernames must be between 3 and 25 characters
 long.' ) );

         return $errors;

 }
 }}}

--
Ticket URL: <https://core.trac.wordpress.org/ticket/28092#comment:2>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list