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
1689  *
1690  * @since 1.5.0
1691  *
1692  * @param string $email Email address to filter.
1693  * @return string Filtered email address.
1694  */
1695 function sanitize_email( $email ) {
1696      // Test for the minimum length the email can be
1697      if ( strlen( $email ) < 3 ) {
1698           return apply_filters( 'sanitize_email', '', $email, 'email_too_short' );
1699      }
1700
1701      // Test for an @ character after the first position
1702      if ( strpos( $email, '@', 1 ) === false ) {
1703           return apply_filters( 'sanitize_email', '', $email, 'email_no_at' );
1704      }
1705
1706      // Split out the local and domain parts
1707      list( $local, $domain ) = explode( '@', $email, 2 );
1708
1709      // LOCAL PART
1710      // Test for invalid characters
1711      $local = preg_replace( '/[^a-zA-Z0-9!#$%&\'*+\/=?^_`{|}~\.-]/', '', $local );
1712      if ( '' === $local ) {
1713           return apply_filters( 'sanitize_email', '', $email, 'local_invalid_chars' );
1714      }
1715
1716      // DOMAIN PART
1717      // Test for sequences of periods
1718      $domain = preg_replace( '/\.{2,}/', '', $domain );
1719      if ( '' === $domain ) {
1720           return apply_filters( 'sanitize_email', '', $email, 'domain_period_sequence' );
1721      }
1722
1723      // Test for leading and trailing periods and whitespace
1724      $domain = trim( $domain, " \t\n\r\0\x0B." );
1725      if ( '' === $domain ) {
1726           return apply_filters( 'sanitize_email', '', $email, 'domain_period_limits' );
1727      }
1728
1729      // Split the domain into subs
1730      $subs = explode( '.', $domain );
1731
1732      // Assume the domain will have at least two subs
1733      if ( 2 > count( $subs ) ) {
1734           return apply_filters( 'sanitize_email', '', $email, 'domain_no_periods' );
1735      }
1736
1737      // Create an array that will contain valid subs
1738      $new_subs = array();
1739
1740      // Loop through each sub
1741      foreach ( $subs as $sub ) {
1742           // Test for leading and trailing hyphens
1743           $sub = trim( $sub, " \t\n\r\0\x0B-" );
 
Line Code
1747
1748           // If there's anything left, add it to the valid subs
1749           if ( '' !== $sub ) {
1750                $new_subs[] = $sub;
1751           }
1752      }
1753
1754      // If there aren't 2 or more valid subs
1755      if ( 2 > count( $new_subs ) ) {
1756           return apply_filters( 'sanitize_email', '', $email, 'domain_no_valid_subs' );
1757      }
1758
1759      // Join valid subs into the new domain
1760      $domain = join( '.', $new_subs );
1761
1762      // Put the email back together
1763      $email = $local . '@' . $domain;
1764
1765      // Congratulations your email made it!
1766      return apply_filters( 'sanitize_email', $email, $email, null );
1767 }
1768
1769 /**
1770  * Determines the difference between two timestamps.
1771  *
1772  * The difference is returned in a human readable format such as "1 hour",
1773  * "5 mins", "2 days".
1774  *
1775  * @since 1.5.0