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