Skip Navigation

[Resolved] Custom Code Snippets conflicting with each other

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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

This topic contains 14 replies, has 3 voices.

Last updated by simonM-5 1 year, 10 months ago.

Assisted by: Minesh.

Author
Posts
#2551807
Screenshot 2023-02-10 at 16.22.21.png
Screenshot 2023-02-10 at 16.22.13.png

Dear Support

Recently Minesh kindly provided us with code (Toolset Settings > Custom Code > func-validate-nanny-ad-approximate-location). See also ticket
https://toolset.com/forums/topic/prevent-toolset-forms-allowing-saving-of-non-german-addresses-with-toolset-maps/page/3/#post-2521441

The code should validate the address entered in the form on a Nanny Ad, to ensure it is an address within Germany.

I tried duplicating the code for Job Ads (func-validate-job-ad-dev-approximate-location), but now when I try to save a Job Ad, it complains about an array from the Nanny Ad code:

Warning: Undefined array key "wpcf-nanny-approximate-location" in /var/www/web24632223/html/dev/wp-content/toolset-customizations/function-validate-nanny-ad-dev-approximate-location.php on line 39

Likewise if I try to save a Nanny Ad, it complains about an array from the Job Ad code:

Warning: Undefined array key "wpcf-family-approximate-location" in
/var/www/web24632223/html/dev/wp-content/toolset-customizations/func-validate-job-ad-dev-approximate-location.php on line 39

To try to solve this error, I prefixed all the variables from the Job Ad version with "jobAd" so they would be different from the variables stored in the Nanny Ad version, so for example, $slug became $jobAdSlug etc, but that didn't solve the issue.

Furthermore,
Post Form - New Nanny Ad (ID: 921) and
Post Form - New Job Ad (ID: 929)

also had code entered in the JS Editor of the forms, which prevented Google from suggesting addresses outside of germany in the first place. This no longer seems to be working, and I was wondering if the new code has somehow broken this JS code too.

To reproduce the error, just enter a city not located in Germany, eg "Paris" or "London" and don't choose anything from the Google AutoComplete suggestions.

Thanks and kind regards
Simon

#2553031

Christopher Amirian
Supporter

Languages: English (English )

Hi there,

Would you please share the changes you have done to the original code so that we can check the details?

What I suspect is you maybe used "cred_form_validate" hook two times?

If yes, please avoid that and use the IF statement to select which form you are doing validation in and add the code there inside only cred_form_validate hook.

Thanks

#2553367

Hi Christopher

You can see my attempt to duplicate the code in the code snippet func-validate-job-ad-approximate-location

I see cred_form_validate in some of the other custom code snippets. And the code contains an IF statement, indicating which forms that should run on.

Kind regards
Simon

#2553435

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please check now.

I've adjusted the code for both forms as given under:

add_filter('cred_form_validate', 'validate_job_ad_approximate_location', 10, 2);
function validate_job_ad_approximate_location ( $error_fields, $form_data ) {
	
	$jobAdSlug = 'wpcf-family-approximate-location';
	list($fields,$errors)=$error_fields;
  if(isset($_POST[$jobAdSlug])){ 
   	$jobAdCountry = getJobAdGeoCountry($_POST[$jobAdSlug]);
	$jobAdCountry = strtolower(trim($jobAdCountry));
   }


	// Post Form - New Job Ad (929)
	// Post Form - Edit Job Ad (931)
	if (in_array($form_data['id'], array(929, 931))){
			if($jobAdCountry==0 or  $jobAdCountry!='germany'){
					$errors[$jobAdSlug] = __('Please enter a valid address in Germany');
			}
		}
		
	return array($fields,$errors);
}

and:

add_filter('cred_form_validate', 'validate_nanny_ad_approximate_location', 10, 2);
function validate_nanny_ad_approximate_location ( $error_fields, $form_data ) {

   $slug = 'wpcf-nanny-approximate-location';
	list($fields,$errors)=$error_fields;
    if(isset($_POST[$slug])){ 
      $country = getGeoCounty($_POST[$slug]);
      $country = strtolower(trim($country));
    }

	// Post Form - New Nanny Ad (921)
	// Post Form - Edit Nanny Ad (923)
	// [dev] Post Form - New Nanny Ad - Unverified Nanny (23612)
	if (in_array($form_data['id'], array(921, 923, 23612))){       
			if($country==0 or  $country!='germany'){
					$errors[$slug] = __('Please enter a valid address in Germany');
			}
		}
		
	return array($fields,$errors);
}

I can see now warning is gone. Can you please confirm it works at your end as well.

#2553499

HI Minesh

Thanks for the updated code. In preliminary testing the address can no longer be saved if the address is not in Germany.

However, as stated in the original description, since this code change, the Google autocomplete suggestion limiting to addresses in Germany no longer works. For Nanny Ads, the JavaScript limiting the date picker in the field AVAILABLE FROM DATE also no longer works. Any idea why the new code has "broken" the existing JS scripts? Is there some conflict or something, or an execution precedence or similar?

On Job Ads, the JavaScript for limited the date picker on START DATE seems to still be working.

Kind regards
Simon

#2553545

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

However, as stated in the original description, since this code change, the Google autocomplete suggestion limiting to addresses in Germany no longer works. For Nanny Ads, the JavaScript limiting the date picker in the field AVAILABLE FROM DATE also no longer works. Any idea why the new code has "broken" the existing JS scripts? Is there some conflict or something, or an execution precedence or similar?
==>
Can you please check now - it should be working. There was a JS code that is commented. I just uncomment that code and it seems to be working.

#2555581

HI Minesh

We are testing this in dev, and I will transfer the code to production. l want to be sure it works in Production as well, before I close the ticket.

Best regards
Simon

#2555631

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Sure - you can run your testing and feel free to mark resolve this ticket when you can.

#2558245

HI Minesh

quick update, we're still testing on production. Looks like we may be getting different results in different browsers for the autocomplete part. Need to clarify with my colleague that it's not a browser issue.

I'll update again asap.

Kind regards
Simon

#2558463

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

ok fine - thank you.

#2560753
Screenshot 2023-02-24 at 12.01.39.png

HI Minesh

We have checked in both environments now in multiple browsers and it appears to work as designed with the one little thing remaining:

The error message when the field is not entered correctly is shown in English, even when working in German. I cannot find this string in WPML String Translation and couldn't add it as an Admin string either.

How can we get the message below to work in German?

Kind regards
Simon

#2560765

PS the translation should be:

MEIN UNGEFÄHRER STANDORT IN DEUTSCHLAND: Bitte gib eine gültige Adresse in Deutschland ein

I already translated all instances in String Translation of MY APPROXIMATE LOCATION IN GERMANY that I could find.

Kind regards
Simon

#2560795

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please check now: hidden link

I've added the following "gettext" filter hook to your current theme's functions.php file to translate the text:

add_filter( 'gettext', 'custom_relationship_button_label', 20, 3 );
function custom_relationship_button_label( $translated_text, $text, $domain ) { 

	$my_current_lang = apply_filters( 'wpml_current_language', NULL );

    if($translated_text=='Please enter a valid address in Germany' and $my_current_lang=='de'){
		$translated_text = 'Bitte gib eine gültige Adresse in Deutschland ein';
	}
	
	if($translated_text=='MY APPROXIMATE LOCATION IN GERMANY' and $my_current_lang=='de'){
		$translated_text = 'MEIN UNGEFÄHRER STANDORT IN DEUTSCHLAND';
	}
	
    
    return $translated_text;
}
#2560883

Hi Minesh

That's done the trick and it's working for Nanny Ads and Job Ads!

Thanks a lot! Have a great weekend.

Kind regards
Simon

#2560885

My issue is resolved now. Thank you!