Menu Adam R Brown

WP hooks navigation: Home/browseActions indexFilters index

Source View: sanitize_email

To save our bandwidth, we show only a snippet of code around each occurence of the hook. View complete file in SVN (without highlighting).

Understanding Source Code

The best way to understand what a hook does is to look at where it occurs in the source code.

Remember, this hook may occur in more than one file. Moreover, the hook's context may change from version to version.

Source View

This hook occurs 8 times in this file.

Line Code
3749            * 'email_no_at', 'local_invalid_chars', 'domain_period_sequence', 'domain_period_limits',
3750            * 'domain_no_periods', 'domain_no_valid_subs', or no context.
3751            *
3752            * @since 2.8.0
3753            *
3754            * @param string $sanitized_email The sanitized email address.
3755            * @param string $email           The email address, as provided to sanitize_email().
3756            * @param string|null $message    A message to pass to the user. null if email is sanitized.
3757            */
3758           return apply_filters( 'sanitize_email', '', $email, 'email_too_short' );
3759      }
3760
3761      // Test for an @ character after the first position.
3762      if ( strpos( $email, '@', 1 ) === false ) {
3763           /** This filter is documented in wp-includes/formatting.php */
3764           return apply_filters( 'sanitize_email', '', $email, 'email_no_at' );
3765      }
3766
3767      // Split out the local and domain parts.
3768      list( $local, $domain ) = explode( '@', $email, 2 );
3769
3770      /*
3771       * LOCAL PART
3772       * Test for invalid characters.
3773       */
3774      $local = preg_replace( '/[^a-zA-Z0-9!#$%&\'*+\/=?^_`{|}~\.-]/', '', $local );
3775      if ( '' === $local ) {
3776           /** This filter is documented in wp-includes/formatting.php */
3777           return apply_filters( 'sanitize_email', '', $email, 'local_invalid_chars' );
3778      }
3779
3780      /*
3781       * DOMAIN PART
3782       * Test for sequences of periods.
3783       */
3784      $domain = preg_replace( '/\.{2,}/', '', $domain );
3785      if ( '' === $domain ) {
3786           /** This filter is documented in wp-includes/formatting.php */
3787           return apply_filters( 'sanitize_email', '', $email, 'domain_period_sequence' );
3788      }
3789
3790      // Test for leading and trailing periods and whitespace.
3791      $domain = trim( $domain, " \t\n\r\0\x0B." );
3792      if ( '' === $domain ) {
3793           /** This filter is documented in wp-includes/formatting.php */
3794           return apply_filters( 'sanitize_email', '', $email, 'domain_period_limits' );
3795      }
3796
3797      // Split the domain into subs.
3798      $subs = explode( '.', $domain );
3799
3800      // Assume the domain will have at least two subs.
3801      if ( 2 > count( $subs ) ) {
3802           /** This filter is documented in wp-includes/formatting.php */
3803           return apply_filters( 'sanitize_email', '', $email, 'domain_no_periods' );
3804      }
3805
3806      // Create an array that will contain valid subs.
3807      $new_subs = array();
3808
3809      // Loop through each sub.
3810      foreach ( $subs as $sub ) {
3811           // Test for leading and trailing hyphens.
3812           $sub = trim( $sub, " \t\n\r\0\x0B-" );
 
Line Code
3817           // If there's anything left, add it to the valid subs.
3818           if ( '' !== $sub ) {
3819                $new_subs[] = $sub;
3820           }
3821      }
3822
3823      // If there aren't 2 or more valid subs.
3824      if ( 2 > count( $new_subs ) ) {
3825           /** This filter is documented in wp-includes/formatting.php */
3826           return apply_filters( 'sanitize_email', '', $email, 'domain_no_valid_subs' );
3827      }
3828
3829      // Join valid subs into the new domain.
3830      $domain = implode( '.', $new_subs );
3831
3832      // Put the email back together.
3833      $sanitized_email = $local . '@' . $domain;
3834
3835      // Congratulations, your email made it!
3836      /** This filter is documented in wp-includes/formatting.php */
3837      return apply_filters( 'sanitize_email', $sanitized_email, $email, null );
3838 }
3839
3840 /**
3841  * Determines the difference between two timestamps.
3842  *
3843  * The difference is returned in a human-readable format such as "1 hour",
3844  * "5 minutes", "2 days".
3845  *
3846  * @since 1.5.0