WordPress email cloak script in theme

Ben je bezig met de ontwikkeling van een WordPress website en wil je graag een manier om de emailadressen automatisch te cloaken, oftewel om te vormen zodat deze niet uitgelezen kunnen worden door spambots? Dan heb ik hier een oplossing voor je. Voeg onderstaande code toe in de functions.php van het theme dat je gebruikt en de emailadressen op je website zullen voortaan automatisch aangepast worden naar een javascript die niet uitgelezen kunnen worden door spambots.

Het script is wellicht verre van geoptimaliseerd, maar het werkt voor mij. Heb jij een betere versie? Laat het mij dan weten in de reacties.

add_filter('the_content', 'email_cloak_filter');
function email_cloak_filter($text) {
	// replace mailto links
	$res1 = preg_match_all(
		"/<a href=[\"|\']mailto:[a-z0-9]+([_\\.-][a-z0-9]+)*@([a-z0-9]+([\.-][a-z0-9]+)*)+\\.[a-z]{2,}/i",
		$text,
		$matches1
	);
	if($res1) {
		foreach(array_unique($matches1[0]) as $expr) {
			$pos1 = strpos($text,$expr);
			$pos2 = strpos($text,"</a>",$pos1);
			$mymatche1 = substr($text,$pos1,($pos2-$pos1+4));
			$res = preg_match_all(
				"/[a-z0-9]+([_\\.-][a-z0-9]+)*@([a-z0-9]+([\.-][a-z0-9]+)*)+\\.[a-z]{2,}/i",
				$expr,
				$matches3
			);
			$staart = strpos($matches3[0][0],"@");
			$to = substr($matches3[0][0],0,strpos($matches3[0][0],"@"));
			$domain = substr($matches3[0][0],strpos($matches3[0][0],"@")+1,strlen($matches3[0][0]));
			$company = substr($domain,0,strpos($domain,"."));
			$tld = substr($domain,strpos($domain,".")+1,strlen($domain));
			$replacement = '<script language="javascript">'."\n".
					'<!--'."\n".
					'var party1 = "'.$to.'";'."\n".
					'var part2 = "'.$company.'";'."\n".
					'var par3 = "'.$tld.'";'."\n".
					'document.write(\'<a class="maillink" href="&#109;a&#105;\' + \'l&#116;o:\' + party1 + \'&#64;\' + part2 + \'.\' + par3 + \'">\');'."\n".
					'document.write(party1 + \'&#64;\' + part2 + \'.\' + par3 + \'</a>\');'."\n".
					'// -->'."\n".
					'</script>';
			$text = str_replace($mymatche1,$replacement,$text);
		}
	}
	// replace mailto links beginning with title=
	$res2 = preg_match_all(
		"/<a title=[\"|\'](.*)[\"|\'] href=[\"|\']mailto:[a-z0-9]+([_\\.-][a-z0-9]+)*@([a-z0-9]+([\.-][a-z0-9]+)*)+\\.[a-z]{2,}/i",
		$text,
		$matches2
	);
	if($res2) {
		foreach(array_unique($matches2[0]) as $expr) {
			$pos1 = strpos($text,$expr);
			$pos2 = strpos($text,"</a>",$pos1);
			$mymatche2 = substr($text,$pos1,($pos2-$pos1+4));
			$res = preg_match_all(
				"/[a-z0-9]+([_\\.-][a-z0-9]+)*@([a-z0-9]+([\.-][a-z0-9]+)*)+\\.[a-z]{2,}/i",
				$expr,
				$matches4
			);
			$tstart = strpos($expr,'title=');
			$tend = strpos($expr,'" ',$tstart);
			$title = substr($expr,$tstart+7,$tend-$tstart-7);
			$staart = strpos($matches4[0][0],"@");
			$to = substr($matches4[0][0],0,strpos($matches4[0][0],"@"));
			$domain = substr($matches4[0][0],strpos($matches4[0][0],"@")+1,strlen($matches4[0][0]));
			$company = substr($domain,0,strpos($domain,"."));
			$tld = substr($domain,strpos($domain,".")+1,strlen($domain));
			$replacement = '<script language="javascript">'."\n".
					'<!--'."\n".
					'var party1 = "'.$to.'";'."\n".
					'var part2 = "'.$company.'";'."\n".
					'var par3 = "'.$tld.'";'."\n".
					'document.write(\'<a class="maillink" title="'.$title.'" href="&#109;a&#105;\' + \'l&#116;o:\' + party1 + \'&#64;\' + part2 + \'.\' + par3 + \'">\');'."\n".
					'document.write(party1 + \'&#64;\' + part2 + \'.\' + par3 + \'</a>\');'."\n".
					'// -->'."\n".
					'</script>';
			$text = str_replace($mymatche2,$replacement,$text);
		}
	}

	// replace email addresses
	$res = preg_match_all(
		"/[a-z0-9]+([_\\.-][a-z0-9]+)*@([a-z0-9]+([\.-][a-z0-9]+)*)+\\.[a-z]{2,}/i",
		$text,
		$matches
	);
	if ($res) {
		foreach(array_unique($matches[0]) as $email) {
			$staart = strpos($email,"@");
			$to = substr($email,0,strpos($email,"@"));
			$domain = substr($email,strpos($email,"@")+1,strlen($email));
			$company = substr($domain,0,strpos($domain,"."));
			$tld = substr($domain,strpos($domain,".")+1,strlen($domain));
			$replacement = '<script language="javascript">'."\n".
					'<!--'."\n".
					'var party1 = "'.$to.'";'."\n".
					'var part2 = "'.$company.'";'."\n".
					'var par3 = "'.$tld.'";'."\n".
					'document.write(party1 + \'&#64;\' + part2 + \'.\' + par3);'."\n".
					'// -->'."\n".
					'</script>';
			$text = str_replace($email,$replacement,$text);
		}
	}
	return $text;
}
Dit artikel delen:


Tags: , , , , , , , , , , , , , , , , , ,