WordPress.org

Ready to get started?Download WordPress

Forums

Writing a Plugin to amend the core - possible ? (9 posts)

  1. swinhoe
    Member
    Posted 2 years ago #

    Hi Folks,
    Im struggling a bit with my first Plugin code and wondered if you clever folks might be able to show me the light.

    Im trying to write a plugin that amends the is_email function in includes/formatting.php to allow email addresses that use apostrophes.

    Fine in principle, but the more i read, the more i think that I cant by-pass that function, and I cant stop it from throwing the error mid-way through when it finds the apostrophe in question.

    Is there a way to preceed the function and skip the exiting function ?

    Im keen to get a handle on whats possible before i continue down this route...

    From what I have read its not really possible ?

    http://wordpress.org/support/topic/how-to-write-a-plugin-that-can-override-core-files?replies=8

    http://wordpress.org/support/topic/apostrophes-or-single-quote-support-in-user-registration-email-address?replies=5#post-2337601

    Thanks.

    b

  2. Jackson
    Member
    Posted 2 years ago #

    The function has a filter which tells you where it failed you could use to catch the exit and run your own validation and return true instead of false.

    // LOCAL PART
    	// Test for invalid characters
    	if ( !preg_match( '/^[a-zA-Z0-9!#$%&\'*+\/=?^_{|}~\.-]+$/', $local ) ) {
    		return apply_filters( 'is_email', false, $email, 'local_invalid_chars' );
    	}

    Psuedo-code...

    add_filter( 'is_email', 'my_email_validation', 10, 3 );
    function my_email_validation( $return, $email, $context ) {
        if( 'local_invalid_chars' != $context ) {
            return $return;
        } else {
            // Validate $email however you please, return false or true
        }
    }

    If you think this should be part of core, head over to Trac and open a ticket. http://core.trac.wordpress.org/

    Hope that helps.

  3. Jackson
    Member
    Posted 2 years ago #

    Also note, that if you do this you will skip the domain validation parts of the is_email function and will need to include that validation in your function.

  4. swinhoe
    Member
    Posted 2 years ago #

    Hi Jackson, thanks for the response.

    Im hammering away at the code now.. will post-up what I have and look forward to comments.

    Cheers

  5. swinhoe
    Member
    Posted 2 years ago #

    Am I correct i thinking that I will need to run all of the validation that follows the local part in the 'is_email' function ?

  6. Jackson
    Member
    Posted 2 years ago #

    That's correct.

    Check this out: https://gist.github.com/972837

    Might do just what you want.

  7. swinhoe
    Member
    Posted 2 years ago #

    Hmmm.. Ive tested it, and no-dice.. still errors - sadly.

    But it cant be far away from what I need..

    *keeps going*

  8. _kb
    Member
    Posted 2 years ago #

    Ran up against this same problem a while ago, and this was the solution I came up with. Put the following code into your functions.php file. Pulling the email address from the post variable was the only way that would work.

    function email_validator($email_address='' ){
            //Validator will fail on apostrophes when is_email is called if we don't check the POST var instead of $email_address
            $user_email_address = ($_POST['email']) ? $_POST['email'] : $email_address;
            if(filter_var(stripslashes($user_email_address), FILTER_VALIDATE_EMAIL)){
                return stripslashes($user_email_address);
            } else {
                return false;
            }
    }
    add_filter('is_email', 'email_validator', 1, 1);
    add_filter( 'sanitize_email', 'email_validator', 1, 1 );
  9. swinhoe
    Member
    Posted 2 years ago #

    Works a treat.

    Good stuff..thanks !
    :)

Topic Closed

This topic has been closed to new replies.

About this Topic