Extract the Domain Name from the Email Address

0

In this step we will connect a snippet to an existing new user registration form. This form was created in the Create a Member or Team Member Directory course. You can collect any information about your user that you need as long as your form includes an email address.

To get started, we will need to add a snippet to separate out the domain name (the end of the email address after the @) from the rest of the email address. 

We’ll need to collect a couple of IDs from your form, so go to the edit screen of your new user registration form. First, make note of your Form ID in the orange box at the top of the form.

Also make note of your email field ID:

With that information collected, let’s install the following snippet from Gravity Wiz:

// Update "123" to your form ID; and "4" to your Email field ID.
add_filter( 'gppa_live_merge_tag_value_123_4', function( $value ) {
    $bits = explode( '@', $value );
    return array_pop( $bits );
} );

Installing a Snippet

If you are not familiar with modifying your theme’s functions.php file, follow these instructions to install the snippet.

Note the instructions at the top of the snippet to replace 123 with your form ID (3 for my form from the screenshot above) and replace 4 with your email field ID (5 for my form from the screenshot above).

Now that the snippet is installed, head back to your form and add a single line text field after the email field and call it Domain Name.

On the Advanced Tab of the new field, enter the following default value: @{Email:5}, where 5 is the Field ID of your email field. This will pull in the results from the snippet we just installed.

Next test it out. When you enter an email address, you should see the domain name populated in the next field automatically:

Once we have completed testing the form to ensure that it all works, you can hide this field so that users won’t see it.