Skip Navigation

[Resolved] Possible to get userAgent info and send with email notification from CRED form?

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to capture the userAgent information from the browser in a CRED form, and access that information in an email notification.

Solution: In order to send the User Agent information to the admin in a notification email, it must be inserted as the value of a generic field. If you do not place this information in an input field of some kind, the email notification system will not be able to access it. If you do not want to show the User Agent information to the end User filling out the form, use a "hidden" type generic field. The value will still be captured, but it won't be shown on the front-end.

This generic field code should be added to the form:

[cred_generic_field field='useragent' type='hidden' class='' urlparam='']
{
"required":0,
"validate_format":0,
"default":"Form custom JavaScript error"
}
[/cred_generic_field]

Add this JavaScript in the CRED form's JS panel to insert the User Agent value in the generic field:

jQuery(window).bind('load', function(){
  jQuery('input[name="useragent"]').val(navigator.userAgent);
});

This code should be added to functions.php to allow you to access the generic field value in email notifications:

add_filter('cred_body_notification_codes', 'custom_generic_field_notification');
add_filter('cred_subject_notification_codes', 'custom_generic_field_notification');
 
function custom_generic_field_notification( $defaultPlaceHolders ) {
 
  $newPlaceHolders = array(
    '%%USER_AGENT%%' => $_REQUEST['useragent']
  );
 
  return array_merge($defaultPlaceHolders, $newPlaceHolders );
}

Then in your email notification, you can access the User Agent information like so:

User Agent: %%USER_AGENT%%

Relevant Documentation:
https://toolset.com/documentation/user-guides/inserting-generic-fields-into-forms/
https://toolset.com/documentation/user-guides/how-to-use-custom-placeholders-in-cred-notifications/

This support ticket is created 6 years, 8 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 7 replies, has 2 voices.

Last updated by jeffS-2 6 years, 8 months ago.

Assisted by: Christian Cox.

Author
Posts
#625904

Tell us what you are trying to do?
I have a CRED form for visitors to post jobs. Upon Submit, the form data is saved in a CPT and set as "Pending." An email is sent to Admin (me) with the same data. Is there a way to include the browser userAgent string in that email that is sent to me? I can't seem to get a simple line of javascript to work for this.

Thanks,
Jeff Safire

#625975

Hi, you should be able to use JavaScript to set the value of a generic field with the navigator.userAgent property. When adding JavaScript to manipulate a CRED form, you should execute your custom code in a window "load" event handler like this:

jQuery(window).bind('load', function(){
  jQuery('input[name="useragent"]').val(navigator.userAgent);
});

You may need to adjust the input name in this code to work with your actual generic field name.

#627611

This REQuirement for a Reply here is ridiculous. I've been complaining about this for over a year. It was a quick simple answer and fix from Christian and I already checked I am happy with the support from him. What more do I need to say here?
Please get rid of the "required" for this textarea field.

#627879

Christian -

Sorry I have to re-open...
I understand how to get the info with your one-liner code. However, I am trying to put this info into a notification email and I can't seem to figure out how to do that...
So, someone fills the form at hidden link to post a job. A notification email is then sent to the website admin (me). I want the userAgent to appear in the notification email, not in the form (obviously). I've been wrestling with that and can't seem to get it to work because I don't know how to connect the JQuery to the email.

Here is my notification email code:

<div style="width: 31.7em; height: auto; font-size: 18pt; padding: 0; border-width: 0.12em; border-color: #000; border-style: solid none none;">
<div style="width: 28.1em; height: 1.3em; text-align: left; color: #fff; background-color: #5a9fdb; font-family: 'Expletus Sans','roboto','Helvetica Neue','sans-serif'; letter-spacing: 0.03em; padding: 0.5em 1.5em 0.6em; font-size: 18pt; border-width: 0.3em; border-color: #5a9fdb; border-style: solid solid none;">IEEE-CNSV     <span style="font-size: 0.9em;">Consultants' Network of Silicon Valley</span></div>
</div>
<div style="width: 40.75em; height: auto; font-size: 14pt; padding: 0; border: 0.12em none #000;">
<div style="width: 37em; height: auto; background-color: #e8f8f0; padding: 1.5em; padding-top: 0.5em; font-family: roboto,helvetica-neue,sans-serif; font-size: 14pt !important; border: 0.4em solid #5a9fdb;"><span style="font-size: 0.9em; color: #333;">New Project Posted for CNSV Members…</span><br />&nbsp;<br /><span style="font-size: 0.8em; color: #a00; font-weight: bold;">Reminder: IEEE-CNSV Project postings are for the benefit of IEEE-CNSV <u>members</u> <u>only</u> and should not be forwarded to non-members!</span><br />&nbsp;<br /><span style="font-size: 1em;"><strong>%%POST_TITLE%%</strong></span><br />&nbsp;<br /><span style="font-size: 0.8em;"><strong>Summary:</strong></span><br />
<div style="font-size: 0.8em !important; color: #444;">[types field="project-summary-wysi" output="raw"][/types]</div>
<br />&nbsp;<br /><span style="font-size: 0.8em;"><strong>Responsibilities:</strong></span><br />
<div style="font-size: 0.8em !important; color: #444;">[types field="project-responsibilities-wysi" output="raw"][/types]</div>
<br />&nbsp;<br /><span style="font-size: 0.8em;"><strong>Qualifications:</strong></span><br />
<div style="font-size: 0.8em !important; color: #444;">[types field="project-qualifications-wysi" output="raw"][/types]</div>
<br />&nbsp;<br /><span style="font-size: 0.8em;"><strong>Start date:</strong></span> <span style="font-size: 0.8em !important; color: #444;">[types field="project-start-date" output="raw"][/types]</span> <br /><span style="font-size: 0.8em;"><strong>Length:</strong></span> <span style="font-size: 0.75em;">[types field="project-length-of-contract" output="raw"][/types]</span> <br /><span style="font-size: 0.8em;"><strong>On-site/remote?</strong></span> <span style="font-size: 0.75em;">[types field="project-work-on-site" output="raw"][/types]</span> <br /><span style="font-size: 0.8em;"><strong>Travel required?</strong></span> <span style="font-size: 0.75em;">[types field="project-travel-required" output="raw"][/types]</span> <br />&nbsp;<br /><span style="font-size: 0.8em;"><strong>Contact:</strong></span><br /> <span style="font-size: 0.85em;">[types field="project-contact-first-name" output="raw"][/types] [types field="project-contact-last-name" output="raw"][/types]</span> <br /><span style="font-size: 0.8em;">[types field="project-contact-email" output="raw"][/types]</span> <br /> <span style="font-size: 0.8em;">[types field="project-contact-phone" output="raw"][/types]</span> <br />&nbsp;<br /> <span style="font-size: 0.8em;"><strong>Company:</strong></span> <br /><span style="font-size: 0.75em;">[types field="project-company-name" output="raw"][/types] <br /> [types field="project-company-street-address" output="raw"][/types] <br /> [types field="project-company-city" output="raw"][/types], [types field="project-company-state" output="raw"][/types] [types field="project-company-zip-code" output="raw"][/types] <br /> [types field="project-company-country" output="raw"][/types]</span><br /><br />&nbsp;<br /><span style="font-size: 0.6em;">Project ID: %%POST_ID%% </span><br />&nbsp;<br /><span style="font-size: 0.8em;">See more consulting projects listed at <a href="<em><u>hidden link</u></em>" target="_blank">CaliforniaConsultants.org</a> (CNSV member log-in required).</span><br />&nbsp;<br /> <span style="font-size: 0.75em;">To stop receiving these New Project Notifications from the IEEE-CNSV Website, please <a href="mailto:bberg@gmail.com?subject=CNSV:%20UNSUBSCRIBE%20Me%20From%20Project%20Notifications&body=Please%20Unsubscribe%20me%20from%20the%20IEEE-CNSV%20New%20Project%20Notification%20emails.%0D%0AThanks!%0D%0A">UNSUBSCRIBE</a>.</span></div>
<div> <br /> </div>

[types field="usrAgntHere" output="raw"][/types]

</div>
#628045

I want the userAgent to appear in the notification email, not in the form (obviously).
In order to send the User Agent information to the admin in a notification email, it must be inserted as the value of a generic field. If you do not place this information in an input field of some kind, the email notification system will not be able to access it. If you do not want to show the User Agent information to the end User filling out the form, use a "hidden" type generic field. The value will still be captured, but it won't be shown on the front-end.

This code should be removed. The Types field shortcode is designed to display information already stored in a custom field on some post:

[types field="usrAgntHere" output="raw"][/types]

This generic field code should be added to the form:

[cred_generic_field field='useragent' type='hidden' class='' urlparam='']
{
"required":0,
"validate_format":0,
"default":"Form custom JavaScript error"
}
[/cred_generic_field]

When the CRED form loads, the JavaScript you added earlier will insert the User Agent value in the generic field:

jQuery(window).bind('load', function(){
  jQuery('input[name="useragent"]').val(navigator.userAgent);
});

This code should be added to functions.php to allow you to access the generic field value in email notifications:

add_filter('cred_body_notification_codes', 'custom_generic_field_notification');
add_filter('cred_subject_notification_codes', 'custom_generic_field_notification');

function custom_generic_field_notification( $defaultPlaceHolders ) {

  $newPlaceHolders = array(
    '%%USER_AGENT%%' => $_REQUEST['useragent']
  );

  return array_merge($defaultPlaceHolders, $newPlaceHolders );
}

Then in your email notification, you can access the User Agent information like so:

User Agent: %%USER_AGENT%%

More information about generic fields and custom email notification placeholders:
https://toolset.com/documentation/user-guides/inserting-generic-fields-into-forms/
https://toolset.com/documentation/user-guides/how-to-use-custom-placeholders-in-cred-notifications/

#628091

Hi Christian -

Thanks for this. I spent a half hour or more searching here but didn't realize those %%xxxx%% are called "placeholders." So, now I understand how these can work.

In the meantime, I was able to make a "kludge" and get it to work by:
1. Putting a div around the cred-field I used to first get the userAgent string into a field.
2. Set the div to visible=hidden.
3. Now the userAgent string is in the types field so I can reference it anywhere, like I need to in the email notification.
4. This also saves the string with the form in case I need it later. (Although, most likely the email notification would usually be enough as I am only collecting the userAgent for debugging - in case someone filling the form complains about a bug.)

Although this is somewhat of a kludge and possibly the "wrong" way to accomplish this, it is actually simpler than your latest suggestion. I will implement your method also to see how it works so I know how to do it in the future.

I don't really need further help unless you think there is something terribly wrong with the way I have currently implemented this. So otherwise, you can close this request but, I wanted to mention my method to you first.

Thanks for your excellent help.
Jeff

#628377

Okay great, glad you were able to find a simple solution that works for your project. If you have additional questions feel free to open a new ticket.

#630189

All is well.