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
2042  *
2043  * @since 1.5.0
2044  *
2045  * @param string $email Email address to filter.
2046  * @return string Filtered email address.
2047  */
2048 function sanitize_email( $email ) {
2049      // Test for the minimum length the email can be
2050      if ( strlen( $email ) < 3 ) {
2051           return apply_filters( 'sanitize_email', '', $email, 'email_too_short' );
2052      }
2053
2054      // Test for an @ character after the first position
2055      if ( strpos( $email, '@', 1 ) === false ) {
2056           return apply_filters( 'sanitize_email', '', $email, 'email_no_at' );
2057      }
2058
2059      // Split out the local and domain parts
2060      list( $local, $domain ) = explode( '@', $email, 2 );
2061
2062      // LOCAL PART
2063      // Test for invalid characters
2064      $local = preg_replace( '/[^a-zA-Z0-9!#$%&\'*+\/=?^_`{|}~\.-]/', '', $local );
2065      if ( '' === $local ) {
2066           return apply_filters( 'sanitize_email', '', $email, 'local_invalid_chars' );
2067      }
2068
2069      // DOMAIN PART
2070      // Test for sequences of periods
2071      $domain = preg_replace( '/\.{2,}/', '', $domain );
2072      if ( '' === $domain ) {
2073           return apply_filters( 'sanitize_email', '', $email, 'domain_period_sequence' );
2074      }
2075
2076      // Test for leading and trailing periods and whitespace
2077      $domain = trim( $domain, " \t\n\r\0\x0B." );
2078      if ( '' === $domain ) {
2079           return apply_filters( 'sanitize_email', '', $email, 'domain_period_limits' );
2080      }
2081
2082      // Split the domain into subs
2083      $subs = explode( '.', $domain );
2084
2085      // Assume the domain will have at least two subs
2086      if ( 2 > count( $subs ) ) {
2087           return apply_filters( 'sanitize_email', '', $email, 'domain_no_periods' );
2088      }
2089
2090      // Create an array that will contain valid subs
2091      $new_subs = array();
2092
2093      // Loop through each sub
2094      foreach ( $subs as $sub ) {
2095           // Test for leading and trailing hyphens
2096           $sub = trim( $sub, " \t\n\r\0\x0B-" );
 
Line Code
2100
2101           // If there's anything left, add it to the valid subs
2102           if ( '' !== $sub ) {
2103                $new_subs[] = $sub;
2104           }
2105      }
2106
2107      // If there aren't 2 or more valid subs
2108      if ( 2 > count( $new_subs ) ) {
2109           return apply_filters( 'sanitize_email', '', $email, 'domain_no_valid_subs' );
2110      }
2111
2112      // Join valid subs into the new domain
2113      $domain = join( '.', $new_subs );
2114
2115      // Put the email back together
2116      $email = $local . '@' . $domain;
2117
2118      // Congratulations your email made it!
2119      return apply_filters( 'sanitize_email', $email, $email, null );
2120 }
2121
2122 /**
2123  * Determines the difference between two timestamps.
2124  *
2125  * The difference is returned in a human readable format such as "1 hour",
2126  * "5 mins", "2 days".
2127  *
2128  * @since 1.5.0