Email Obfuscation with WordPress & Advanced Custom Fields

I tend to use WordPress as my main goto CMS and have found it’s use expanded greatly with Advanced Custom Fields (ACF) for tailoring the backend for clients.

I recently was asked to implement email obfuscation on a site using this setup so try to defeat crawlers/spammers (the endless fight…). I thought it’d be some use to share this with the world.

Here is what I started with…

<a href="mailto:<?php the_field('email'); ?>">
<?php the_field('email'); ?></a>

 

So currently the ACF field is called ’email’ and is being entered as the mailto: link and the description. WordPress has a built in obfuscation ability called Antispambot which converts email addresses characters to HTML entities to block spam bots (not bulletproof but better than nothing).

To use this with ACF, you just have to add a variable that you then declare in the <a>.

<?php $email = get_field("email"); ?>
<a href="mailto:<?php echo antispambot($email); ?>">
<?php echo antispambot($email); ?></a>

 

Thanks to willthemoor for the hint.