Get Domain from Email

By: Chris Titus | Asked: 10/22/2023
ForumsCategory: General questionsGet Domain from Email
Chris Titus asked 12 months ago
Hi all, I have a form in which I collect user's email addresses.  I'd like to pass the domain into a separate field.  How can I do that? Thanks for any help you can provide. Chris
2 Answers
Victor Font answered 12 months ago
Here are a few ways using JavaScript: https://www.plus2net.com/javascript_tutorial/string-email.php
Chris Titus replied 12 months ago

Thanks Victor. I just solved it by creating a shortcode. However, it is not working inside of a frm-stat shortcode - [frm-stats id=123 type=count 123="[456]@[user_email_domain]"]

For my project, I appended a category to an email address and used this as a filter. Using [email] worked fine. However, I realized that I'll have multiple people from the each company and the filter should apply to all people w/ the same domain. Hence, I created the shortcode below using ChatGPT (I'm not a developer). This shortcode isn't working the same way [email] worked. Are you able to see anything at first glance?

function get_user_email_domain() {
// Check if the user is logged in
if (is_user_logged_in()) {
// Get the current user's ID
$user_id = get_current_user_id();

// Get the user's email address
$user_email = get_userdata($user_id)->user_email;

// Extract and return the domain
list($user_name, $user_domain) = explode('@', $user_email);

return $user_domain;
}

return 'User not logged in';
}

function user_email_domain_shortcode() {
return get_user_email_domain();
}
add_shortcode('user_email_domain', 'user_email_domain_shortcode');

Thanks,
Chris

Victor Font replied 12 months ago

ChatGPT is notorious for not writing good code, especially anything specific to Formidable. As such, the code you posted retrieves the email from the WordPress user record, not a form field. THis means the user has to be registered and logged in. The code completely ignores Formidable data. If you need a developer's help, please see https://formidable-masterminds.com/developers-directory/

Chris Titus replied 12 months ago

<p>The user must be logged in to view my forms.  It produces the desired result - it displays the domain of the email address.  However, it isn't working inside of the frm-stat.  I believe [email] is also grabbing the user's email only if they are logged in, correct?</p><p>Also, I have entered this shortcode into a Text field where the domain is saved and appended with each form submission</p>

Victor Font replied 12 months ago

When a user fills out your registration form with the User Registration add-on enabled, you already have the user's email captured in the form data. At that point, it's a trivial task to separate the email ID and domain into separate hidden fields with jQuery/JavaScript that will work with frm-stats.

Your ChatGPT shortcode extracts the email from the WordPress user table. You can't count entries using frm-stat unless the data you want is in Formidable's data tables. The ChatGPT code is never going to work to accomplish your requirement. The design is wrong for Formidable. Please consult with a developer. ChatGPT is not a developer you can depend upon.

Chris Titus replied 12 months ago

Thanks for your response. I apologize if there is some miscommunication here. I think I see the problem as you are trying to explain it to me. The timing of processing a shortcode w/in a FF shortcode is not lining up. Whenever the user submits a purchase form, the form captures his email address, and now it captures his domain as well. I was thinking "The data is in the table." However, to run the frm-stats shortcode, it now needs to run another shortcode inside of it. It will be easier if I first capture the user's domain separately upon registration and reference that field in the frm-stats shortcode instead of processing another shortcode within it. Am I on the right track? Thanks for your patience!

Victor Font replied 12 months ago

Exactly!

Chris Titus answered 12 months ago

For anyone who needs the code ...  

Chris Titus replied 12 months ago

jQuery(document).ready(function($) {
var sourceField = '#field_wjqeu'; // Replace wjqeu with your source field key
var destinationField = '#field_iyhhr'; // Replace iyhhr with your destination field key

$(sourceField).on('input', function() {
var sourceValue = $(sourceField).val();
var atIndex = sourceValue.indexOf('@');
if (atIndex !== -1) {
var domain = sourceValue.substring(atIndex + 1);
$(destinationField).val(domain);
} else {
$(destinationField).val('');
}
});
});

Making the Best WordPress Plugin even better - Together

Take on bigger projects with confidence knowing you have access to an entire community of Formidable Experts and Professionals who have your back when the going gets tough. You got this!
Join the community
crossarrow-right