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