Welcome, visitor! Log in
 

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.

  • Action hooks look like this: do_action( "hook_name" )
  • Filter hooks look like this: apply_filters( "hook_name", "what_to_filter" ).

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