// Verbose example to show steps: $user_email = "example@domain.com"; $email_array = explode( "@", $user_email ); $email_domain = array_pop( $email_array ); echo $email_domain; // Will output "domain.com" // Simple one line, get domain from email: $domain = array_pop( explode( "@", $user_email ) ); /** * Here's a more detailed example of getting a user's email * and then extracting the domain in WordPress. (assumes * you have the user ID in $user_id). * * @uses get_user_by() */ $user = get_user_by( 'ID', $user_id ); $user_email = $user->user_email; $domain = array_pop( explode( "@", $user_email ) ); /** * Similar WP example to handle getting the email domain * when the user registers (assumes default WP reg form). */ $user_email = sanitize_email( $_POST['user_email'] ); $domain = array_pop( explode( "@", $user_email ) );
Get email domain from a user’s email
This post brought to you by RocketGeek, ButlerBlog, and the following: