Skip Navigation

[Resolved] Restrict registration to work emails only

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.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 4 replies, has 2 voices.

Last updated by himanshuS 1 year, 3 months ago.

Assisted by: Waqar.

Author
Posts
#2524975

Hi there,
I have an employer account and would like to limit registration only for work emails.
I want to check the email address using $_POST or $fields but none of them returns the email address.

This is happening before logging in. How can I get email address? I am using AJAX.

 
<?php

// Put the code of your snippet below this comment.
function function_name($field_data, $form_data) {
 
    // Split $field_data into separate $fields and $errors
    list( $fields,$errors ) = $field_data;
      
    // validate specific form
    if ( $form_data['id'] == 'form-id' ) {
   //    $employeremail = $_POST['user_email'];
      $employeremail = $fields['user_email']['value'];
      
        if ( empty( $employeremail ) ) {
            // check if endorser email has been entered
            $errors['user_email'] = 'You must provide an email';
        } else {
          $publicemaildomains = array("gmail", "protonmail", "yahoo", "outlook", "yahoo", "hotmail", "live", "msn", "passport");
//          $current_user_id = get_current_user_id();
  //        $user_meta=get_userdata($current_user_id);
    //      $useremail = $user_meta->user_email;
          $useremailsplitpos = strpos($employeremail, '@');
          $useremailname = substr($employeremail, 0, $useremailsplitpos);
          $endorseremailsplitpos = strpos($employeremail, '@');
          $endorseremailname = substr($employeremail, 0, $endorseremailsplitpos);
          $endorseremaildomain = substr($employeremail, $endorseremailsplitpos + 1);
            foreach ($publicemaildomains as &$publicemaildomain) {
              if (strpos($endorseremaildomain, $publicemaildomain) !== false) {
                $errors['user_email'] = "Please register with a business email address";
                break;
              }
            }
          }
    return array($fields,$errors);
              }
}
add_filter( 'cred_form_validate', 'function_name', 10, 2 );
  ?>
#2525541

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi,

Thank you for contacting us and I'd be happy to assist.

I made small adjustments to the code and it seems to be working as expected on my test website:


add_filter('cred_form_validate','my_validation',10,2);
function my_validation($error_fields, $form_data)
{
	//field data are field values and errors
	list($fields,$errors)=$error_fields;    
	//validate if specific form
	if ($form_data['id']==123456)
	{
		$employeremail = $fields['user_email']['value'];

		if ( empty( $employeremail ) ) {
			// check if endorser email has been entered
			$errors['user_email'] = 'You must provide an email';
		}
		else
		{
			$publicemaildomains = array("gmail", "protonmail", "yahoo", "outlook", "yahoo", "hotmail", "live", "msn", "passport");
			$useremailsplitpos = strpos($employeremail, '@');
			$useremailname = substr($employeremail, 0, $useremailsplitpos);
			$endorseremailsplitpos = strpos($employeremail, '@');
			$endorseremailname = substr($employeremail, 0, $endorseremailsplitpos);
			$endorseremaildomain = substr($employeremail, $endorseremailsplitpos + 1);

			foreach ($publicemaildomains as &$publicemaildomain) {
				if (strpos($endorseremaildomain, $publicemaildomain) !== false) {
					$errors['user_email'] = "Please register with a business email address";
					break;
				}
			}
		}
	}
	//return result
	return array($fields,$errors);
}


I hope this helps and please let me know if you need further assistance.

regards,
Waqar

#2529223

Thanks for the note. It seems like you changed the parameter in the function to $error_fields.

Is this a new change or has this been always the case? Because I used examples from your documentation to write this code.

#2530347

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thanks for writing back.

Although the example function in our documentation uses 'error_fields' as the first parameter's name, it doesn't matter if you change it to something else like "field_data".
( ref: https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate )

I've just tested the exact code that you shared and it works on my test website too. Perhaps the form ID that you were using in place of 'form-id' wasn't correct?

#2530677

My issue is resolved now. Thank you!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.